hello everyone.
i'm developing a program containing a for loop where the answer of each each respondent to each questrion is read in.

the program is saying:100 people conmpleted the following questionaire:
what is your highest qualification?
1.if grade 10
1.if grade 12
3. if any tertiary qualification

how old are u?
1.if below 20
2.between 20-32
3.if above 32

what is your gender
1.
m if male
2.f if female
3.h if hetero
the program has to determine
a)the number of males aged between 20-32 and highest qualification
b)the number of people older than30 with a tertiary qualification
c)the number of females aged between 20-30 with a tertiary qualification and display the three total.

i'm just asking for a pseudocode the rest i will do please help

Recommended Answers

All 27 Replies

start loop that counts from 0 to 100
print first question
ask for answer

repeat above for each question
end of loop

what is your gender
1. m if male
2.f if female
3.h if hetero

hetro is not a gender -- its a sexual preference. So only 1 and 2 are the only two possible answers to that question, unless I've slept too long.

sir then how can i force the user to enter the righr information

>then how can i force the user to enter the righr information
I'll assume you mean forcing them to choose one of the selected options. ;) You do that with validation. After the choice is entered, you make sure that it matches one of the options and if it doesn't, require the option to be re-entered.

so which loop control is a possibility to be used

you could use either a do-loop or while-loop. For example

start of loop
    display menu
    get input 
    is input one of the menu items?
    yes, when exit the loop
 end of loop

my problem is this below information that is there whereas it is npt important in the output of the program whereas its part of the program. this information below.

how old are?
1.if below 20
2.if between 20-32
3.if above 32

what is your highest grade.
1.if grade 10
2.if grade 12
3.if teriary qualification.

what is your gender?
1.m if male
2.f if female
3. if heterosexual.

the output is

calcutlation of nale between 20-30
calculation of female between 20-30
calculation of people aboqve 32 where gender doesn't matter

and lastly displaying the total of the three information above.
my loop is starting at 100 because that is the total of the people who were doing this survey

this is what i did.

#include<iostream>
using namespace std;
int main()
{
int maleage32;int femaleage32;int maleagebetween20and32;int femaleagebetween20and32;
int highestgrade;int tertiary;int gender;int malebetween20and30=1;
int i;int age;int age20;int age32;int agebetween20and32;
int male;int female;int heterosexual;int between20and30;
int highestgrade10;int highestgrade12;int olderthan30;

int total;int femalebetween20and30=2;int peopleolderthan30=3;
char m=male; char gendermale;
char f=female;char genderfemale;
char h=heterosexual;char genderheterosexual;
for (i<100;i++;)
highestgrade10=1;
highestgrade12=2;
tertiary=3;
m=male;
f=female;
h=heterosexual;
cout<<"what is your highest grade?"<<endl;
cin>>highestgrade;

age20=1;
maleagebetween20and32=2;femaleagebetween20and32;
maleage32=3;femaleage32=2;
 
cout<<"how old are you?"<<endl;
cin>>age;
gendermale =m;
genderfemale=f;
genderheterosexual=h;
cout<<" what is your gender?"<<endl;
cin>>gender;

total=malebetween20and30+femalebetween20and30+peopleolderthan30;
heterosexual=femalebetween20and30+malebetween20and30;
female=femalebetween20and30;
male=malebetween20and30;
if (gender=m,highestgrade=2)
cout<<"number of males between 20 and 30 and the highest grade12";
else if (gender=heterosexual,highestgrade=3)
cout<<"the number of people older than 30 with a tertiary qualification";

else if(gender=f,highestgrade=3)
cout<<"the number of females between 20 and 30 with a tertiary qualification";

else
cout<<"the total of males,female and people older than 30 is"<<total;
 
 
 
 
return 0;
 
 
 
 
 
}

Please, the next time you post code please please copy so that it retains spacing. How to do that will depend on what compiler you are using

The loop at line 15 is constructed incorrectly. The for statement contains three parts -- (1) initial value of the loop counter, (2) test to determine when to exit the loop, and (3) increment or decrement the loop counter.

The statement on line 15 skipped the first part (initialize loop counter)

Example to correct that line:

for (i = 0; i<100; i++)

