#1 Members - Reputation: 128
Posted 27 November 2012 - 03:13 PM
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!
#2 Members - Reputation: 3284
Posted 27 November 2012 - 03:21 PM
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.
#4 Members - Reputation: 3284
Posted 27 November 2012 - 03:35 PM
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 Members - Reputation: 382
Posted 27 November 2012 - 03:36 PM
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.
#6 Members - Reputation: 128
Posted 27 November 2012 - 03:41 PM
Kuramayoko10, I will indeed my good sir / ma'am.
#7 Members - Reputation: 444
Posted 27 November 2012 - 04:03 PM
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 Members - Reputation: 128
Posted 27 November 2012 - 05:04 PM
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 Members - Reputation: 845
Posted 27 November 2012 - 07:59 PM
// 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 Members - Reputation: 128
Posted 27 November 2012 - 09:36 PM
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.
#11 Members - Reputation: 1575
Posted 28 November 2012 - 04:07 AM
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.
There are ten kinds of people in this world: those who understand binary and those who don't.






