File input

This is a discussion on File input within the C++ Programming forums, part of the General Programming Boards category; I get input from a file called "accounts.txt" and try to compare it with a predefined word such as 'blue'. ...

File input

I get input from a file called "accounts.txt" and try to compare it with a predefined word such as 'blue'. Here's an example:

int main()
{
char word[50];
ifstream file;
file.open("accounts.txt");
file>>word;
if(word == "blue")
cout<<"The program works";
else
cout<<"The program does not work";
return 0;
}
I never get "The program works" even if the only possible word is blue!
Help me!!!!!!!

The reason it doesn't work for character arrays is that the character string "blue" is a string literal and its use in your comparison statement in effect is trying to compare the address of the character array word with the address of that string literal in memory. Those two addresses will NEVER be the same.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens