if statement invloving string
Jul 19, 2010 at 7:58am UTC
Write a program that asks the user to input the capital of Canada. If the input is Ottawa, output
“Correct”, otherwise, output “Incorrect”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
using namespace std;
int main() {
char cityName[7];
cout << "What is the capital of Canada?\n" ;
cin >> cityName ;
if (cityName == ("Ottawa" ) )
cout << "Correct\n" ;
else
cout << "Incorrect!\n"
<< "\n" ;
return 0;
}
Though I input Ottawa it still gives me "incorrect", what is the problem?
Last edited on Jul 19, 2010 at 8:01am UTC
Jul 19, 2010 at 8:08am UTC
You can't compare char arrays like this. Either use std::string which overloads the comparsion operators, or use strcmp(). You can look both up here on this site at the reference section.
Topic archived. No new replies allowed.