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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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:
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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 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?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343