In my program, I have a cout statement I want peopl to be able to rea, so I use cin.get() to pause. For some reason, it skips that and ends. I think its my cpu speed or keyboard I unno. Can someone help?
Printable View
In my program, I have a cout statement I want peopl to be able to rea, so I use cin.get() to pause. For some reason, it skips that and ends. I think its my cpu speed or keyboard I unno. Can someone help?
Since you did not post any code I will guess and say add "cin.ignore()".
This is a common question. A search of the board would probably give more of a explanation. But here are some examples.
This will work.
But this will not.Code:#include <iostream>
using namespace std;
int main()
{
cout << "Enter hours worked" << endl;
cin.get();
return 0;
}
This will.Code:#include <iostream>
using namespace std;
int main()
{
cout << "Enter hours worked" << endl;
int var1 =0;
cin >> var1;
cout << "Hours worked = " << var1 << endl;
cout << "Press the Enter to continue" << endl;
cin.get();
return 0;
}
Long story short you need to get rid of the '\n' that was left in the stream. If the search does not yield good results please post back with any questions.Code:#include <iostream>
using namespace std;
int main()
{
cout << "Enter hours worked" << endl;
int var1 =0;
cin >> var1;
cout << "Hours worked = " << var1 << endl;
cout << "Press the Enter to continue" << endl;
cin.ignore();
cin.get();
return 0;
}
Your cin.ignore() worked, but first I had to switch cin>> with cin.getline() because it missed the spaces and skipped the cin.get(). Thanks for the help!!! :)
...then you don't need cin.ignore().Quote:
but first I had to switch cin>> with cin.getline()
just out of preference, i use getch(). lol. i don't know why.