ArtOfWarfare

May 9, 2012, 03:05 PM

I'm making a simple command line tool in C++. It asks the user a yes or no question and needs a valid response before it can move on.

Here's my code:

#include <iostream>

using namespace std;

int main(int argc, char* argv[])

{

askComplex:

cout << "Will complex quoefficients be needed?" << endl << "Y/N > ";

char complex = 'U';

cin >> complex;

switch (complex)

{

case 'Y': case 'y':

cout << "Complex coefficients will be used." << endl;

cout << "Enter a1's real component > ";

break;

case 'N': case 'n':

cout << "Complex coefficients will not be used." << endl;

cout << "Enter a1 > ";

break;

default:

cout << complex;

cout << " isn't valid input. Enter Y for yes or N for no." << endl;

cout << endl;

goto askComplex;

break;

}

return 0;

}

Here's some sample input/output...

First off, several examples of it working properly:

Will complex quoefficients be needed?

Y/N > Y

Complex coefficients will be used.

Enter a1's real component >

Will complex quoefficients be needed?

Y/N > Yes

Complex coefficients will be used.

Enter a1's real component >

Will complex quoefficients be needed?

Y/N > No

Complex coefficients will not be used.

Enter a1 >

Will complex quoefficients be needed?

Y/N > U

U isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Y

Complex coefficients will be used.

Enter a1's real component >

Here's where I have an issue... if the user types more than one character, when it gets to cin, it'll just take the next character from what the user already entered! How can I make it forget about what the user previously entered?

Will complex quoefficients be needed?

Y/N > Shucks, beats me!

S isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > h isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > u isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > c isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > k isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > s isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > , isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > b isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > e isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > a isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > t isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > s isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > m isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > e isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > ! isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Maybe

M isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > a isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Complex coefficients will be used.

Enter a1's real component >

Here's my code:

#include <iostream>

using namespace std;

int main(int argc, char* argv[])

{

askComplex:

cout << "Will complex quoefficients be needed?" << endl << "Y/N > ";

char complex = 'U';

cin >> complex;

switch (complex)

{

case 'Y': case 'y':

cout << "Complex coefficients will be used." << endl;

cout << "Enter a1's real component > ";

break;

case 'N': case 'n':

cout << "Complex coefficients will not be used." << endl;

cout << "Enter a1 > ";

break;

default:

cout << complex;

cout << " isn't valid input. Enter Y for yes or N for no." << endl;

cout << endl;

goto askComplex;

break;

}

return 0;

}

Here's some sample input/output...

First off, several examples of it working properly:

Will complex quoefficients be needed?

Y/N > Y

Complex coefficients will be used.

Enter a1's real component >

Will complex quoefficients be needed?

Y/N > Yes

Complex coefficients will be used.

Enter a1's real component >

Will complex quoefficients be needed?

Y/N > No

Complex coefficients will not be used.

Enter a1 >

Will complex quoefficients be needed?

Y/N > U

U isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Y

Complex coefficients will be used.

Enter a1's real component >

Here's where I have an issue... if the user types more than one character, when it gets to cin, it'll just take the next character from what the user already entered! How can I make it forget about what the user previously entered?

Will complex quoefficients be needed?

Y/N > Shucks, beats me!

S isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > h isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > u isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > c isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > k isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > s isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > , isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > b isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > e isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > a isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > t isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > s isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > m isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > e isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > ! isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Maybe

M isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > a isn't valid input. Enter Y for yes or N for no.

Will complex quoefficients be needed?

Y/N > Complex coefficients will be used.

Enter a1's real component >