PDA

View Full Version : I have a problem with a C++ Char declaration...


xFear of Napalm
07-10-2010, 06:32 AM
I'm currently programming a simple console program for Rubik's
Cube scrambles in Dev-C++ 4.9.9.2, and I've declared "move" as
a char as such:

char move;

When i try to assign a value later in the program in an if statement,
I get a compiler error saying that I cannot convert a constant char
into a char! Why is this happening?

I'm extraordinarily new to C++ and it's syntax, so I'm sorry if I'm over-
looking something big here...

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int main()
{
int trnface;
char move;
bool dblturn;
bool inverse;
unsigned short int movecount;
string scramble = "";
int a;

cout << "I will create a Rubik's Cube scramble for you.\nHow many moves do you desire?\n" ;

cin >> movecount;

srand((unsigned)time(0));

for(a = 1; a <= movecount; ++a)
{
trnface = (rand() % 6 + 1);
cout << trnface;

if (trnface = 1)
{move = "F";}
if (trnface = 2)
{move = "B";}
if (trnface = 3)
{move = "L";}
if (trnface = 4)
{move = "R";}
if (trnface = 5)
{move = "U";}
if (trnface = 6)
{move = "D";}

cout << move;


}

system("PAUSE");

}

Here's all the code that I have written so far, and I've created similar
programs in VB...

oracleguy
07-10-2010, 07:11 AM
Double quotes are strings, not characters. And a string literal is a pointer which is why it won't let you do it. If you want to assign a single character, you need to use single quotes. e.g.
move = 'F';

By the way, this code:

if (trnface = 1)
{move = "F";}
if (trnface = 2)
{move = "B";}
if (trnface = 3)
{move = "L";}
if (trnface = 4)
{move = "R";}
if (trnface = 5)
{move = "U";}
if (trnface = 6)
{move = "D";}


Should be done as an else if, or even better using a switch statement. Right now if trnface equals 2, it still checks if it equals 1, 3, 4, 5 and 6 when you know it can't and that is a waste of cpu time.

xFear of Napalm
07-10-2010, 07:58 AM
Thanks, I still suck at C++, but now, I guess I suck a little bit less... Thank you again.

abduraooft
07-10-2010, 08:07 AM
if (trnface = 1)
{move = "F";}
if (trnface = 2)
{move = "B";}
if (trnface = 3)
{move = "L";}
if (trnface = 4)
{move = "R";}
if (trnface = 5)
{move = "U";}
if (trnface = 6)
{move = "D";}

All your if statements are logically wrong! You are using the assignment operator instead of comparison operator.

oracleguy
07-10-2010, 07:03 PM
All your if statements are logically wrong! You are using the assignment operator instead of comparison operator.
I totally missed that when I read the code. Good catch.

Yeah, that is very wrong. All of those if statements will evaluate to true because you are doing assignments instead of equality comparisons which is the double equals.

Shyama_Mukherji
07-14-2010, 06:01 AM
Check by using sngle quotes instead of double quotes.
e.g. move = 'A' ; etc.