Jump to content

  • Log In with Google      Sign In   
  • Create Account


Requesting Assistance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 ThinkingsHard   Members   -  Reputation: 209

Like
0Likes
Like

Posted 27 November 2012 - 03:13 PM

Hello!

I have gotten back into my programming groove, after a small break, however I never got very far. I switched from Java to C++, a lot of people suggested I do so, and on top of that, a friend at work uses C++, I hope to eventually go back and learn how to perform the operations in Java, however for now I am sticking with C++.

Anyway, enough about my lifes story! Haha.

I've been reading Stoustrup's Programming Principles and Practice using C++, and I was doing one of the 'drills', when I noticed how ugly the command prompt gets with all the cin and cout in the same window.

I looked around however, I didn't see anything that was readable at my level related to my question.

The Question!

How do I request the inputs to be in a popup / child window instead of the parent / main window, to keep the program cleaner.



( Incoming Code, this is what I have made so far, and I know I don't need to include all of the #include's that I did... But they make me feel happier about my life, which is a good enough reason to have them!)

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin>>ch;}

int main(){

cout<<"Please enter the name of the person you wish to write to.\n";
string first_name;
cin >> first_name;
cout<<"Dear" << " " << first_name <<",\n";
cout<<"How are you?\n";
string first_response;
cin >> first_response;
cout <<"I miss you.\n";
cout << "Please enter the name of our friend.\n";
string friend_name;
cin >> friend_name;
cout << "Have you seen" << " " << friend_name<< " " << "lately?\n";
cout << "Enter the sex of" << " " << friend_name << " " << "as a 'm' or 'f'.\n";
char friend_sex = 0;
cin >> friend_sex;
if (friend_sex == 'm'){
cout<<"If you see" << " " << friend_name <<" " << "please have him call me.\n";
}
if (friend_sex == 'f'){
cout<<"If you see" << " " << friend_name <<" " << "please have her call me.\n";
}
cout<< "Please enter recipients age\n";
int age;
cin >> age;
if (age != 0 && age < 110)
{
cout << "I hear you recently had a birthday, and you are now" << " " << age << " " << "years old.\n";
}
else
{
cout << "You're kidding!";
}
if (age ==17)
{
cout<< "Next year you will be able to vote!";
}
if (age > 70)
{
cout << "I hope you are enjoying retirement!";
}
cout << "Next year you will be" << " " << age+1 << ".\n";
cout << "Yours Sincerely\n";
cout << "\n";
cout << "\n";
string writers_Name;
cin >> writers_Name;
cout << writers_Name;

keep_window_open();
}




Thanks!

Sponsor:

#2 Serapth   Crossbones+   -  Reputation: 5010

Like
0Likes
Like

Posted 27 November 2012 - 03:21 PM

You can do what you are talking about, have one application create a second console window, so you can have one for input and one for output.

That said, it is not a trivial thing to do. In fact it would be a fair bit of work. On windows for example, you could use the Console APIs to accomplish this, but trust me, such a task is not something you probably want to embark on.

Basically what happens in C++ is each application has something called stdin and stdout. These are simply buffers for text input and output. Where the actual text goes is somewhat irrelevant to C++. When you use cout or cin, you are actually writing to or reading from stdin and stdout, at least by default. Problem is, by default a new terminal window means a new process, which means a new cin and cout. You can easily redirect stdout and stdin, say... to a file. But redirecting the results to another running application is much more complicated involving cross-process communications of some form, such as a pipe.

Edited by Serapth, 27 November 2012 - 03:33 PM.


#3 ThinkingsHard   Members   -  Reputation: 209

Like
0Likes
Like

Posted 27 November 2012 - 03:27 PM

Alright.... Going to look up how to do that, and implement it.... Man this drill has no idea what's coming for it! Thanks!

#4 Serapth   Crossbones+   -  Reputation: 5010

Like
2Likes
Like

Posted 27 November 2012 - 03:35 PM

Well if you are a glutton for punishment, and working on Windows, start here. The trickiest part is, your console application is going to actually be a Windows application behind the scenes. The logististics of what that actually means can prove a bit confusing, beyond the fact one type of application has a main() and the other has a WinMain() entry point. It should prove an interesting learning experience, although I warn you Alice, this rabbit hole is deep, dark and kinda scary.

EDIT:

You can probably go the pure console app route using spawn and a pipe to control communications, that should be more cross platform but perhaps a bit more confusing at the end of the day. This example illustrates using a pipe to communicate between a parent and child process opened using spawnl().

Edited by Serapth, 27 November 2012 - 03:56 PM.


#5 kuramayoko10   Members   -  Reputation: 383

Like
0Likes
Like

Posted 27 November 2012 - 03:36 PM

Wouldn't it require working with a window API, like Win32?

