S
subramanian100in
I want to do the following:
read an integer
do some processing
ask the user if he wants to continue.
if yes, continue the above.
For this I wrote the following code:
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
inline void process(int arg)
{
cout << "From process() function: " << arg << endl;
}
int main()
{
char flag;
do
{
cout << "Enter an integer: ";
int arg;
if (!(cin >> arg))
break;
process(arg);
cout << "Continue(Y/N) ? ";
} while (cin >> flag && toupper(flag) == 'Y');
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 and works fine as long as
correct input is entered. However this program does not work correctly
for wrong test input explained as follows:
Now consider the following scenarios.
Scenario 1: Suppose I run this program. It asks:
Enter an integer:
For this, suppose I enter, '10Y'. Then it displays:
From process() function: 10
Continue(Y/N) ? Enter an integer:
What happens is that for 'Continue(Y/N) ?', it does not wait for the
input because the 'Y' entered(in '10Y') for the previous integer, is
taken as the input for the 'Continue' flag. Here How do I get rid of
the input remaining after reading the integer ? ie How to remove all
the characters remaining after the integer ?
Scenario 2: Suppose I run this program. It asks:
Enter an integer:
For this, suppose I enter, '10'. Then it displays:
From process() function: 10
Continue(Y/N) ?
For this suppose I enter 'Y20'.
Then it displays:
Enter an integer: From process() function: 20
Continue(Y/N) ?
Here it does not wait for the integer input during the second pass.
What happens is that the 'Y' in 'Y20' is taken for 'Continue' flag and
the remaining integer '20' is taken for the subsequent read of the
integer value. Here how do I get rid of all the characters after the
first character for the flag ?
Is there a better program for the problem I stated in the beginning of
this post? Kindly provide the code.
Thanks
V.Subramanian
read an integer
do some processing
ask the user if he wants to continue.
if yes, continue the above.
For this I wrote the following code:
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
inline void process(int arg)
{
cout << "From process() function: " << arg << endl;
}
int main()
{
char flag;
do
{
cout << "Enter an integer: ";
int arg;
if (!(cin >> arg))
break;
process(arg);
cout << "Continue(Y/N) ? ";
} while (cin >> flag && toupper(flag) == 'Y');
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 and works fine as long as
correct input is entered. However this program does not work correctly
for wrong test input explained as follows:
Now consider the following scenarios.
Scenario 1: Suppose I run this program. It asks:
Enter an integer:
For this, suppose I enter, '10Y'. Then it displays:
From process() function: 10
Continue(Y/N) ? Enter an integer:
What happens is that for 'Continue(Y/N) ?', it does not wait for the
input because the 'Y' entered(in '10Y') for the previous integer, is
taken as the input for the 'Continue' flag. Here How do I get rid of
the input remaining after reading the integer ? ie How to remove all
the characters remaining after the integer ?
Scenario 2: Suppose I run this program. It asks:
Enter an integer:
For this, suppose I enter, '10'. Then it displays:
From process() function: 10
Continue(Y/N) ?
For this suppose I enter 'Y20'.
Then it displays:
Enter an integer: From process() function: 20
Continue(Y/N) ?
Here it does not wait for the integer input during the second pass.
What happens is that the 'Y' in 'Y20' is taken for 'Continue' flag and
the remaining integer '20' is taken for the subsequent read of the
integer value. Here how do I get rid of all the characters after the
first character for the flag ?
Is there a better program for the problem I stated in the beginning of
this post? Kindly provide the code.
Thanks
V.Subramanian