If/Then Statements with char

I am still new to programming with C++ and I'm trying out some If/Then statements. Basically I'm trying to do something where someone enters one of the spells from the different char and output different messages depending on the spell type you enter. For some reason though, I'm still missing something. Any input would be appreciated.

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

int main() {
     
     char fire;
     char ice;
     char thunder;
     char wind;
     char poison;
     char rock;

    cout << "\nCast a spell: ";
    
    cin >> fire;
    cin >> ice;
    cin >> thunder;
    cin >> wind;
    cin >> poison;
    cin >> rock;
    
    if char = fire {
        cout << "\nYou burned your enemy!!!: " << endl;
        }
    
    if char = ice {
        cout << "\nYou freezed your enemy!!!: " << endl;
    }
    
    if char = thunder {
        cout << "\nYou shocked your enemy!!!: " << endl;
    }
    
    if char = wind {
        cout << "\nYou blasted your enemy!!!: " << endl;
    }
    
    if char = poison {
        cout << "\nYou poisoned your enemy!!!: " << endl;
        
    if char = rock {
        cout << "\nYou smashed your enemy!!!: " << endl;
    }
    
    else {
        
    return 0;
What are you trying to enter into your variables?

Do you realize that "char" is a type of variable not a variable, you can't compare a type to a variable.

Do you realize that a char holds a single character?

= is the assignment operator.

== is the equality check operator.
try it like this:
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;


int main() 
{     
    string input;
    cout << "\nCast a spell: ";    
    cin >> input;
    
    if input == "fire" 
	 {
       cout << "\nYou burned your enemy!!!: " << endl;
     }
    else if input == "ice" //this is just style, else can be on its own line or with the next one. there is no 'elseif' single statement
	 {
       cout << "\nYou froze your enemy!!!: " << endl;//freezed is not correct english. 
     }  
    //...etc
	else cout <<"unknown attack, valid attacks are fire, ice, ... rock\n"
    	
    return 0;
}

@Jonnin

I tried your code but I got some errors in it:

C:\WINDOWS\system32\cmd.exe /C ""C:/Program Files/mingw-w64/mingw64/bin/mingw32-make.exe" -j4 SHELL=cmd.exe -e -f Makefile"
"----------Building project:[ IFSTATE - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/CPP/IFSTATE/IFSTATE'
"C:/Program Files/mingw-w64/mingw64/bin/g++.exe" -c "C:/CPP/IFSTATE/IFSTATE/main.cpp" -g -O0 -std=c++17 -Wall -o Debug/main.cpp.o -I. -I.
C:/CPP/IFSTATE/IFSTATE/main.cpp: In function 'int main()':
C:/CPP/IFSTATE/IFSTATE/main.cpp:13:14: error: could not convert 'input' from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'bool'
if (input) == "fire"
^
C:/CPP/IFSTATE/IFSTATE/main.cpp:13:16: error: expected primary-expression before '==' token
if (input) == "fire"
^~
C:/CPP/IFSTATE/IFSTATE/main.cpp:17:19: error: could not convert 'input' from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'bool'
else if (input) == "ice" //this is just style, else can be on its own line or with the next one. there is no 'elseif' single statement
^
C:/CPP/IFSTATE/IFSTATE/main.cpp:17:21: error: expected primary-expression before '==' token
else if (input) == "ice" //this is just style, else can be on its own line or with the next one. there is no 'elseif' single statement
^~
C:/CPP/IFSTATE/IFSTATE/main.cpp:22:71: error: expected ';' before 'return'
else cout <<"unknown attack, valid attacks are fire, ice, ... rock\n"
^
;
C:/CPP/IFSTATE/IFSTATE/main.cpp:24:5:
return 0;
~~~~~~
mingw32-make.exe[1]: *** [IFSTATE.mk:98: Debug/main.cpp.o] Error 1
mingw32-make.exe: *** [Makefile:5: All] Error 2
mingw32-make.exe[1]: Leaving directory 'C:/CPP/IFSTATE/IFSTATE'
====6 errors, 0 warnings====
You need to look again at your textbook, to learn the correct syntax for an "if" statement.

Hint: look carefully at where the parentheses go.
it was not meant to run. I clearly left off () on the if statement, have ... and etc all through it..
its how to do it, not done for you -- consider it pseudo code that is much like c++. I missed some ;s as well.

if statements require ():
if(input == "fire") is correct, if you did not know that.
if input == "fire" is not correct.

Give it a try with these ideas but your own code. If you can't get it, I will help explain whatever didn't work if you post your try.
Last edited on
Topic archived. No new replies allowed.