#### Archived

This topic is now archived and is closed to further replies.

# new to programming. cin.get ( ) not working.

## Recommended Posts

i''ve been programming c++ for 2 days know. i''m using online tutorials to see if it''s worth a book. so far, it''s looking like i''m making a trip to the library. but that''s not what this post is about. I was practicing my if, then statements, but when i run the program and it runs throught the if statements fine, but the results come up to fast, then it closes. i''m using cin.get () before the return 0, but it still flashes by. what''s up? here''s the code

#include <iostream.h>

int main ()
{
int salir;
int age;
cout << "Grade Finder - By Gomeza54" << endl;
cin >> age;
cout << endl;
// begin the if statements

// if the age is less that 4 (4-) then print out this and end program

if (age<6) {
cout << "You are to young to be in school!" << endl;
cout << "Press any key. ";
cin.get ();
return 0;
}
// if the age is more than 18 (19+) then print out this and end program

else if (age>18) {
cout << "You are too old to be in school!" << endl;
cout << "Press any key. ";
cin.get ();
return 0;
}
// if the age can go to school then run the function grade

else {
cout << "You are in Grade " << (age-6) << " or Grade " << (age-5) << "." << endl;
cout << "Press any key. ";
cin.get ();
return 0;
}
}  
ps - i made not have used the right terms...

##### Share on other sites
cin.get() is non-blocking. Use getch (conio.h) instead.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
Thanks to Kylotan for the idea!

##### Share on other sites
Hi,

I am afraid that is not exactly correct, cin.get() is blocking providing there is no pending input in the stream. Also in general I thought that mixing C++ streams and traditional C I/O was not a good idea. (see Effective C++ for example)

The reason it is scrolling by Gomeza, is because input with the iostream lib is done using streams, you can think of these a big buffers or road-side stops where the information waits to be "extracted" infact the >> is called the Extraction Operator.

Anyways, after your first cin >> age, the carriage return remains in the stream (age only takes the int!). So when you call cin.get() the function sees that carriage return is still in the stream so it will return that ascii value for a carriage return (the value 10). For instance:

cin >> age;
cout << endl;

if (age<6) {
cout << "You are to young to be in school!" << endl;
cout << "Press any key. ";
int a = cin.get();
char b = ''\n'';
cout << a << endl;
cout << (int)b << endl;
return 0;
}

Will display 10, 10.

Anyways to fix your program you have to PURGE that carriage return that is still in the Stream. You can modify your program as such to do it to use the cin.ignore() function.

cin >> age;
cout << endl;
cin.ignore();
..... // The rest as in your post

Enjoy, HTH
- James

##### Share on other sites
I think you have to use two cin.get() or cin.getline() functions to stop the screen from exiting so fast. Try that

Bill Gates is my Pool Boy!!
Nothing is to good for me!!

##### Share on other sites
yes! i got it to work. thanks to the anonymous poster. i got it working exactly how i wanted. thanks to all those who posted other suggestions. i think i''m gonna make a couple more simple programs with if statements then i''ll move on to harder stuff...

##### Share on other sites
Also, by default the delimeter for ignore is '\n'. You can use ignore to ignore n number of characters until you meet a ';' or anything at all.

For functions that take in the carriage return or '\n', you don't have to use ignore. For example, using cin.getline(...) will eat the '\n' so you don't have to use ignore().

[edited by - Darkor on April 29, 2002 12:53:38 AM]

• ### Forum Statistics

• Total Topics
628345
• Total Posts
2982195

• 9
• 24
• 10
• 9
• 13