Is there a way to limit how many characters a user can input?

So I'm using a book to help me learn c++ and I'm very new to it. The exercise it's telling me to do is to write a program that lets the user select from a list of options, and if the input is not one of the options, reprint the list.

The problem I am facing is that I don't want the user to be able to input multiple characters at once. Basically, I want to limit the user to once character. So if he types ACD, he gets an invalid response reply. Is there a way to limit how many characters he can input?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char answer = ' ';
    cout << "Hello, user. Welcome to your life. It's the morning." << endl;
    cout << '\n';
    while(answer != 'a' || answer != 'A' || answer != 'b' || answer != 'B' || answer != 'c' || answer != 'C' || answer != 'd' || answer != 'D')
    {
        cout << "Would you like to: " << endl;
        cout << "A. Eat some cereal." << endl;
        cout << "B. Get dressed." << endl;
        cout << "C. Go pee." << endl;
        cout << "D. Take a shower." << endl;
        cout << '\n';
        cout << '\n';
        cout << '\n';
        cout << "Please choose your answer: " << endl;
        cin >> answer;
        cout << '\n';
        if(answer == 'a' || answer == 'A')
        {
            cout << "Alright, let's go eat some cereal." << endl;
            break;
        }
        if(answer == 'b' || answer == 'B')
        {
            cout << "Alright, put some clothes on then. Pussy" << endl;
            break;
        }
        if(answer == 'c' || answer == 'C')
        {
            cout << "Alright, go pee then. Nobody's stopping you." << endl;
            break;
        }
        if(answer == 'd' || answer == 'D')
        {
            cout << "Good. You stink." << endl;
            break;
        }
        else
        {
            cout << "Your answer was incorrect. Please try again." << endl;
            cout << '\n';
        }
    }
}
Is there a way to limit how many characters he can input?

Not in standard C++.

Btw, the condition in your while loop is wrong.

The truth table for logical or looks like this:

 v1    v2   | result
--------------------
true  true  | true
true  false | true
false true  | true
false false | false


So, the only way while (answer != 'a' || answer != 'A') will be false
is when answer is equal to both 'a' and 'A', which isn't possible.

Last edited on
@cire

so should I just not worry about it for now since it's an exercise? Will I learn more about limiting characters later on?
so should I just not worry about it for now since it's an exercise?

I wouldn't.


Will I learn more about limiting characters later on?

Not in standard C++, but of course you're certainly welcome to delve into system specific methods or third party libraries which enable it.
@cire

Would this be any better:

 
while(answer != 'a' || answer != 'A', answer != 'b' || answer != 'B', answer != 'c' || answer != 'C', answer != 'd' || answer != 'D')


Or is this still wrong? How would you set it up?
This would look a bit better.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <ctype.h> // to use toupper(char)

using namespace std;

int main()
{
	char answer = ' ';
	cout << "Hello, user. Welcome to your life. It's the morning.\n\n";
	while (answer != 'A' || answer != 'B' || answer != 'C' || answer != 'D')
	{
		cout << "Would you like to:\n";
		cout << "A. Eat some cereal.\n";
		cout << "B. Get dressed.\n";
		cout << "C. Go pee.\n";
		cout << "D. Take a shower.\n\n\n";
		cout << "Please choose your answer:\n";
		cin >> answer;
		answer = toupper(answer);
		cout << endl;
		
		switch (answer)
		{
			case 'A':
				cout << "Alright, let's go eat some cereal." << endl;
				break;
			break;

			case 'B':
				cout << "Alright, put some clothes on then. Pussy" << endl;
				break;
			break;

			case 'C':
				cout << "Alright, go pee then. Nobody's stopping you." << endl;
				break;
			break;

			case 'D':
				cout << "Good. You stink." << endl;
				break;
			break;

			default: cout << "Your answer was incorrect. Please try again.\n\n";	;
		}
	}
}
@Mr Impact
this has helped me a lot.

Although, my book hasn't gone over switch statements yet so I don't want to use switch statements because that would be cheating.

I ended up making something like this:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <ctype.h> //I didn't know I needed a separate library to use tolower. Thanks!

using namespace std;

int main()
{
    char answer = ' ';
    cout << "Good morning. Welcome to the game of life. What will you do first?" << endl;
    cout << endl;
    do
    {
    cout << "Please select an option: " << endl;
    cout << '\n';
    cout << '\n';
    cout << "A.    Eat some breakfast." << endl;
    cout << "B.    Take a shower." << endl;
    cout << "C.    Take a piss." << endl;
    cout << "D.    Eat a banana!" << endl;
    cout << '\n';
    cout << '\n';
    cin >> answer;
    answer = tolower(answer);
    cout << '\n';
        if(answer == 'a')
        {
            cout << "Alright, let's go eat some breakfast!" << endl;
        }
        if(answer == 'b')
        {
            cout << "Alright, go take a shower. It's good to stay clean." << endl;
        }
        if(answer == 'c')
        {
            cout << "Hey man you do what you gotta do... " << endl;
        }
        if(answer == 'd')
        {
            cout << "Alright. Bananas are cool." << endl;
        }
        else
        {
            cout << "Sorry, none of your answer choices were valid. Please try again." << endl;
            cout << '\n';
            cout << '\n';
        }
    } while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd');

    return 0;




}



The only problem I'm having now is that for some reason, the else statement is executed no matter what


jk fixed it by replacing if with else if.
Last edited on
Hi,

I have a pathological hate for constructs like this:

} while (answer != 'a' && answer != 'b' && answer != 'c' && answer != 'd');

Instead, consider using a bool variable to control a while loop:

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
31
32
33
bool Quit = false;

while (!Quit) {
    // show menu with a quit option
   std::cin >> answer;
    answer = std::tolower(answer);
    std::cout << '\n';
        if(answer == 'a')
        {
            std::cout << "Alright, let's go eat some breakfast!\n";
        }
        else if(answer == 'b')
        {
            std::cout << "Alright, go take a shower. It's good to stay clean.\n";
        }
        else if(answer == 'c')
        {
            std::cout << "Hey man you do what you gotta do...\n ";
        }
        else if(answer == 'd')
        {
            std::cout << "Alright. Bananas are cool.\n";
        }

        else if (answer =='q')
        {
            Quit = true;
        }
        else
        {
            std::cout << "Sorry, none of your answer choices were valid. Please try again.\n\n";
        }
}
Topic archived. No new replies allowed.