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 August 1st, 2005, 11:00 AM
Sun Sun is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 24 Sun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 3 m 19 sec
Reputation Power: 0
Why need two cin.get() ???

Hello everyone,

I write below programming exercise but, why i need to write two time cin.get () at the end in order to "hold" the screen??? If i just wrote one cin.get (), I found the progamme automatically close the screen after run the programme.

I am using DEV++, will it be a reason? Thank you.


// Programme to read and print the absolute value //

#include <iostream>
using namespace std;

int main ()
{
int value;

cout << " Please enter a integer : ";
cin >> value;
if( value < 0 )
value = -value;
cout << " The absolute value of the integer is :" << value <<endl;
cin.get();
cin.get();

return 0;
}

Reply With Quote
  #2  
Old August 1st, 2005, 01:30 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
This is because of a keyboard buffer error. When you press <Enter>, all the keys you just entered get flushed through to the program, but the newline itself rarely does, thus the next time you request input there is only a single newline in the buffer so it automatically reads that in and, since it's a newline, terminates the stream right there.

Thus, your first cin.get() is just reading in the leftover newline from your first cin>>, and the second cin.get() is actually pausing the program.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old August 3rd, 2005, 06:22 AM
Sun Sun is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 24 Sun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 3 m 19 sec
Reputation Power: 0
Thanks B-con,

So, what should i suppose to do in order to de-bug it??

Thank you very much.

Reply With Quote
  #4  
Old August 3rd, 2005, 02:19 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
To fix the problem of using two cin.get()'s, I personally use the line "fseek(stdin,0L,SET_END)" before every string (or individual character) I read in, that will clear the buffer of any residue from previous inputs.

ie:
Code:
fseek(stdin,0L,SET_END); 
cin>> str; 
. . . 
fseek(stdin,0L,SET_END); 
cin.get(); 

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Why need two cin.get() ???


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 3 hosted by Hostway