Now the problem is: what is line 15 supposed to do? What is really does is execute line 16 100 times -- or set highestgrade10 to a constant value 100 times. You can achieve the same thing by deleting line 15 (the loop) so that line 16 is executed only one time.

If your intent is to execute more than just line 16 within that loop then you need to add braces '{' and '}' around the lines that you want executed, like this

for (i = 0; i<100; i++)
{
      // code to be executed go here
}

thanks sir i will change that but my question is my output is the calculation of males between 20-30
females between 20-30
and people older than 32
so how can i increment them and initialised them

i can assure u sir this program is giving me sleepless nights

Where is the data coming from? I hope its from a text file because entering all those questions for 100 people from the keyboard would be a real pain in the a** :)

If you know about c++ classes yet then that would be the most efficient way to write the program. Otherwise just simple arrays will do.

>>i can assure u sir this program is giving me sleepless nights

Yea, programming has that affect on all of us. :twisted:

sir which simple arrays are ur refferring to and we haven't done the classes yet but can u tell me about those classes that can help me please

you should probably wait on classes until you study them in school.

simple integers will work too if you are more comfortable with using them and it appears you have already defined them. Each of the variables should be initialized to 0, not 1 or 2.

If the data is coming in from a data file then you need to declare an ifstream object and include <fstream> header file. After opening the file read it until no more data (end-of-file). Since I don't know the file format I can't really give any more detailed instructions. Can you post the first two or three lines of the file?

One way to do this is to store the data as it's entered so you can then analyze the data once it's all entered. Containers (for example, arrays) could be used to hold the data.
If you don't want to use classes then I'd consider having several 2 dimensional arrays of ints. For example, 1 array could hold all the information regarding males and another all the information regarding females. The arrays could be organized such that the rows represent age and the columns represent highest education. Then as you read in the information you store the response in the appropriate array using the appropriate row and column. And when you want to answer questions relating to the information presented just find the correct table and look up the appropriate row and column. Note: If you know how to use a 3d array, then you could use that instead of several 2d arrays.

Alternatively, since the questions being asked are known ahead of time then you could collect your information as it is read in and not use containers at all. For example, collect the three pieces of information to three different functions. Each function will analyze the data to answer one of the three questions you need to provide information for. Say information entered was 1, 1, 1 which represents grade 10, under age 20, and female. Sent all three ints to question 1 called males2030highestEducation. Within the function check the third parameter first. This person is a female so don't bother to evaluate the rest of the information since it is irrelevant. Then pass the information to the second function called femalesUnderTwenty, or whatever. Evaluate the third parameter first, which inidicates this is a female, so then evaluate the second parameter which does indicate under twenty so increment a counter to say you found a respondent who fits these criteria. Then send the information to the third function called anyoneWithTertiaryEducation. This person doesn't qualify on that account so just move on.

The problem using functions is how to keep track of the value of the counter used in each function so that you don't start at zero each time you call the function. There are three answers I can think of. First, the counter for each function could be global in scope; but using global variables when you don't have to is frowned on. Second the counters could be declared with the static modifier, but my bet is you probably don't know about that technique yet, and it probably won't allow you access to the counter from main() anyway. Third, you could pass the appropriate counter by reference from main() to the appropriate function. That way any changes to the counter in the function will be maintained in main(). That's the best way, IMO. Whatever way you do it, though, when you're all done just output a statement of the question and the value of the appropriate counter.

Using a container of class objects would be a meld of the two versions noted above. Each class object would hold all the information regarding a single respondent. Then just send each object and a counter to each of three functions to see if that person meets the criteria of the question. It's cleaner than multiple arrays or a single 3 dimensional array and you only send two parameters to each function instead of 4. But, if you don't know about classes yet, then it isn't an option for you.

If you don't know about arrays, or functions, or classes, then using all of the individual variable you declare in your is yet another way, I suppose, but it appears very messy to me and it seems almost unimaginable that it is the approach your instructor intended.

commented: good explination +14

I note that in your code within the conditional statement of the if()s it looks as if you are making the common mistake of using the = sign for equality when in reality equality is the == sign and = is the assignment operator.

thank u sir this is what i did now.

