how to make something like this work?
if (KeysPressed="K");

Recommended Answers

All 7 Replies

how to make something like this work?
if (KeysPressed="K");

The part on how to fix it:
I see you want to compare a string-variable with a string literal, but you're doing something wrong: you're using the = -operator, this operator is also called the assignment operator, and is used if you want to assign a value, but in this case you want to compare two values, and not assign a value right? So how do we fix it? Simple, to compare two values, you use the == -operator.
So, in order to make your code work (in a more logical way, as your code is syntactically correct), you must change if (KeysPressed="K"); to if (KeysPressed=="K"); .

The more in-depth coverage:
The reason why = doesn't work is simple: = is an assignment operator, which you can't use to compare two variables (at least in C, C++, and some other languages; yes I know, in BASIC-like languages, you can compare using = , but as of C/C++ this isn't the case).
If you write if (KeysPressed="K"); , you assign the string "K" to the string variable KeysPressed, that is: variable KeysPressed now contains the string "K" , as of the assignment operator, it always returns the assigned value, this is why your program did compile, but did not work correctly, in this line of code: if (KeysPressed="K"); , "K" will be used to control the if-statement, "K" is a non-zero value, which means: true, so the code associated with your if will always get executed.
Some programmers follow another convention, they for example write: if ("K"=KeysPressed); , instead of if (KeysPressed="K"); , in the first one, the compiler will report you an error when you try to compile this, because you can't assign a value to a literal.
But in the second one, the compiler will happily compile the whole thing, whether you made the mistake(/typo) of typing one = instead of two.
Conclusion: The programmers who write it this way: if ("K"=KeysPressed); will always be reported an error by the compiler in case they made a typo :)
After seeing the error message they can directly fix it to: if ("K"==KeysPressed); :)

[edit]
Sorry, but for some strange reason, I always tend to write so much commas in one sentence, logical result: my posts are always a few sentences, while they are a whole bunch of lines :P
I hope you can forgive me that :)
[/edit]

commented: marvelous explanation! +12

btw, if you are thinking of comparing a char type only and not a char string,

ie: if keypressed is of type char. You should use ' instead of " as quotes

Thanks for the help guys, but somehow it tends to do stuff no matter if the string value is K or something else..

but somehow it tends to do stuff no matter if the string value is K or something else..

I don't get that, what do you mean by this?

[edit]
I think I got it :)
Could you otherwise post the code of your whole program?
For now I can only guess that it has something to do with the semicolon at the end of the if-statement: if (KeysPressed="K")[B];[/B] :)
[/edit]

example:

cin >> KeysPressed; 
if (KeysPressed=="K");
cout << "you pressed K";

it print "you pressed K" no matter what you you just pressed

example:

cin >> KeysPressed; 
if (KeysPressed=="K");
cout << "you pressed K";

it print "you pressed K" no matter what you you just pressed

Right what I was thinking, you need to tell your compiler what code you want to associate with the if-statement, so this is incorrect

cin >> KeysPressed; 
if (KeysPressed=="K");
cout << "you pressed K";

if you want to associate the line cout << "you pressed K"; with the if-statement.
You can easily fix it, by using an if as follows:

cin >> KeysPressed; 
if (KeysPressed=="K")  // semicolon removed
   cout << "you pressed K"; // this statement is now associated with the if
// all code below is NOT associated with the if

As you see in the above code example, you can only associate one line with the if-statement, however there's another approach: associating a block of code with an if-statement (a block of code starts with a { and ends with a }), example:

cin >> KeysPressed; 
if (KeysPressed=="K")
{
   cout << "you pressed K";
   // all the other code you want to associate with
   // this if-statement must be put between the curly brackets
}
// all the code here doesn't belong to the if anymore

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.