flush cin buffer ?
Jun 7, 2012 at 10:23pm UTC
when i run the program works fine, but after continue it reverse the new number but shows also the previous number. how can i clean the cin buffer ?
i tried with cin.ignore() but didn't work
thanks for the help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n,m=0;
char next;
do
{
cout<<"\n\nnumber to reverse: " ;
cin >> n;
while (n>0)
{
m *= 10;
m += n%10;
n /= 10;
}
cout<<"\nreversed number: " <<m << "\n\n\n" ;
cout<<"\n continue (y/n) " ;
cin>>next;
cin.ignore();
}
while (next == 'y' || next == 'Y' );
return 0;
}
Jun 7, 2012 at 10:52pm UTC
Initialize variable 'm' inside the do-while loop. That way, every time it loops, m resets.
Jun 8, 2012 at 5:31am UTC
thanks :)
Topic archived. No new replies allowed.