Counting the Characters a user inputs

Okay, I have a while loop with a for loop that waits till the user inputs one of two acceptable answers y(yes), and n(no). The loop is correct and all, but when the user inputs more than one character the loop goes through multiple times, and writes out the initial question several times. My question is, how do I make my program catch that the whole string the user inputs is wrong instead of checking every single character?

bool x= true;
char lExt;
while ( x = true )
{
std::cout << "Do you have any questions before we continue?[y/n]" << std::endl;

std::cin >> lExt;

if ( lExt == 'y' )
{
std::cout << "What would you like to know more about?" << std::endl;
aboutarra();
break;
}
else if ( lExt == 'n' )
{
std::cout << "Okay, lets continue!" << std::endl;
break;
}

}
After you cin >> lExt, do a cin.ignore().

http://www.cplusplus.com/reference/iostream/istream/ignore/

Note the arguments that function can take.

Also, you're using the assignment operator in your while loop, use == instead of =. You could also just write while (x). On the other hand, you never update x, so you could just write while (true).
Last edited on
Use != operator like this.

if(IExt != 'Y' && IExt != 'N')
{
//Error Message like "Invalid input"
//Go back
}

Another tip. Split this up in functions.

Good luck!
Hi sunnychips, welcome to cplusplus forum.

Use != operator like this.


1
2
3
4
5
6
7
8
9
10
if(IExt != 'Y' && IExt != 'N')
{
//Error Message like "Invalid input"
//Go back
}
if(IExt != 'Y' && IExt != 'N')
{
//Error Message like "Invalid input"
//Go back
}


I find this use of != to test for invalid input counter intuitive and error prone. The OP's code has some problems but i like the use of the boolean in the while expression. I prefer the OP's if statement rather than this != code.

The OP needs to update the value of x before the end of the loop. Also he doesn't collect any input after one of the questions.




Last edited on
Thank you so much! I totally forgot about the != operator, and after a while of playing around with it I got it to work! I know this is off topic, but do any of you know how to make a suitable "Press any key to continue..." function other than

system("PAUSE"); // I read that this is a very bad way of doing it.

or

cin.get(); // When I do it this way, i have to press a key in twice
cin.ignore(); // for the program to move on.

last question, I promise.
Thank you so much! I totally forgot about the != operator, and after a while of playing around with it I got it to work!


I am not sure whether you got it to work by using the != or not. I am saying that doing it that way isn't good.

Consider this function, which decides whether a char is an operator or not. You could have a similar function that decides if the input is good or not.

1
2
3
4
5
6
7
8
9
10
11
bool IsOperator(char symbol)
{
  switch (symbol)
  {
  case '+':
  case '-':
  case '*':
  case '/': return true;
  default:  return false;
  }
}


This switch statement relies on execution falling through the different cases. Here is another one that shows how switches normally work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

switch ( MenuOption )
        {

            case 'A':
                UseCalculator();
                break;

            case 'B':
                PlayGuessingGame();
                break;

            case 'C':
                EnterText();
                break;

            default:
            cout << "" << MenuOption << " Is not a choice." << endl;
            break;
        }



This shows how to carry out different functions for different options. I am just showing this because it is how switches normally work.

Your program needs something similar to IsOperator above. See if you can figure out how to change it and incorporate it into your code.
Last edited on
Okay, Ive got it to go through the switch statement, but my problem is the default option. when I type in say "234" it just quits the program. Ill have to work on it a bit.

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
30
void contstory()
{
	char lExt;
	
	std::cout << "Do you have any questions before we continue?[y/n]" << std::endl;

	std::cin >> lExt;

		switch (lExt)
		{
		case 'y':
			{
				std::cout << "What would you like to know more about?" << std::endl;
				aboutarra();
				break;
			}
		case 'n':
			{
				std::cout << "Okay, lets continue!" << std::endl;
					pressakey();
					break;
			}
		default:   //Work on this!!! 
			{
				std::cout << "I didnt understand you, come again?" << std::endl;
				pressakey();
				break;
			}
		}
}
Topic archived. No new replies allowed.