Input Validation Question

Hi everyone! I'm working on a program and have mostly all the input validation correct, the only problem I'm having is when you input 'ye' or 'yes' when asking for another number it outputs:

"Good evening sir or madam!"
"Fancy a number to input? Only whole numbers, no decimals by golly!"
"By George you are cheeky! Try again."
"Fancy a number to input?"

instead of just:

"Bits and bobs! Lets try that again."
"Fancy more digits chap?\nEnter Y for yes or N for No."

I'm hoping a few brackets will fix the issue, but I'm stuck at this point. Any help would be greatly appreciated. We haven't learned much outside if-else, switch, and do-while statements so I'm trying to keep it pretty basic. Thanks for you time.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

using namespace std;

int main()
{
    // Variables
    int number;
    int positive;
    int negative;
    int odd;
    int even;
    char again;

    // Lets us ask user if they want to input another number
    do {

    // Asks users to input a number
    cout << "Good evening sir or madam!\nFancy a number to input? Only whole numbers, no decimals by golly!" << endl;
    cin >> number;

    // Input validation, lets them try again
    if (!cin){
        cout << "By George you are cheeky! Try again." << endl;
        cin.clear();
        cin.ignore(100, '\n');

        cout << "Fancy a number to input?" << endl;
        cin >> number;

        // If it's not a number again this super British program terminates
        if (!cin){
                cout << "Oh bother! Pip pip, cheerio!" << endl;
        return 1;
        }
    }
    // If-else statements that differentiate negative/positive and even/odd numbers
    if ((number < 0) && (number % 2 == 0)){
        cout << "How lush! Your number is negative and even." << endl;
    }
    else if ((number > 0) && (number % 2 == 0)){
        cout << "Positively smashing! Your number is positive and even." << endl;
    }
    else if (number == 0){
        cout << "That's quite lovely. Your number is positive and even." << endl;
    }
    else if ((number < 0) && (number % 2 != 0)){
        cout << "Ohh very peng.. Your number is negative and odd." << endl;
    }
    else if ((number > 0) && (number % 2 != 0)){
        cout << "Now that's tidy. Your number is positive and odd." << endl;
    }
        // Asks user if they want to enter another number
        cout << "Fancy more digits chap?\nEnter Y for yes or N for No." << endl;
        cin >> again;

        // More input validation, Gives them one more chance
        if (again != 'Y' && again != 'y' && again != 'N' && again != 'n'){
            cout << "Bits and Bobs! lets try that again." << endl;
            cin.clear();
            cin.ignore(100, '\n');

            cout << "Fancy more digits chap?\nEnter Y for yes or N for No." << endl;
            cin >> again;

        // Gives up asking them
        if (again != 'Y' && again != 'y' && again != 'N' && again != 'n'){
            cout << "Wee bit thick today mate? Toodaloo!" << endl;
            return 1;
        }

        }



    } while (again !='N' && again !='n');

}
Last edited on
when you enter any character besides y or n(lower or upper) it will print the bits and bobs if statement,I entered h and that part of the code ran,

the reason it runs again is because you entered 'Y' or 'y' so it will loop again.
How can I cut out the extra two lines of output to clean it up a little? Either the first two lines or second two lines of:

"Good evening sir or madam!"
"Fancy a number to input? Only whole numbers, no decimals by golly!"
"By George you are cheeky! Try again."
"Fancy a number to input?"
Hello ccarmines,

In the following code read the comments I put in.

For the header files I added "cctype". The comment will tell you what it is for. You should also include the header file "limits". More on that one in a bit.

Inside "main" you have four variables that are not used.

Not always needed, but a good idea to initialize your variables. The empty {}s initialize the "int" to zero and the "char" to "\0".

Inside the do/while loop I added some "\n"s to allow the output to look more presentable.

On prompts I generally end the text with a colon and a space and leave off the "endl" so the "cin" will follow on the same line.

Now the "cin.ignore()" is more commonly written as std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.

The"return 1;"s, The right way to use it, but the wrong time to use it. There is a good chance the the console window will close before you can read the line that just printed. I would consider setting "again" equal to "N" and use the key word "continue;" to bypass everything left and go to the while condition of the do/while loop. Just thought of that one and have not tested it yet.

The if/else if statements are all one liners therefor the {}s are not needed unless you planed on adding something more later.

After entering some form of "y" or "n" is where the problem is. The if condition is trying to check for to much. Choose a letter either "y" or "n", but not both. You will see I chose "n" based on what is there. The next if statement has the same problem.

Unless you are using this as a learning tool the last two if statements are not needed. They really do not make any sense.

The last thing is in the while condition. When you look at the code you will see what I did to shorten it.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cctype>  // <--- Added for std::tolower() and std::toupper().
#include <limits>

using namespace std;  // <--- Best not to use.

int main()
{
	// Variables
	int number{};
	//int positive;  // <--- Not used.
	//int negative;  // <--- Not used.
	//int odd;  // <--- Not used.
	//int even;  // <--- Not used.
	char again{};

	// Lets us ask user if they want to input another number
	do
	{

		// Asks users to input a number
		cout << "\nGood evening sir or madam!\nFancy a number to input?\nOnly whole numbers, no decimals by golly!: ";
		cin >> number;

		// Input validation, lets them try again
		if (!cin)
		{
			cout << "\nBy George you are cheeky! Try again." << endl;
			cin.clear();
			//cin.ignore(100, '\n');
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

			cout << "\nFancy a number to input?: ";
			cin >> number;

			// If it's not a number again this super British program terminates
			if (!cin)
			{
				cout << "\nOh bother! Pip pip, cheerio!" << endl;
				return 1;
			}
		}

		// If-else statements that differentiate negative/positive and even/odd numbers
		if ((number < 0) && (number % 2 == 0))
			cout << "\nHow lush! Your number is negative and even." << endl;
		else if ((number > 0) && (number % 2 == 0))
			cout << "\nPositively smashing! Your number is positive and even." << endl;
		else if (number == 0) 
			cout << "\nThat's quite lovely. Your number is positive and even." << endl;
		else if ((number < 0) && (number % 2 != 0))
			cout << "\nOhh very peng.. Your number is negative and odd." << endl;
		else if ((number > 0) && (number % 2 != 0))
			cout << "\nNow that's tidy. Your number is positive and odd." << endl;

		// Asks user if they want to enter another number
		cout << "\nFancy more digits chap?\nEnter Y for yes or N for No.: ";
		cin >> again;

		// More input validation, Gives them one more chance
		if (again != 'N' && again != 'n')
		{
			cout << "\nBits and Bobs! lets try that again." << endl;
			cin.clear();
			cin.ignore(100, '\n');

			cout << "\nFancy more digits chap?\nEnter Y for yes or N for No." << endl;
			cin >> again;

			// Gives up asking them
			if (again != 'Y' && again != 'y')
			{
				cout << "\nWee bit thick today mate? Toodaloo!" << endl;
				//return 1;  // <--- Should be zero, but not needed in the first place.
			}
		}
	} while (std::toupper(again) != 'N');

	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

Hope that helps,

Andy
Thanks Andy.
Topic archived. No new replies allowed.