Incorrect Output, please help??

#include <iostream>
using namespace std;

int main( )
{
int age, nrDaysPerWeek ;
char gender;
float nonReaders, menOverForty, womenOverForty, maleReaders;


// Initialise

nonReaders = 0;
menOverForty = 0;
womenOverForty = 0;
maleReaders = 0;


// Loop
for ( int i = 1; i <= 30; i++)
{
cout << "How old are you? ";
cin >> age;


do
{
cout << "What is your gender? Enter M (for male) or F (for female):";
cin >> gender;
if ( gender != 'M' && gender != 'F');
} while ( gender != 'M' && gender != 'F');


do
{
cout << "How many days per week do you read a newspaper? ";
cin >> nrDaysPerWeek;
if ( nrDaysPerWeek > 7);
} while ( nrDaysPerWeek > 7);

if (nrDaysPerWeek == 0) {
nonReaders++;
} else if (gender = 'M' && nrDaysPerWeek == 2) {
menOverForty++;
} else if (gender = 'F' && nrDaysPerWeek == 1) {
womenOverForty++;
} else if (gender = 'M' && nrDaysPerWeek == 7) {
maleReaders++;
}

cout << nonReaders << " do not read a newspaper " << endl;
cout << menOverForty << " men older than 40 years read a newspaper once or twice a week " << endl;
cout << womenOverForty << " women older than 40 read a newspaper once a week " << endl;
cout << maleReaders << " men read a newspaper 7 days a week " << endl;

return 0;
}


How do i get the program not to compile the total after each set of input, but to do so after all the data has been inputed???
I'm assuming you want to input the 30 sets of age & reading days then display a single set of totals?

The code almost does it already - all you need is a closing '}' before the line
cout << nonReaders << " do not read a newspaper " << endl;
The code would not compile without this.

Note also that in the two do {..} while loops, the 'if (...);' does not actualy do anything and can be removed.
#include <iostream>
using namespace std;

int main( )
{
int age, nrDaysPerWeek ;
char gender;
float nonReaders, menOverForty, womenOverForty, maleReaders;


// Initialise

nonReaders = 0;
menOverForty = 0;
womenOverForty = 0;
maleReaders = 0;


// Loop
for ( int i = 1; i <= 30; i++)
{
cout << "How old are you? ";
cin >> age;
} //why is this loop used ....its is not clear...

do
{
cout << "What is your gender? Enter M (for male) or F (for female):";
cin >> gender;
if ( gender != 'M' && gender != 'F');
} while ( gender != 'M' && gender != 'F'); //while statement is not clear???


do
{
cout << "How many days per week do you read a newspaper? ";
cin >> nrDaysPerWeek;
if ( nrDaysPerWeek > 7);
} while ( nrDaysPerWeek > 7); // this line should be shifted to*

if (nrDaysPerWeek == 0)
{
nonReaders++;
}
else if (gender = 'M' && nrDaysPerWeek == 2)
{
menOverForty++;
}
else if (gender = 'F' && nrDaysPerWeek == 1)
{
womenOverForty++;
}
else if (gender = 'M' && nrDaysPerWeek == 7)
{
maleReaders++;
}
//*should be shifted here

cout << nonReaders << " do not read a newspaper " << endl;
cout << menOverForty << " men older than 40 years read a newspaper once or twice a week " << endl;
cout << womenOverForty << " women older than 40 read a newspaper once a week " << endl;
cout << maleReaders << " men read a newspaper 7 days a week " << endl;
getch();
return 0;
}


try with this
Last edited on
closed account (z05DSL3A)
@milee
Your code is not correct!
1
2
3
4
5
6
// Loop
for ( int i = 1; i <= 30; i++)
{
cout << "How old are you? ";
cin >> age;
} //why is this loop used ....its is not clear...  

The for loop should not be closed here, this will just ask you for your age 30 times and do nothing else. it will also make the remaining code execute once. As Faldrax said the closing brace needs to go above
cout << nonReaders << " do not read a newspaper " << endl;
Last edited on
@Grey Wolf
well, the whole conditional statement should be executed here....before the loop closes...
Last edited on
Topic archived. No new replies allowed.