input validation

I have tried using while statements, if statements, do while statements etc. None are working. I want to be able to have the input be validated and if the input is found to be less than one or greater than 10 then it should come back and ask for a valid input, but no matter how I do it, unless I use the default case which exits the program, I can;t get it to work. I really want it to ask user again instead telling them to re-run the program. Here is what I have.

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
 #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int num;
	cout << "\n\n\n";
	cout << setw(5) << " " << "Enter a number between 1 and 10: ";
	cin >> num;

		//switch statements must be in brackets or break cannot be used
		switch (num) {
		case 1: cout << setw(5) << " " << "The Roman Number equivalent of  " << num << " is I\n";
			break;

		case 2:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is II\n";
			break;

		case 3:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is III\n";
			break;

		case 4:  cout << setw(5) << " " << "The Roman Number equivalent of" << num << " is IV\n";
			break;

		case 5:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is V\n";
			break;

		case 6:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VI\n";
			break;

		case 7:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VII\n";
			break;

		case 8:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VIII\n";
			break;

		case 9:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is IX\n";
			break;

		case 10:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is X\n";
			break;

		default: cout << setw(5) << " " << "ERROR! Number Out of Range! Run again\n";
			break;
			cout << endl;
		}

	return 0;
}
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int num;
	cout << "\n\n\n";
	cout << setw(5) << " " << "Enter a number between 1 and 10: ";
	cin >> num;
	while (num < 1 || num > 10) {
		cout << "Enter a number between 1 and 10: ";
		cin >> num;
	}

	//switch statements must be in brackets or break cannot be used
	switch (num) {
	case 1: cout << setw(5) << " " << "The Roman Number equivalent of  " << num << " is I\n";
		break;

	case 2:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is II\n";
		break;

	case 3:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is III\n";
		break;

	case 4:  cout << setw(5) << " " << "The Roman Number equivalent of" << num << " is IV\n";
		break;

	case 5:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is V\n";
		break;

	case 6:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VI\n";
		break;

	case 7:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VII\n";
		break;

	case 8:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is VIII\n";
		break;

	case 9:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is IX\n";
		break;

	case 10:  cout << setw(5) << " " << "The Roman Number equivalent of " << num << " is X\n";
		break;

	default: cout << setw(5) << " " << "ERROR! Number Out of Range! Run again\n";
		break;
		cout << endl;
	}

	return 0;
}
"None are working" doesn't help explain the problem. Please avoid this phrase and its cousins ;D

- Try to find part(s) you want repeated and enclose them in a while loop.
- Be sure to come up with a true condition and a way to make that condition false so that the loop stops.
- Alternately, use a while(true) loop (infinite loop) and come up with a way to break out in order to stop it.

Here is an example where the user can enter "-1" to stop:
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    string converter[] {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
    int low = 1;
    int high = 10;
    int num;
    
    while(true)
    {
        cout << setw(5) << " " << "Enter a number between " << low << " and " << high << ": ";
        cin >> num;
        cout << setw(5) << " ";
            
        if (low<=num && num<=high)
            cout << "The Roman Number equivalent of  " << num << " is " << converter[num] << '\n';
        else if (num == -1)
            break;
        else
            cout << "ERROR! Number Out of Range! Run again\n";
    }
    return 0;
}


- I've also tweaked your repeating section be array indices instead -- always try to reduce repetition with design changes ;D
- Arbitrary numbers that could repeat, like "1" and "10" for the ranges, should be stored in variables, hence the use of "low" and "high"
Thank you for this. I am still a beginner even though I have already taken two classes. My teacher of those classes left me wanting so another teacher is, out of the kindness of his heart, working with me in email so I am really trying hard to do these things right.
Last edited on
Topic archived. No new replies allowed.