Need to make a function for user input validity

I have a menu driven program where the user can enter the number 0, 1, 2, 3, 4, 5, 6, and -1 that corresponds to functions to do things with a random number that is generated. I used a while loop with a switch function for their choice and each case in the main program. If the user enters a NUMBER that is not one of those 8 numbers (for instance 7 or 11) my program prints "Invalid Choice!" and outputs the menu options again. However, if the user enters a character or string it only works once and gives the menu options again, but if a char or string is entered again then the program immediately terminates . Where else should I put this validInput function in the program to make it consistently work every time and not just on the first try? This is the function I have:

1
2
3
4
5
6
7
8
9
bool validInput()
{
    if (!cin)
    {
        cout << "Invalid Input!\n\n";
        cleanup();
    }
    return true;
}


And here is where I've implemented the function in my main function of code:
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

int randomNumGenerator(int);
void menuOptions();

long int funcOne(long int, long int);
long int funcTwo(long int, long int);
long double funcThree(long double, int, long int);
long int funcFour(long int, long int);
long double funcFive(string, long double, long int);
long double funcSix(string, long double, long int);

long int addTen(long int, long int, int);
void isPrime(long int, int);

bool validInput();
void cleanup();

int main()
{
    int randNum, choice, choice2, divisor;
    long int currentNum, add, doubledNum;
    long double reversedNum = 0;
    long double poweredNum;
    long int sumOfNum = 0;
    string firstToSecond, secondToThird;
    long double twoDigitPowered, threeDigitPowered;

START:
    cout << fixed << showpoint << setprecision(2) << endl;
    cout << "The following random number is generated: ";
    
    currentNum = randomNumGenerator(randNum);
    cout << currentNum;
    cout << "\n\n";
    
    isPrime(currentNum, divisor);
    menuOptions();
    cin >> choice;
    cout << "\n";
    if (validInput())
    {
        menuOptions();
        cin >> choice;
        cout << "\n";
    }
    
   while(choice != 0)
{
    switch(choice)
    {
        case 1:
            currentNum = funcOne(doubledNum, currentNum);
            cout << "Doubled = " << currentNum
                 << "\n\n";
            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
            break;
        case 2:
            currentNum = funcTwo(reversedNum, currentNum);
            cout << "Reversed Number = " << currentNum
                 << "\n\n";
            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
            break;
        case 3:
            cout << "Raise to the power of 2, 3, or 4?: ";
            cin >> choice2;
            cout << "\n";
            if (choice2 == 2 || choice2 == 3 || choice2 == 4) //prevents bugging
            currentNum = funcThree(poweredNum, choice2, currentNum);
            cout << currentNum
                 << "\n\n";
            
            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
                break;
        case 4:
            
            currentNum = funcFour(sumOfNum, currentNum);
            cout << "Sum of the digits of the number = "
                 << currentNum << "\n\n";
            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
            break;
        case 5:
            
            currentNum = funcFive(firstToSecond, twoDigitPowered, currentNum);
            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
            break;
        case 6:
            
            currentNum = funcSix(secondToThird, threeDigitPowered, currentNum);

            
            currentNum = addTen(currentNum, add, divisor);
            isPrime(currentNum, divisor);
            break;
        case -1:
            goto START;
            break;
        default:
            cout << "Invalid Choice!\n\n" << "Current Number: "
                 << currentNum << "\n";
            break;
    }
    if (validInput())
    {
        menuOptions();
        cin >> choice;
        cout << "\n";
    }
    cleanup();
    menuOptions();
    cin >> choice;
    cout << "\n";
}
    
    return 0;
}


Under the while loop it does not work. Where should this function be put? And is there a more efficient way to improve this function?
Last edited on
Your validate input function seems to be returning true every time, try having the function return something different if the input wasn't valid. Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool validInput()
{
    bool result = true;
    if (!cin)
    {
        result = false;
        cout << "Invalid Input!\n\n";
        cleanup();
    }
    return result;
}




Last edited on
Thanks for responding, I tried implementing your function into the main function in the same places I had mine (Line 48 for example) and when I execute the program and type a character or string it says "Invalid Input!" but then just exits the program. I need a way to keep the program going and current number the same if a character or string is typed by the user. Any hints?
Last edited on
You are having a scope issue. I looks to me that your validInput function can't see what is in main since you don't pass in any parameters. I think it will be much easier to have a function that gets and validates the input, and doesn't send anything back to main until it has been validated through a loop of some kind(a do-while loop is great for this).

Here is a skeleton of what I would do for input validation. If you input a string or character, you need account for that condition specifically and properly clean the input buffer. This I suspect, is why your program was acting strange earlier. I don't know what is in your "Cleanup" function but I imagine some of that code will go in here. As far as keeping the program going if the user inputs a string or character is a strange requirement that could be done, I just don't see a reason to, unless it is required in the assignment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int validInput()
{
	int choice;
	bool valid = true;

	do
	{

		cout << "Enter choice ";
		cin >> choice
		/*
		Here you do the validation with if statements 
		and set valid to false if the input is invalid so you don't exit the loop with bad input.
		ie check out of range or check if the input was a string or character 
                This is blank b/c you need to figure out what your invalid conditions will be. 
		*/


	}while(!valid)
	return choice;
}


Topic archived. No new replies allowed.