Question:
What is wround with this c++ console source code?
Dave
2010-10-30 08:21:32 UTC
Ok, this is like one of my first c++ program. I just started learning yesterday and idk whats wroound with it. It compiles fine but when i enter all my data in the console, it closes

heres the code:


#include
using namespace std;

int main()
{

unsigned short ipod_nanos;
unsigned short ipod_touches;
unsigned short macbook_pros;
unsigned short quarter_pounders;

cout << "Hello, i am here to take your orders lol\n";
cout << "Enter number of ipod nanos: ";
cin >> ipod_nanos;

cout << "Enter number of ipod touches: ";
cin >> ipod_touches;

cout << "Enter number of macbooks pros: ";
cin >> macbook_pros;

cout << "Enter number of quarter pounders(fat ***): ";
cin >> quarter_pounders;

cout << "\n====================================";
cout << "\nHello i a here to take your order";
cout << "\n====================================";
cout << "\nyour order"
<< "\nItem Type Qty"
<< "\nipod nanos: " << ipod_nanos
<< "\nipod touches: " << ipod_touches
<< "\nmacbook pros: " << macbook_pros
<< "\nquarter pounders: " << quarter_pounders
<< "\n\n";
}
Three answers:
husoski
2010-10-30 09:54:22 UTC
What's probably happening is that the console window is closing before you can read the output. There are several ways to create a pause at the end of the program, and you've seen one good one and one bad one in previous answers. (The bad one has a thumbs-up from someone.)



If you are using a Visual Studio (or Express) version to develop, you can use "Start Without Debugging (Ctrl+F5)" and that has an automatic pause. The plain start button (F5) doesn't do this.



You can add your own pause at the end, too. I like:



while (cin.get() != '\n');

cout << "Press 'Enter'...";

cin.get();



...if I know that is included. The reason that cin.get() alone won't work is that the last cin>> that you did read the number of quarter pounders, but NOT the newline that ended that input line, or trailing spaces, or non-numeric leftover garbage. The while loop clears that out. Then you can do the *real* cin.get(), with or without a prompt.



Ignore the TurboC++ advice...you're not using it, otherwise you'd be including the ancient instead of .



If you know you're only going to compile on a Windows (or MS-DOS!) platform, you can include at the top and use:

system("pause");
Rajarshi
2010-10-30 15:37:08 UTC
I think you are using Turbo C++. In Turbo C++ the screen goes off fast just after displaying the output. To hault the screen you have to use



#include



just after #include statement.



Then use



getch();



instruction as the last instruction of the program. You can see the output after execution.
green meklar
2010-10-30 15:43:12 UTC
First, remove this part:



<< "\n\n";



If it still closes, then add this line at the end of main():



cin.get();


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...