Reputation: 904
I'm pretty new to programming. I wanted to make an application that asks for your information and saves them into a text file (but that's coming up later).
I got stuck here, where I'm suppose to make the program read what the user input says:
char nimi[20];
int aika;
int ika;
char juoma[3];
cout << "Hello!\nWhat's your name?\n";
cin >> nimi;
cout << "\n\nHi ";
cout << nimi;
cout << "!\n";
cout << "\nES or MF?";
cin >> juoma;
The program should read if juoma
is "ES" or "MF", and then execute some code depending on the answer.
If something like this would work, it'd solve it, but it won't:
if(juoma==ES){
cout << "Nice choice!"
}
Upvotes: 1
Views: 4148
Reputation: 798
As nvoigt suggested: you have to add double quotes, with that the compiler will be able to create a temporary std::string object and use it to compare it with juoma:
A possible solution that uses const std::string objects would be:
const std::string esCompareHelper("ES");
const std::string mfCompareHelper("MF");
if( (esCompareHelper == juoma) || (mfCompareHelper == juoma) )
{
// your specialized code
}
Upvotes: 0
Reputation: 77304
if(juoma=="ES")
{
cout << "Nice choice!"
}
You were missing the double quotes. You need to have your variable juoma declared as std::string
to work. Working with char arrays in C++ is plain torture, don't do that.
Upvotes: 5