You are comparing the single character 'a' to the array of characters 'b'. You have to compare an array to an array. I would recommend using <string> though.
Actually I think they are comparing the character a to the character at index 0 in the array b which in this case would be 'h'. (or the address of the first element anyways)
The problem with your code is that char b[] is an array that holds the characters of hello and null terminating character, but char a only holds a single character 'h' form the input stream and the 'ello' is left in the stream. Then in your if statement you are comparing a ('h') to see if it is the same as b (hello) which returns false. You could use strings to make it much simpler. Like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string b {"hello"};
string a;
cin >> a;
if (a == b){
cout << "hello to you too\n";
}
}
hello
hello to you too
Otherwise you would have to make char a an array large enough to hold hello from the input and then compare a with b. Like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
char b[] = {'h', 'e', 'l', 'l', 'o','\0'};
char a[6];
cin >> a;
if (a == b){
cout << "hello to you too\n";
}
}
hello
hello to you too
giblit wrote:
Actually I think they are comparing the character a to the character at index 0 in the array b which in this case would be 'h'. (or the address of the first element anyways)
No, it is comparing 'h' to 'hello'. To compare the character in a with an element of b he would have to use b with an index (b[0...n]). See:
I believe you have to include <cstring> to make that last code snippet work. You can't just cin to a char array by default. Apparently you can, and <cstring> is not needed. My last sentence is still true though.
You would also have to use strcmp() to compare the string, the == operator is not overloaded for that.
Corrected Code:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main ( )
{
char a[6], b[] = "hello";
std::cin >> a;
if ( !strcmp ( a, b ) ) std::cout << "hello to you too\n";
else std::cout << "no\n";
}
Though, if you try to input something longer than five characters it will write out of bounds.
Right now I'm trying to figure out what is going on with my snippet. I tested all three of them before posting then pasted each output. When I tested that snippet it printed out the if statement so I assumed it was correct, but then wouldn't print it any test after you pointed that out. Definitely scratching my head on it right now.
I figured out what I did, I apparently pasted the output by accident of this test sample. Sorry about the confusion, bad idea to code multiple tests at 3am after being up all day with only 3 hrs sleep.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
usingnamespace std;
int main() {
char b[] = {'h', 'e', 'l', 'l', 'o', '\0'};
string a;
cin >> a;
if (a == b){
cout << "Hello to you";
}
return 0;
}