String Ifstatements
Sep 14, 2018 at 9:59am UTC
Trying to write a simple code to declare opinions for each teacher (guess which one set the task.) Problem is that I didn't think I'd have to declare string variables for each teachers name just my own variable teacherName. No matter what I try it just comes back with my else statement for "Do not recognise this teacher."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
int main(void )
{
string teacherName;
string darren;
string dave;
string jim;
cout << "Please enter your teachers name \n" ;
cout << "\n" ;
cin >> teacherName;
if (teacherName == darren)
{
cout << "Legend \n" ;
}
else if (teacherName == dave)
{
cout << "OK \n" ;
}
else if (teacherName == jim)
{
cout << "Good \n" ;
}
else
{
cout << "I do not recognise this teacher." ;
}
system("pause" );
return 0;
}
Sep 14, 2018 at 10:12am UTC
darren ,
dave and
jim are string variables that contains empty strings "".
If you want
darren to contain the string "darren" you have to write
string darren = "darren" ;
If you don't want to use these string variables you can write the string literal directly in the if condition.
1 2 3 4
if (teacherName == "darren" )
{
cout << "Legend \n" ;
}
Last edited on Sep 14, 2018 at 10:13am UTC
Sep 14, 2018 at 1:01pm UTC
Thanks! I can see what I've done wrong. Appreciate it Peter87!
Topic archived. No new replies allowed.