#include<iostream>
using namespace std;
int main()
{
int num,age,gender,grade,grade10=0,grade12=0,tertiary=0;
int agebelow20=0;
int agebetween20and30=0;
int ageabove32=0;
int male=0;
int female=0;
int heterosexual=0;
for(i=1;i>=100;i++)
{
cout<<"what is your highest grade?"<<endl;
cout<<" enter 1 if grade is 10"<<endl;
cout<<"enter 2 if grade is 12"<<endl;
cout<<"enter 3 if tertiary qualification"<<endl;
cin>>grade;
 
 
if (grade==1)
{grade10=grade10+1;}
else
if(grade==2)
{grade12=grade12+1;}
 
else
if(grade==3)
{tertiary=tertiary+1;}
 
cout<<"how old are you?"<<endl;
cout<<"enter 1 if below 20"<<endl;
cout<<"enter 2 if between 20 and 30"<<endl;
cout<<"enter 3 if above 32"<<endl;
cin>>age;
 
if
(age==1)
{agebelow20=agebelow20+1;}
 
else
 
if(age==2)
{agebetween20and30=agebetween20and30+1;}
 
else
if(age==3)
{ageabove32=ageabove32+1;}
 
 
 
cout<<" what is your gender?"<<endl;
cout<<"enter 1 if male"<<male<<endl;
cout<<"enter 2 if female"<<female<<endl;
cin>>gender;
}
return 0;
}

now it is compiling but i want to know how to make it calculate the number of males between20-30 with the highest grade12,female between 20-30 with tertiary qualification and the number of people above 32,and then i have to add the total of the three.

line 12 is still incorrect. The initial value of the loop counter i is initialized to 1, so the exit condition i >= 100 will immeditely fail and the loop will stop. what you want is something like this:

for(i = 0; i < 100; i++)

In this case the loop counter starts at 0 and continues the loop until it reaches 100

Before you can calculate the values you have to add some code to either get the values from the keyboard (bad idea because there are too many ) or read them from a file (most appropriate source of the data). If you get the data from a file, lines 31 thru 35 are not necessary and can be deleted. Likewise you do not need lines 52 to 55. It is not necessary to ask questions when reading the data from a text file.

sir my problem is now i'm stucked so i don't know how to continue.please help

this is what i came out with

#include<iostream>
using namespace std;

int main()
{
int num,age,gender,grade,grade10=0,grade12=0,tertiary=0;
int agebelow20=0;
int agebetween20and30=0;
int ageabove32=0;
int male=0;
int female=0;
int heterosexual=0;
for(i=0;i>=100;i++)
{
cout<<"what is your highest grade?"<<endl;
cout<<" enter 1 if grade is 10"<<endl;
cout<<"enter 2 if grade is 12"<<endl;
cout<<"enter 3 if tertiary qualification"<<endl;
cin>>grade;


if (grade==1)
{grade10=grade10+1;}
else
if(grade==2)
{grade12=grade12+1;}

else
if(grade==3)
{tertiary=tertiary+1;}

cout<<"how old are you?"<<endl;
cout<<"enter 1 if below 20"<<endl;
cout<<"enter 2 if between 20 and 30"<<endl;
cout<<"enter 3 if above 32"<<endl;
cin>>age;

if
(age==1)
{agebelow20=agebelow20+1;}

else

if(age==2)
{agebetween20and30=agebetween20and30+1;}

else
if(age==3)
{ageabove32=ageabove32+1;}

cout<<" what is your gender?"<<endl;
cout<<"enter 1 if male"<<male<<endl;
cout<<"enter 2 if female"<<female<<endl;
cin>>gender;
}

return 0;
}

i hope this will u help u to instruct me the next stepcos i'm already stucked

Here's a skeleton form for how to do this with keyboard input from 100 respondents using the function approach from my first post, and what I could salvage from your last post

#include<iostream>
using namespace std;

//function prototypes
void question1(int, int, int, int&);
void question2(int, int, int, int&);
void question3(int, int, int, int&);

