5 Replies - 931 Views - Last Post: 19 April 2007 - 05:58 AM Rate Topic: -----

#1 Simply  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

correct layout of coding may be causing errors

Posted 19 April 2007 - 04:47 AM

The program has to compute 4 totals
how many women older than 30 years who dont watch TV at all
how many women indecate M for theri fav
how many people younder than 20 years indicate S for their fav
how many men older than 35 indicate S for theri fav.

I need to initilaise the for totals
for loop over 3 people
input A to first Q
input A to second A and validate it useing a do -while statment
input A to third Q
a block of nested ifs where the correct total is incremented.
Display the 4 totals.

Th questionaire looks as follows
how old are you?
what is your gender M or F?
which of the following trypes of TV programs do you watch most often?
N = News
S = Sport
M = movies
E = education
O = other
X = dont watch tv

This is what I have done so fare. I do believe I have followed the insturctions above correctly but I am not sure if my nested if statments are correct and I am getting errors when I try run the program.

//Questionaire
#include <iostream>
using namespace std;

int main( )
{
int Age;
char Gender, F, M, Favorate;

//for loop 
for ( int i = 1; i <= 3; i++)
{
//Question one
cout << "How old are you? ";
cin >> Age;

//counters
int TotalNoTV = 0;
int TotalM = 0;
int TotalSport = 0;
int TotalMen = 0;

//Do loop to validate the answer of question two
do
{
cout << " What is your Gender? " <<endl;
cin >> Gender;

if (Gender != 'F' && Gender != 'M')
cout << " TYPE F FOR FEMALE OR M FOR MALE! ";

} while ( Gender != 'F' && Gender != 'M');//End of loop

//Third question
cout << " Which of the following types of TV programs do you watch most often";
cout << " (choose one of N,S,M,E,O or X):";
cin >> Favorate;

//nested if statment
{
if (Gender ==F && Age <=30)
	TotalNoTV++;
else 
	if(Gender == F && Favorate == M)
	TotalM++;
	else
		if ( Age <= 20 && Favorate == S)
		TotalSport++;
		else
			if ( Gender == M && Favorate == S)
			TotalMen++;	 
}//end of if statment

//displays
cout << "The total number woman older than 30 who dont watch TV add up to: " << TotalNoTV << endl;
cout << "The amount of woman who indicated M for their favourit add up to: " << TotalM << endl;
cout << "The total number of people younger than 20 that indicated S as their favorate add up to: " << TotalSport << endl;
cout << "The amount of men older than 35 that also indicated S as their facorate add up to: " << TotalMen << endl;

return 0;
}


Error I recieve is the following:
[Warning] In function `int main()':
`S' undeclared (first use this function)
[Build Error] (Each undeclared identifier is reported only once for each function it appears in.)
expected `}' at end of input

Is This A Good Question/Topic? 0
  • +

Replies To: correct layout of coding may be causing errors

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: correct layout of coding may be causing errors

Posted 19 April 2007 - 05:08 AM

if ( Gender == M && Favorate == S)


Is S mean to be a value entered by the user? Or a variable name? If the latter, it has not been declared as a variable. If the former, you will have to enlcose it in single quotes.
Was This Post Helpful? 0
  • +
  • -

#3 Simply  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

Re: correct layout of coding may be causing errors

Posted 19 April 2007 - 05:17 AM

View PostAmadeus, on 19 Apr, 2007 - 05:08 AM, said:

if ( Gender == M && Favorate == S)


Is S mean to be a value entered by the user? Or a variable name? If the latter, it has not been declared as a variable. If the former, you will have to enlcose it in single quotes.



Yes its meant to be a value entered by the user! I know the simple answer would be to then declare it as a variable but then I thought by declaring Favorates as a character it would provent me from delaring all the TV options as variables

char Gender, F, M, Favorate;


This post has been edited by Simply: 19 April 2007 - 05:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: correct layout of coding may be causing errors

Posted 19 April 2007 - 05:20 AM

It would...if you treat it like a character, as so:
if ( Gender == M && Favorate == 'S')


Without the quotes, the compiler sees it as a variable - one that has not been declared.
Was This Post Helpful? 0
  • +
  • -

#5 Simply  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 07

Re: correct layout of coding may be causing errors

Posted 19 April 2007 - 05:29 AM

Ok that makes sense! Thank you . I guess I could even have it as follows

  if ( Age <= 20 && Favorate == 'S' || Favorate == 's')
		TotalSport++;
		else
			if ( Gender == M && Favorate == 'S' || Favorate == 's')
			TotalMen++;



What about the last error. I get it often and I dont know what it means or how to find it

[Warning] In function `int main()':
expected `}' at end of input


This post has been edited by Simply: 19 April 2007 - 05:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: correct layout of coding may be causing errors

Posted 19 April 2007 - 05:58 AM

 
{
if (Gender ==F && Age <=30)
TotalNoTV++;
else
if(Gender == F && Favorate == M)
TotalM++;
else
if ( Age <= 20 && Favorate == S)
TotalSport++;
else
if ( Gender == M && Favorate == S)
TotalMen++;
}//


the { braces are not needed here....they are not delineating anything.

As for the other error, it appears as if you have not closed the for loop anywhere.

As an FYI, for this line (and similar ones):
 if ( Age <= 20 && Favorate == 'S' || Favorate == 's')


I think you may mean:
 if ( Age <= 20 && (Favorate == 'S' || Favorate == 's'))


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1