My function is not working properly, please help

I'm currently in a predicament where i'm trying to get the program to test if the user picked fire, water, and nature, and if its not any of those, have it go through the fourth if statement and have it loop again. I'm trying to do this without gotos, and i think i messed up with my while statement, so if anyone can please fix my problem that would be nice, (it never hits the first 3) thank you.

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
void YourType(Monster & monster)
{
           string CharacterDetails;
           cout << "What character do you want?\n\n";
           cout << "There is a fire type who specializes with fire attacks. (fire)\n";
           cout << "There is a water type who specializes with water attacks. (water)\n";
           cout << "There is a nature type who specializes with nature attacks. (nature)\n\n";
           cout << "Please enter the type of the monster you would like:";
           cin >> CharacterDetails;
     while (CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
     {    
           if(CharacterDetails == "fire")
           {
           cout << "Your Monster Stats" << endl;
           YourFireType(monster);
           }
           if(CharacterDetails == "water")
           {
           cout << "Your Monster Stats" << endl;
           YourWaterType(monster);
           }
           if(CharacterDetails == "nature")
           {
           cout << "Your Monster Stats" << endl;
           YourNatureType(monster);
           }
           if(CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
           {
           cout << "Invalid Choice, Choose Again" << endl;
           cin >> CharacterDetails;
           }
     }
     CharacterDetails.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Last edited on
while (CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")

So the loop will only be entered if CharacterDetails is NOT fire, and it is NOT water, and it is NOT nature.

So if we're inside the loop, we know for sure that it isn't any of those three. From here, we suppose that we are inside the loop, and we know that to get inside the loop, CharacterDetails is NOT fire, and it is NOT water, and it is NOT nature.

So here we are inside the loop, and we know absolutely it is NOT fire, so when is this ever coming to be true?
if(CharacterDetails == "fire")

So we know absolutely it is NOT water, so when is this ever coming to be true?
if(CharacterDetails == "water")

So here we are inside the loop, and we know absolutely it is NOT nature, so when is this ever coming to be true?
if(CharacterDetails == "nature")

So here we are inside the loop, and we know absolutely it is NOT fire and it is NOT water and it is NOT nature, so this will ALWAYS be true.
if(CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
Last edited on
i figured that out and got into the prediciment of how to get the 4th statement running, if i fix the first, i know it'd be an easy solution to use gotos, but i'm trying to not use gotos, yet get back up there to get the first 3 to work
Last edited on
try
while (CharacterDetails == "fire" || CharacterDetails == "water" || CharacterDetails == "nature")

instead off
while (CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
Last edited on
Move all that stuff about what to do if the variable Is fire, or IS water, or IS nature, OUT of the while loop. It has no business being in there.

Your while loop is for forcing the variable to one of those three states. When it is one of those three sates, the while loop should end.

1
2
3
4
5
6
7
cout << "Please enter the type of the monster you would like:";
cin >> CharacterDetails;
while (CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
{
  cout << "Invalid Choice, Choose Again" << endl;
  cin >> CharacterDetails;
}
yes that would fail because it would miss the fourth statement Alx101, and i found the soluting to the problem a few seconds ago (had to learn something ive never had to do) a do-while loop

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
void YourType(Monster & monster)
{
           string CharacterDetails;
           cout << "What character do you want?\n\n";
           cout << "There is a fire type who specializes with fire attacks. (fire)\n";
           cout << "There is a water type who specializes with water attacks. (water)\n";
           cout << "There is a nature type who specializes with nature attacks. (nature)\n\n";
           cout << "Please enter the type of the monster you would like:";
     do{ 
           cin >> CharacterDetails;   
           if(CharacterDetails == "fire")
           {
           cout << "Your Monster Stats" << endl;
           YourFireType(monster);
           }
           if(CharacterDetails == "water")
           {
           cout << "Your Monster Stats" << endl;
           YourWaterType(monster);
           }
           if(CharacterDetails == "nature")
           {
           cout << "Your Monster Stats" << endl;
           YourNatureType(monster);
           }
           if(CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature")
           {
           cout << "Invalid Choice, Choose Again" << endl;
           }
     }while (CharacterDetails != "fire" && CharacterDetails != "water" && CharacterDetails != "nature");
     
     CharacterDetails.clear();
     cin.ignore(numeric_limits <streamsize>::max(), '\n' );
}
Last edited on
Topic archived. No new replies allowed.