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:
#include "stdafx.h"
#include <iostream>
usingnamespace 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!";
elseif (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!";
elseif (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)
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'.
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!