Reputation: 43
Hey so i'm still learning C++ and i've ran into a snag while compiling this part of my code. Before i go any further with the text game that I'm developing for a project i'd like to know why I am getting this error when attempting to compile so I can debug as I develop.
ISO C++ forbids comparison between pointer and integer [-fpermissive] if (answer1 == 'Yes' && answer1 == 'yes) {
I'm not exactly sure where i am going wrong because the code is storing the result as "Yes". (I checked by adding a std::cout >> answer1; after gathering the users y/n answer.)
but when i try to check that with the If statement i get this error.
#include <iostream>
int main() {
char name[50];
char answer1[50];
std::cout << "Ahh... Welcome newcomer. What is your name?\n";
std::cin >> name;
std::cout << "You said your name was " << name << " correct?\n";
std::cin >> answer1;
// Let the game begin!
if (answer1 == 'Yes' && answer1 == 'yes') {
std::cout << "Lets get this game started then!\n";
}
else {
std::cout << "Then why are you even wasting my time?\n";
}
}
I know it's basic and rudimentary but I am still learning (self-taught, too). Thanks for reading.
Upvotes: 4
Views: 542
Reputation: 311038
The variable answer1
is declared as a character array
char answer1[50];
In the condition of this if statement
if (answer1 == 'Yes' && answer1 == 'yes') {
the array designator is converted implicitly to pointer to its first element (character) that is has the type char *
.
On the other hand, for example this literal 'Yes'
is a multibyte character literal that has the type int
.
So you are trying to compare the address of the first character of an array with an integer that has implementation defined value. It is obvious that this does not make sense.
What you need is to use the standard C function strcmp
that is declared in the header <cstring>
. And instead of character literals use string literals.
Also you have to use the logical OR operator ||
instead of the logical AND operator &&
inside the condition.
For example
#include <cstring>
//…
if ( strcmp( answer1, "Yes" ) == 0 || strcmp( answer1, "yes" ) == 0 ) {
The program will be more safer if instead of character arrays you will use the standard class std::string
.
Upvotes: 4
Reputation: 263357
This: 'Yes'
is a character literal, not a string literal. Multi-character literals have implementation-defined values of type int
, and are not what you want to be using. They are an obscure language feature that is rarely useful, but they seem to be a common stumbling block for new programmers.
You would want this:
if (answer1 == "Yes" && answer1 == "yes")
except that you've defined answer
as a character array. The comparison would compile, but it wouldn't do what you want. It would compare the char*
pointer values, not the contents of the strings, and the comparison would always fail.
Using std::string
, defined in the <string>
header, would let you do the comparison using the overloaded ==
operator. (To compare C-style strings stored in character arrays, you'd have to use strcmp()
declared in <cstring>
, but there's little point in doing that in C++.)
Upvotes: 1