C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 10th, 2006, 03:32 PM
puit puit is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 4 puit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 12 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old February 10th, 2006, 03:52 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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

Reply With Quote
  #3  
Old February 10th, 2006, 03:58 PM
puit puit is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 4 puit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 12 sec
Reputation Power: 0
I typed it in Dev-C++ and just compiled and ran it from there.

Reply With Quote
  #4  
Old February 10th, 2006, 05:02 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
In that case you have to add another cin, so it waits before closing.

Reply With Quote
  #5  
Old February 10th, 2006, 05:03 PM
puit puit is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 4 puit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 12 sec
Reputation Power: 0
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

Reply With Quote
  #6  
Old February 10th, 2006, 05:31 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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:
  1. You add something at the end of the program to pause it while you read the output. The extra cin accomplishes this, since the program waits for more input before finishing.
  2. You start the terminal program SEPERATELY form DevCpp, and run the program by going to the directory and executing the binary (.exe). Since the terminal program is already running, it won't close upon completion, but simply return to the command prompt, allowing you to start the program again, start another program, or do whatever the hell you like (That's what terminals do, they let you do what you want to do).

If you don't know how to operate a DOS terminal, search for a tutorial on the net.

Reply With Quote
  #7  
Old February 10th, 2006, 05:48 PM
puit puit is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 4 puit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 51 m 12 sec
Reputation Power: 0
Thanks for taking the time for helping me out.

Reply With Quote
  #8  
Old February 11th, 2006, 01:37 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
No problem.

Reply With Quote
  #9  
Old February 11th, 2006, 03:37 PM
Aurora Aurora is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 1 Aurora User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 18 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Please help this newb at C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT