Requesting Assistance

Started by
9 comments, last by Khatharr 11 years, 4 months ago
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!
Advertisement
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.
Alright.... Going to look up how to do that, and implement it.... Man this drill has no idea what's coming for it! Thanks!
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().
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.
Programming is an art. Game programming is a masterpiece!
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.[sup] [/sup]
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!
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.
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
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

This topic is closed to new replies.

Advertisement