I was busy revising previous question papers and I got stuck with this question.
Please help me understand this code, i don't understand why is it giving me the output its giving.
CODE
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string favour;
int tvHours, favourHours;
cout << "How many hours per week do you watch sport on TV, " << endl
<< "what is your favourite sport, how many hours for that? ";
cin >> tvHours >> favour >> favourHours;
if (tvHours > 10)
if (favour == "soccer")
{
if (favourHours > tvHours / 2)
cout << "Group A" << endl;
}
else
if (favourHours > tvHours / 4)
cout << "Group B" << endl;
else
cout << "Group C" << endl;
else
if (favour != "rugby")
cout << "Group D" << endl;
return 0;
I get no output when I input " 11 soccer 5 "
And I get Group D when I input " 6 soccer 6 "
you have no curly brackets {} for the first if statement --> if (tvHours > 10)
remember that if you have statements for an if statement that result in more than one line, you must use brackets to enclose those statements - much more if you are going to nest several if statements within. In fact, none of your if statements have brackets.