Cannot convert Char to Int

Hello All,

This is my first real C++ program which I am building from a very simple example I found on the net. I've already run into a problem which i'm not sure i understand. Here is the code:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout <<"\tWelcome to my text based game!\n";
    cout <<"\nPlease enter your username: ";
	
	char name[256];
	cin.getline(name, 256);

	cout <<"Hello, "<< name <<"!\n\n";

    bool isPickRaceTrue = false;
	int pickRace;
	do {
		cout <<"Please pick your race: \n";
		cout <<"1 - Human\n";
		cout <<"2 - Orc\n";
		cout <<"Pick your race: ";
		cin >>pickRace;
	    
		switch (pickRace)
		{
			   case 1:
				    cout <<"You picked the Human race.\n";
					isPickRaceTrue = true;
					//cin.getline(" ");
					break;
			   case 2:
				    cout <<"You picked the Orc race.\n";
					isPickRaceTrue = true;
					//cin.getline(" ");
					break;
			   default:
                   cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
				   isPickRaceTrue = false;
				   system("CLS");
		}
	} while (!isPickRaceTrue);
	
	bool isLevelTrue = false;
	int difficulty = 0;
	do {
	cout <<"\nPick your level difficulty: \n";
    cout <<"1 - Easy\n";
    cout <<"2 - Medium\n";
    cout <<"3 - Hard\n";
	cout <<"\n";
    cout <<"Pick your level difficulty: ";
    cin >>difficulty;

	switch(difficulty)
        {
           case 1:
                cout <<"You picked Easy.\n\n";
				isLevelTrue = true;
				break;
           case 2:
                cout <<"You picked Medium.\n\n";
                isLevelTrue = true;
				break;
           case 3:
                cout <<"You picked Hard.\n\n";
                isLevelTrue = true;
				break;
           default:
                cout <<"Error - Invalid input; only 1,2 or 3 allowed.\n";
				isLevelTrue = false;  
				system("CLS");
				break;
		} 
	} while (!isLevelTrue);
	
	cout << "Well " << name << " it looks like you have a hearty adventure ahead! ";
	cout << "Being a " << pickRace << " isn't easy, especially as you have choosen ";
	cout << "the ";
		if (difficulty = "1") cout << " easy level to play the game, but good luck!";
		else if (difficulty = "2") cout << " medium level to play the game, but good luck!";
		else cout << " hard level to play the game, but good luck!";


    system("PAUSE");
    return 0;


Now the errors I'm getting refer to the lines near the bottom of the program.
1
2
3
if (difficulty = "1") cout << " easy level to play the game, but good luck!";
		else if (difficulty = "2") cout << " medium level to play the game, but good luck!";
		else cout << " hard level to play the game, but good luck!";


The errors are:

1>My First Project.cpp
1>c:\users\scott\documents\visual studio 2008\projects\my first project\my first project\my first project.cpp(83) : error C2440: '=' : cannot convert from 'const char [2]' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\scott\documents\visual studio 2008\projects\my first project\my first project\my first project.cpp(84) : error C2440: '=' : cannot convert from 'const char [2]' to 'int'
1> There is no context in which this conversion is possible
1>Build log was saved at "file://c:\Users\Scott\Documents\Visual Studio 2008\Projects\My First Project\My First Project\Debug\BuildLog.htm"
1>My First Project - 2 error(s), 0 warning(s)

Can anybody help??

Many thanks in Advance!

should be
if (difficulty == 1)

one = sign is assignment.
you're comparing an integer to an array (which you can't even do directly, it must be done element by element), you you just want to test if it's equal to one, not a character array starting with '1'.
Last edited on
Oh, you need to change line 78 because 'pickRace' is not a race it is a number. So for example, the sentence might come out:

"Well mcleano it looks like you have a hearty adventure ahead! Being a 1 isn't easy..."
Thanks for that, I knew it was something simple but it was just eluding me! Yes it was coming out as a 1 or 2 but i needed to sort this problem out first as it is the same thing! Many thanks to you both!
Topic archived. No new replies allowed.