I looked up here and I found this old thread. I think it is a good start.

Well, if you manage to do it don't forget to post here how you did it. This is something I had never thought about.

Edited by kuramayoko10, 27 November 2012 - 03:38 PM.

Programming is an art. Game programming is a masterpiece!

#6 ThinkingsHard   Members   -  Reputation: 209

Like
1Likes
Like

Posted 27 November 2012 - 03:41 PM

Serapth, I take your warning seriously, however know that Alice isn't afraid of the dark, does not easily scare, and likes long walks in a forest talking about the philosophical problems and solutions related to Chance as a manipulable force.

Kuramayoko10, I will indeed my good sir / ma'am.

#7 brx   Members   -  Reputation: 626

Like
0Likes
Like

Posted 27 November 2012 - 04:03 PM

Apparently Serapth already answered your question, but to be honest, I don't get it... do you want to have the input requests in a different console than the output? That would, imho, not make it cleaner but totally confusing. Afterall, it's a question and answer game and I would think it's weird to give the answer to someone else than the one who asked me... I'd suggest to delay those pop-up windows until you start with GUI programming (which is not hard at all... take a look at wxwidgets or Qt, for example).

Until then I'd suggest to stick with the "ugliness" of the console and be happy (since you are coming from Java) that cin and cout is so much shorter than System.out.println and
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
(yeah, I know nowadays, there's the java.io.Console class, but even that is so much more than a simple "cin"...)
;)

P.S.: I really like Java, don't get me wrong, please!

#8 ThinkingsHard   Members   -  Reputation: 209

Like
0Likes
Like

Posted 27 November 2012 - 05:04 PM

brx, by ugliness, I meant it in a different way than you, I guess.

In the current form of the program, it takes all the inputs and outputs on the same screen, so when it asks you a question, you answer it in that same window, and then it responds, for instance, at the very end of the program, it asks you to say

Yours Sincerely,


<Your name>

However it ends up printing, because of the inputs being saved in the same window

Yours Sincerely,


<Your name>
<Your name>

If the inputs were accepted in a different window, then it would be a lot easier to read, it would make more sense and be overall nicer to the user.

#9 Matt-D   Crossbones+   -  Reputation: 1405

Like
0Likes
Like

Posted 27 November 2012 - 07:59 PM

Don't worry, when you reach Chapter 12 you're going to be introduced to graphical user interfaces (GUIs) &amp; graphics :-) Chapter 16 further expands on GUIs.
// BTW, Chapter 12 is available freely: http://www.stroustrup.com/Programming/programming_ch12.pdf

Edited by Matt-D, 27 November 2012 - 08:04 PM.


#10 ThinkingsHard   Members   -  Reputation: 209

Like
0Likes
Like

Posted 27 November 2012 - 09:36 PM

I have the entire book on a PDF, 1285 pages or something like that. It's been great fun. I'm just starting Chapter 3. A lot of this stuff I already knew, from Java, and I just needed to learn how to properly translate my operations into C++. And as someone said previously, It feels a lot better to type cin or cout<< stuff << more stuff << more stuff << final stuff;

Rather than typing System.out.println


I was reading on the pipes, and spawns. And I'm going to keep reading the chapters in this PDF, and if it fails to teach me what I want (Which is simply a new window to take in he inputs and then accept those into the main window, as both in 1 window gets redundant and ugly) I shall (as I have already bookmarked the pages) find other materials to teach myself how to do said thing. I hope to have a working version within a week, that I can reply to this thread and say "Haha! I am pleased with myself! I did it by..."

Thanks to everyone for their replies. This is why I want to be a programmer, so far you are nothing but helpful, as though you want more professionals to communicate and work with. :P

#11 Khatharr   Crossbones+   -  Reputation: 2616

Like
0Likes
Like

Posted 28 November 2012 - 04:07 AM

In terms of the code maybe you could create a function that asks a question and then gets a valid answer. I don't know if that kind of thing is something that you could do yet but it would cut down on some of the clutter there a little. This one's not too bad, but long functions make me scared, lol.

Also, something that may make your life a little easier:

cout << "Have you seen" << " " << friend_name<< " " << "lately?\n";

could just as easily be:

cout << "Have you seen " << friend_name<< " lately?\n";

Unless you're after explicit spacing there, which is fine.

Finally, please enclose code on the forum in source braces. When you're making a post and you want to insert code just click the little red brackety button at the end of the second line of buttons above the text field. It makes it a lot easier to read and has syntax hilighting and stuff:

[source lang="cpp"]cout << "How many times a day do you cin?\n";int times = 0;cin >> times;cout << "Really!? " << times << " times?\nRepeat after me: \"Forgive me console, for I have cin'ed.\"\n";[/source]

Edited by Khatharr, 28 November 2012 - 04:09 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS