Question 3b
Tony Keets has to determine how many of his fellow students regularly use public transport. The C++ program
below is supposed to input the data (Y indicates public transport, N indicates private transport, X indicates the end of
the data) and display the result.
// How many students use public transport
#include <iostream>
using namespace std;
int main( )
{
char answer;
int numPublic;
// initialize
numPublic = 0;
cout << "First student: Do you normally use public transport? " << endl;
cout << "Answer Y (for yes), N (for no), X (for end of input)" << endl;
cin >> answer;
// loop
while (answer != 'X')
{
if (answer == 'Y') // public transport or not?
numPublic++;
} // end of while loop
// results
cout << "Number of students using public transport is ";
cout << numPublic << endl;
return 0;
}