Trying to compare strings using If Else

Jun 29, 2016 at 1:05pm
Hi, I am a novice trying to learn more about c++. I really like text RPGS and I want to try and make one. I am trying to create a section in the game where it asks for your name and your job class. I am trying to get it to where a message is printed out depending on the class you chose and if you did not choose any of the available classes then it just keeps looping until you do. I been reading about if-else statements and strings but I am having an issue with the syntax. I keep getting the error "In function 'int main()':
22:4: error: expected ';' before 'else'" I been messing around with the semi colon but have not been successful.

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

int main ()
{
    string name;
    string job;
    string knight;
    string warrior;
    string thief;
    
  cout <<"Hello Adventure what is your name?" ;
  getline (cin, name) ;
  cout <<"Welcome," << name << "Are you a warrior, a thief, or a knight?;
  getline (cin, job);
  
  if (job == warrior)
  {
   cout <<"Ah so you are the mighty Warrior."
   else if (job==knight)
   cout <<"Not bad, the brave Knight."
   else if (job==thief)
   cout <<"Oh no a sneaky thief I see!"
   else (job !==knight or !==warrior or !==thief)
   cout <<"Please choose Knight, Warrior, or Thief to select your class";
  }
   
  
} 
Last edited on Jun 29, 2016 at 1:06pm
Jun 29, 2016 at 1:36pm
sting knight IS NOT "knight". It just reserves a space in memory and to access that space you use the identifier knight. The data in the variable knight isn't "knight".
I don't know if I'm making any sense. I have edited it now it works

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

int main ()
{
    string name;
    string job;
    
  cout <<"Hello Adventure what is your name?" ;
  getline (cin, name) ;
  cout <<"Welcome," << name << "Are you a warrior, a thief, or a knight?";
  
  
  while(1)  {
      getline (cin, job);
  if (job == "warrior") {
    cout <<"Ah so you are the mighty Warrior."; break; } //break exits the while loop
   else if (job=="knight")  {
    cout <<"Not bad, the brave Knight."; break; }
   else if (job=="thief")   {
    cout <<"Oh no a sneaky thief I see!"; break; }
   else 
    cout <<"Please choose Knight, Warrior, or Thief to select your class"<<endl;
  
  }
   
  
} 


Also, look at the if-else statement syntax more.
Last edited on Jun 29, 2016 at 3:04pm
Jun 29, 2016 at 1:43pm
Line 9-11: These are uninitialized strings.

Line 13: You're missing a close quote.

Line 18,21,23: You're comparing to uninitialized strings. Either give lines 9-11 values, or compare to quoted strings.

Line 25: Several problems with your else
1) C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

2) You want and not or here.

3) else does not take a condition clause. You could have said else if, but the condition is really not required since you've already determined job is not a warrior and not a thief and not a knight.

Lines 19-27: This is a single block which is executed only if job is "warrior". Line 21-26 are misplaced. They should be moved to after line 27.

Lines 20,22,24: You're missing the ; at the end of the statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main ()
{   string name;
    string job;
    
    cout <<"Hello Adventure what is your name?" ;
    getline (cin, name) ;
    cout <<"Welcome," << name << "Are you a warrior, a thief, or a knight?";
    getline (cin, job);
  
    if (job == "warrior")
    {   cout <<"Ah so you are the mighty Warrior.";
    }
    else if (job=="knight")
        cout <<"Not bad, the brave Knight.";
    else if (job=="thief")
        cout <<"Oh no a sneaky thief I see!";
    else 
        cout <<"Please choose Knight, Warrior, or Thief to select your class"; 
}

Last edited on Jun 29, 2016 at 1:47pm
Jun 29, 2016 at 1:45pm
Hey Brown ogum, thanks for your reply. I don't understand it entirely just yet but I have only took a glance at it now. I am going to look into it more. Just wanted to reply back and let you know I appreciate your time. What is the (1) to the right of the while loop?
Jun 29, 2016 at 1:50pm
while (1) is the same as while (true). In other words, it loops forever.
Jun 29, 2016 at 2:11pm
closed account (E0p9LyTq)
@gazalle2016,

AbstractionAnon mentioned the main problems you were having with your code. There is another subtle logic error lurking in the code. Comparing multi-character strings, especially when comparing strings users input, ignores the characters' case (w vs. W).

Each of the strings you are comparing to has the first character differ, so I would only compare the first character:
if (job[0] == 'w' || job[0] == 'W')

When comparing a single character I would just "force" the character to either upper or lower case so only a single comparison is needed:
if (toupper(job[0]) == 'W')
http://www.cplusplus.com/reference/cctype/toupper/
http://www.cplusplus.com/reference/cctype/tolower/

Don't forget to include the <cctype> header if you use either function! :)
Jun 29, 2016 at 3:16pm
@gazalle2016

You're welcome. while (1) or while(true) is a loop without condition.

conditions are statements like a > b. this condition is replaced with false/0 or true/(non-zero integer) depending on the values of a and b.
e.g a=5; b=6; if(a>b) {....}
is actually if(false) OR if( 0 ) false =0 because a is not greater than b.


so... while(1) is an infinite loop that is why I had to put "break" statement in the while loop if the user entered a correct word
Jun 29, 2016 at 3:28pm
Hello everyone, thank you for your responses. I have been reading through all of them and I do understand a lot more about my issue. I'm sorry I haven't been able to reply back individually. I'm actually at work atm and provide IT support, so I been doing this intermittently. Just wanted to reply back and let everyone know I appreciate their feedback. When the text rpg is complete I'll post it to the forums! Thank you all again for such detailed help. I really really appreciate it.
Last edited on Jun 30, 2016 at 12:02am
Topic archived. No new replies allowed.