| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Please help this newb at C++
I just bought a book a couple of days ago, and I am wondering why one of the programs won't work.
Here is the part of the script I am having trouble with. int x; cout<<"\nHow many of x? "; cin>>x; cout<<"x: " <<x<<endl; Every time I enter something for the value of x, the program closes itself out. It goes on if there is another cin statement, but it seems awkward to press enter a value and press enter to exit the program. It does the same thing on all of the scripts with cin that I have really looked at. It lets me enter it, it goes down to the next cin, and it exits right after the cin bypassing all other things in the program. How do I get it to just go on without having to place another cin statement further down? |
|
#2
|
||||
|
||||
|
I assume you're starting the program from windows explorer.
Try starting it from a dos prompt (Start->Run->'cmd', enter). That way, the window won't close after the program has finished.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 280
![]() |
|
#3
|
|||
|
|||
|
I typed it in Dev-C++ and just compiled and ran it from there.
|
|
#4
|
||||
|
||||
|
In that case you have to add another cin, so it waits before closing.
|
|
#5
|
|||
|
|||
|
If you need a little more info. on what I am trying to say here,
I am using the bloodshed Dev-C++ compiler, and here is an example of the script showing all of the things needed to show you what I mean. #include<iostream> using namespace std; int main() { int x; cout<<"what is the value of x? "; cin >> x; cout<<"x is" <<x<<endl; cout<<"Press enter to exit!"; cin.ignore(cin.rdbuf()->in_avail()+1); return 0; } When I enter the value for x (say 50), It just closes the program out, and it won't show what x equals in the cout command. Last edited by puit : February 10th, 2006 at 05:21 PM. Reason: forgot to add the lines about press enter to exit |
|
#6
|
||||
|
||||
|
That is, because the terminal window in which the program is running, closes as soon as it detects the program has finished running.
There are two solutions to this:
If you don't know how to operate a DOS terminal, search for a tutorial on the net. |
|
#7
|
|||
|
|||
|
Thanks for taking the time for helping me out.
|
|
#8
|
||||
|
||||
|
No problem.
|
|
#9
|
|||
|
|||
|
You can also type in
system ("PAUSE"); at the end of your code, right before the return 0. This will pause the screen and display a prompt "Press any key to continue..." which will exit the program. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Please help this newb at C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|