0
Could someone help me with a bug I'm getting in my program? Thanks
Here's the program: int main() { cout <<"guess the first letter of my name!/n"; char firstLetter; cin >> firstLetter; if (firstLetter = r || R) { cout << "I'm not rumple stilsken, I'll give you that."; } else if (firstLetter = q || Q) { cout << "Really? Q?? You guessed q??"; } else { cout << "WRONG. No pancakes for you..."; } } The only error it gives me is that my q's and r's "were not declared in this scope." What's going on here?
3 ответов
+ 1
You forgot this ( ' )
Because you don't have ( ' ), so the compiler thought that r's and q's is variable. And there's no variable named r or q. Which is told by the compiler. (was not declared in the scope.)
+ 1
Thomas Wald
Write it like this
if (firstLetter == 'r' ||
firstLetter == 'R') {
...
}
Do the same pattern with the else if statement, the == is to check equality, while = is assignment. Another error is r, R, and other characters are not inside the single quotation mark ( ' ). Even if you write it like this 'r' || 'R' , it's still wrong, since it will always give true because it checks the value of character r or R, they both have the value more than one, so it's true, firstLetter == true will always be true since every characters has value more than 0. So, you have to write the code like I did above
0
Ok. Thanks that helps a lot 👍👍👌