Help with loops

Ok, total newb here --- loops are driving me crazy. I know the solution is probably very simple to my problem. Honestly, I'm trying to wrap my head around loops and having a difficult time --- it seemed so easy reading about them in the book and then I go to implement and... well... maybe I'm just getting old :)

Anyways, my code waits for your response to enter 1-4 as it should. Then, for some reason, even though you've typed "asdsad" (or whatever) it interprets that as "0" and goes into infinite loop for "Please enter a number for you character class selection". I'm thinking it should return to the top of the loop based on the continue command but cin>>response; apparently gets ignored after that.

#include <iostream>
#include <string>
using namespace std;
void MakeCharacter();

int main(){

MakeCharacter();
return 0;
}

void MakeCharacter(){
int response = 0;
cout<<"Which class do you want to be:"<<endl<<"1. Warrior"<<endl
<<"2. Thief"<<endl<<"3. Wizard"<<endl<<"4. Priest"<<endl<<endl<<"Selection: ";
while ((response <1)||(response>4)){
cin>>response;

if (response==0){
cout<<"Please enter a number for you character class selection";
continue;
}
if ((response<1)||(response>4)){
cout<<response<<" - is not a valid selection"<<endl;
continue;
}
}
}
Don't take my word as law. I barely know what I'm talking about.

I'm fairly certain the problem is that
cin>>response;
needs to go before
while((response<1||(response>4

The way you have it written, when the program reaches the 'while' line, the response variable hasn't been read from the command line yet, so the while loop is using the 0 value that you gave it when you declared "int response" to test for ((response<1)||(response>4)).

Edits begin here
Now that I've looked at it again, I understand more completely now what's wrong. You intended my above statement to be the case so that your program would assume a response equaling 0, which you also intended, to print the "please enter..."statement. I apologize for my hasty response.

Your while loop only takes into account that the response variable is not valid. Take a look at your while statement.
while((response<1||(response>4))
This means that the while loop will only get entered if the response variable is invalid. This works initially, because you intend for the response variable to be 0, however the next time when you assume the response variable IS valid, the while loop won't execute because it can only execute if response is invalid.
Wham bam thank you ma'am.
Last edited on
even though you've typed "asdsad" (or whatever) it interprets that as "0"
Not exactly. The program wants an integer, but instead receives alphabetic characters. That means the variable 'response' retains its previous value, it is unchanged.

More importantly, it causes the error flag to be set for the cin stream.

You need to clear the error condition for non-integer input. Also clear the invalid characters from the input buffer.
1
2
3
4
5
if (!cin)
{
    cin.clear();              // clear the error flags
    cin.ignore(1000,'\n');    // empty the input buffer
}
Last edited on
Hi BillyWilliam,

Your assessment of the loop was correct but that's what I was going for. Basicaly, I wanted a loop where if all entries were invalid, the loop would repeat and keep asking for the response.

I was going to code some actual work for entries 1-4 after the loop. The problem is that I can't even loop the invalid entries correctly --- it goes into infinite loop when I make an invalid entry of letters instead of numbers
Chervil,

Thanks for the input but I guess I am unsure of how to use your code. I tried to plug it in but I'm not sure where it goes --- mainly because I am a little baffled by what the opening if statement means ---- "if (!cin)" ---- I can't figure out where to put it. I have tried a few different places but it doesn't seem to work for me.

You comment that the variable was unchanged from '0' based on alphabetic input gave me the idea to change it to '5' --- that did work for getting it to pass up the initial response==0 but then it gets stuck after it says "5 - is not a valid selection".

With your code inserted in the if statement "as shown below" the loop seems 'broken'. Without your code, I go into infinite loop with "5 - is not a valid selection"

Am I putting the code you suggested in the right place or am I doing it totally wrong?

#include <iostream>
#include <string>
using namespace std;
void MakeCharacter();

int main(){

MakeCharacter();
return 0;
}

void MakeCharacter(){
int response = 5;
cout<<"Which class do you want to be:"<<endl<<"1. Warrior"<<endl
<<"2. Thief"<<endl<<"3. Wizard"<<endl<<"4. Priest"<<endl<<endl<<"Selection: ";
while ((response <1)||(response>4))
{
cin>>response;

if (response==0)
{
cout<<"Please enter a number for you character class selection";

continue;
}
if ((response<1)||(response>4)){
cout<<response<<" - is not a valid selection"<<endl;
if (!cin)

cin.clear();
cin.ignore(1000,'/n');
continue;
}
}
}
Ok, I pasted that wrong (I didn't have the brackets in there --- I've been messing with the code quite a bit). My code is at the bottom and I can't figure out how to attach a picture but here is a recreation of the console output (I am inputing a "d" and hitting enter):

Please enter a number for you character class selection:
1. Warrior
2. Thief
3. Wizard
4. Priest

Selection: d
d
d
d


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
#include <iostream>
#include <string>
using namespace std;
void MakeCharacter();

int main(){

	MakeCharacter();
	return 0;
}

void MakeCharacter(){
	int response = 5;
	cout<<"Which class do you want to be:"<<endl<<"1. Warrior"<<endl
			<<"2. Thief"<<endl<<"3. Wizard"<<endl<<"4. Priest"<<endl<<endl<<"Selection: ";
	while ((response <1)||(response>4))
	{
		cin>>response;
	 
	if (response==0)
	{
		cout<<"Please enter a number for you character class selection";

	continue;
	}
	if ((response<1)||(response>4)){
		cout<<response<<" - is not a valid selection"<<endl;
		if (!cin)
		{
			cin.clear();
			cin.ignore(1000,'/n');
			continue;
		}
	}
	}
}

Please use code tags - see the <> button in the formatting options on the right. [code]your code here[/code]

After this statement cin>>response;, there are two main conditions to check.
1. The input was non-numeric.
2. The input was a number, but outside the permitted range.

You can test (immediately after the cin statement) whether or not it was numeric like this:
1
2
3
4
5
6
7
8
9
        cin>>response;
        
        if (!cin) 
        {
            cin.clear();
            cin.ignore(1000,'/n');
            cout<<"Please enter a number for you character class selection";
            continue;
        }


After that, the value entered must have been numeric, so you can proceed with the other check, to see whether it is in the required range 1 to 4.

I don't see any particular reason to test whether response == 0, as that is just one of the many out-of-range values.
Last edited on
Topic archived. No new replies allowed.