Beginner Help

How do I make this code to where if a user entered credits that are less than 0 and greater than 100 as Invalid? Then repeat and as the question How many credit hours do you have? I don't know how to make it repeat saying invalid until the user enters the right amount of credits then ask the question again.

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


int main()
{
	int credit;
	cout << "How many credit hours do you have?" << endl;
	cin >> credit;

	While (credit <= 0 || credit >= 100);
	cout << "Answer is Invalid, Try Again!" << endl;
	
	 if (credit < 32)
		cout << "You are a Freshman" << endl;

	else if (credit >= 32 && credit <= 63)
		cout << "You are a Sophomore" << endl;

	else if (credit >= 64 && credit <= 95)
		cout << "You are a Junior" << endl;

	else if (credit >= 95)
		cout << "You are a Senior" << endl;



    return 0;
}

Last edited on
You could use a do while loop. It would look something like this.
1
2
3
4
5
do
{
        cout << "How many credit hours do you have?" << endl;
	cin >> credit;
}while(credit > 0 && credit <100);
The 11th line makes no sense. The W in while should be lowercase, and you shouldn't have that ; at the end. Also you'll need another cin in the while block in order to query the user again, otherwise the cout << "Answer is Invalid, Try Again!" << endl; line would repeat forever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std; 
int main()
{
	int credit;
	cout << "How many credit hours do you have?" << endl;
	cin >> credit;

	while (credit <= 0 || credit >= 100) {
		cout << "Answer is Invalid, Try Again!" << endl;
		cin >> credit;
	}
	 if (credit < 32)
		cout << "You are a Freshman" << endl;
	else if (credit >= 32 && credit <= 63)
		cout << "You are a Sophomore" << endl;
	else if (credit >= 64 && credit <= 95)
		cout << "You are a Junior" << endl;
	else if (credit >= 95)
		cout << "You are a Senior" << endl;
    return 0;
}
Ok, thanks for the feedback I''ll be sure to adjust my code. Appreciate the help a lot.
Topic archived. No new replies allowed.