Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 25th, 2006, 2:03 PM   #1
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Bloodshed help

When running any c++ program that requires an input value(see below), bloodshed will automatically close the console window once the user hits enter. I thought that the
cin.get();
line might work, but no luck. Any suggestions?

#include <iostream>
using namespace std;

int main ()
{
    int i;
    cout << "Please enter an integer value: ";
    cin >> i;
    cout << "The value you entered is " << i;
    cin.get();
    return 0;
}
snipertomcat is offline   Reply With Quote
Old Jan 25th, 2006, 2:08 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If cin.get() doesn't work, that means you have more in the buffer than you've read. Consequently, cin.get () takes part or all of it, and having done its job, returns. Execution then continues to the end of the program. Use cin.sync () prior to the cin.get () to discard the contents of the buffer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 25th, 2006, 2:09 PM   #3
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 3 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Or just do this:
#include <iostream>
using namespace std;

int main ()
{
    int i;
    cout << "Please enter an integer value: ";
    cin >> i;
    cout << "The value you entered is " << i << endl;
    system("pause");
    return 0;
}
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Jan 25th, 2006, 2:15 PM   #4
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Works like a charm. Thanks alot guys.
snipertomcat is offline   Reply With Quote
Old Jan 25th, 2006, 2:18 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
system ("pause") is, however, not portable. If that's of no consequence to you, go for it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 25th, 2006, 2:22 PM   #6
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Would cin.sync () be better to use then?
snipertomcat is offline   Reply With Quote
Old Jan 25th, 2006, 2:58 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
cin.sync is for synchronizing, it has nothing do with pausing the program at the end.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Jan 25th, 2006, 2:59 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
cin.sync () followed by cin.get (), as stated in my initial response.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 25th, 2006, 3:04 PM   #9
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Cool thanks.
snipertomcat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC