-
Ultimate Member
cin.get not working?
When I compile and execute this C++ program (by double clicking the executable file), after the program executes, the DOS window closes and I can't see the output.
cin.get() used to work for me but somehow it is not working now. Does anybody see anything that might be causing this problem? Thanks.
-
I can't see your code, but one solution is usually to introduce an extra variable in main (say char garbage), in a cin >> which forces the program to stop. It won't continue to execute until you press a key.
Cheers,
Nini-Veh
-
Ultimate Member
Here...
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
const int buffer = 10;
char book[] = "The Little Prince";
char author[buffer];
cout << "One of my favorite books is " << book << endl;
cout << "\nSpelled backwards that's";
for(int i = sizeof(book) - 1; i >= 0; i--)
cout << book[i];
cout << "\n\nPlease enter the author's last name: ";
cin >> author;
cout << "\nWhat a coincidence!\n"
<< author << " is my last name.\n\n";
cout << "Press enter to exit.";
cin.get();
return 0;
}
-
Banned
-
Ultimate Member
There is no compile errors. The program compiles just fine.
-
Banned
i hate it when people do this to me, but you might be using it wrong, im not sure, ive never really used cin.get().
youll probably learn something new by reading this anyways
http://cplusplus.com/ref/#libs
-
Hired Geek
You need to clear the input stream - your cin.get is reading the key in from the last input. Use:
Code:
cin.sync();
cin.get();
return 0;
at the end instead
-
Ultimate Member
Thanks fishy. What is the difference between cin.clear() and cin.sync()? I tried using cin.clear() once you said that I need to clear the input stream but it didn't work. cin.sync() worked just fine though.
-
Hired Geek
cin.clear() "fixes" a stream if a previous cin operation returned an error (bad input), whereas cin.sync() clears the input buffer. That's my understanding anyway - console programming's not been my thing for a while
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|