int main()
{
  int grade, age, gender; //variables for user input
  int one, two, three;  //counters for results of questions

  //loop to get answers to three questions from 100 respondents
  for(i = 0; i < 100; i++)
  {
    cout<<"what is your highest grade?"<<endl;
    cout<<" enter 1 if grade is 10"<<endl;
    cout<<"enter 2 if grade is 12"<<endl;
    cout<<"enter 3 if tertiary qualification"<<endl;
    cin>>grade;
 
    cout << "how old are you?" << endl;
    cout << "enter 1 if below 20" << endl;
    cout << "enter 2 if between 20 and 30" << endl;
    cout << "enter 3 if above 32" << endl;
    cin>>age;

    cout << " what is your gender?" << endl;
    cout << "enter 1 if male"<<male << endl;
    cout << "enter 2 if female" << female << endl;
    cin >> gender;

    //pass information from each respondent to all three functions for analysis
    question1(grade, age, gender, one);
    question2(grade, age, gender, two);
    question3(grade, age, gender, three);
 }

  //display values of one, two, and three with appropriate verbal statements here.
  
  cin.gets();
  return 0;
}

//define functions here
void question1(int grade, int age, int gender, int& age)
{
   //analyze information sent in to see if information meets qualifications for question 1.
}

void question2(int grade, int age , int gender, int& two)
{ //analyze information for question 2}

void question3(int grade, int age, int gender, int& three)
{//analyze information for question 3}

Here's a skeleton form for how to do this with keyboard input from 100 respondents using the function approach from my first post, and what I could salvage from your last post

#include<iostream>
using namespace std;
 
//function prototypes
void question1(int, int, int, int&);
void question2(int, int, int, int&);
void question3(int, int, int, int&);
 
int main()
{
  int grade, age, gender; //variables for user input
  int one, two, three;  //counters for results of questions
 
  //loop to get answers to three questions from 100 respondents
  for(i = 0; i < 100; i++)
  {
    cout<<"what is your highest grade?"<<endl;
    cout<<" enter 1 if grade is 10"<<endl;
    cout<<"enter 2 if grade is 12"<<endl;
    cout<<"enter 3 if tertiary qualification"<<endl;
    cin>>grade;
 
    cout << "how old are you?" << endl;
    cout << "enter 1 if below 20" << endl;
    cout << "enter 2 if between 20 and 30" << endl;
    cout << "enter 3 if above 32" << endl;
    cin>>age;
 
    cout << " what is your gender?" << endl;
    cout << "enter 1 if male"<<male << endl;
    cout << "enter 2 if female" << female << endl;
    cin >> gender;
 
    //pass information from each respondent to all three functions for analysis
    question1(grade, age, gender, one);
    question2(grade, age, gender, two);
    question3(grade, age, gender, three);
 }
 
  //display values of one, two, and three with appropriate verbal statements here.
 
  cin.gets();
  return 0;
}
 
//define functions here
void question1(int grade, int age, int gender, int& age)
{
   //analyze information sent in to see if information meets qualifications for question 1.
}
 
void question2(int grade, int age , int gender, int& two)
{ //analyze information for question 2}
 
void question3(int grade, int age, int gender, int& three)
{//analyze information for question 3}

thanks so much sir i really appreciate what u have done fro me let me try and see what i can do

so lets say inthe gender i add heterosexual because i want to calculate the people above 32 where gender male or female doesn't matter,so will it not give me some errors

If gener does not matter why not just leave the gender undefined instead of introducing sexual orientation of hetrosexual or homosexual into it. Likewise with the other categories. If for example age does not matter then you would not want to classify someone as either "ancient" or "youngster".

>>because i want to calculate the people above 32 where gender male or female doesn't matter

Then don't send gender information to the function or ignore gender information sent to the function, your choice.

my problem for introducing hetrosexual is to calculate the people above 32, so how can i do that if hetrosexual is ignored

Since respondents must enter 1 or 2 at gender it doesn't matter what they enter. They will be either male or female. You just want to know how many are both over age 32 and have a certain education grade level of a given value. Now if you wanted to know the percentage of respondents over age 32 with a tertiary degree who are female, then you would need to take into account the answer to the gender question.

my problem for introducing hetrosexual is to calculate the people above 32, so how can i do that if hetrosexual is ignored

Because you will be excluding everyone over 32 who are homosexual. Sexual orientation is not the same as genderless. Learner's response is a reasonable solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.