I just started working on a problem that states "Write a C++ program for a theater that will keep track of a movie attendee. The program should ask the user to enter their eage and gender. The user should enter a negative number when there are no more ages to enter. The program should then break down the attendees by age and gender. The following age categories should be used:
0-18 years
19-30 years
31-40 years
41-60 years
Over 60 years.
The program should also display how many males and females attended the movie, in addition to computing and displaying the age of all attendees, the age of the youngest attendee, and that of the oldest.
I have been working on it and this is what I have so far, I just want to make sure I'm on the right track.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
int age;
int sum;
int average;
double F, M;
cout << "Please enter the age of the attendee (-1 to quit) \n" << endl;
cin >> age;
if (age <= 18)
cout << "Age 0 to 18 \n " <<(age * sum )/age << endl;
I get that it's not finished, but there's a serious flaw already: you're using "sum", but it hasn't been initialized. Uninitialized variables hold nonsense.
Other than that: why are there doubles F and M? You're counting how many males and females entered, right? Are you worried 3/4th of a male will enter? 1/3rd of a female? Aside from simply being words, "int" and "double" have meaning as well. You're not forced to use the logical option, but it makes your code much clearer when you do.
Anyhoo, when you get that sorted out, you can continue on the rest. First of all, you're going to need a loop! You don't want to ask an age. You want to ask ages until it reads -1! Perfect while loop!
Thanks Guys! I haven't started fixing yet but it is due for me tomm and it's worth 6 extra credit points, which I need :P So I will be posting the fixed code over here. If you guys could check it out that would be nice.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
int age;
int sum = 0;
int average;
int age1low = 0;
int age1high = 18;
int age2low = 19;
int age2high = 30;
int age3low = 31;
int age3high = 40;
int age4low = 41;
int age4high = 60;
int age5low = 61;
int age5high = 99;
int age1total = 0;
int age2total = 0;
int age3total = 0;
int age4total = 0;
int age5total = 0;
cout << "Please enter the age of the attendee (-1 to quit) \n" << endl;
cin >> age;
while (age >= 0)
{
if (age <= 18)
cout << "Age 0 to 18 \n " << age1low + age1high << endl;
if (age >= 19 && age <= 30)
cout << "Age 19 to 30 \n " << age2low + age2high << endl;
if (age >=31 && age <= 40)
couy << "Age 31-40 \n " <<
return 0;
}
It still has some f laws. For instance, I am kind of stuck on how to add up how many people btw the ages of 0 and 18 attend and so on for every group.
Simple: if age falls between the ages of category X, then do ageXtotal++;.
By the way: there's no need to define each category's lower AND upper limit. Since they are by definition disjoint sets (i.e. every person belongs to 1 category), you can do this:
if (age >= age(X)low && age < age(X+1)low) { ... }
Now, your code still has quite some flaws. For one thing, any control structure (!) that contains several statements needs { } around its body. In the case of your while, it has an opening {, but no closing }.
Secondly, your loop will never end. Why? Age is only asked once! User inputs 8, loop enters, executes code, returns to check condition and it's still 8, so it repeats. Forever. Solution? Repeat your question at the end of your loop.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
int age(0), sex(0), youngest(999), oldest(0), M(0), F(0), age1(0), age2(0), age3(0), age4(0), age5(0), totalage(0), totalcount(0);
do
{
++ totalcount;
++ totalage;
cout << "Enter sex \n" << sex <<endl;
if
(sex == M || sex == F);
else
cout << "Invalid sex entered \n" << endl;
cout << "Enter age of attendee \n" << age <<endl;
if
(age > 0)
cout << "Invalid age \n" << endl;
if
(age >=0 && age <= 18)
++age1;
if
(age >= 19 && age <= 30)
++age2;
if
(age >= 31 && age <= 40)
++age3;
if
(age >=41 && age <=60)
++age4;
if
(age >=61 && age <=100)
++age5;
if
(age < youngest);
youngest += age;
if
(age > oldest);
oldest +=age;
} while (age > 0 );
double average = totalage/totalcount;
cout << "The average age was \n" << average << endl;
cout << "The youngest person was \n" << youngest << endl;
cout << "The oldest person was \n" << oldest << endl;
return 0;
}
First of all, line 27 is wrong. I'm guessing you meant < instead of >.
Secondly, you're incrementing too early. What if the user inputs -1 (quit) as first option? The counters will still be 1. Move them to the end!
Thirdly, if someone enters -1, the rest of the code will still continue. Not much of a problem, except that your youngest will always be -1. Make it so that the loop quits immediately when -1 is encountered (look up the "break;" statement!).
Forthly, your code after the while (74 and on). What if totalcount is 0? Mayhem, that's what!
Fifthly, line 18, 63 and 68 are wrong. If you end an if with ';', it'll will do nothing and skip the else as well. If you want an empty if, add '{}' at the end of the line.
Lastly, fix your indentation. It's absolutely horrific. Keep the conditions on the same lines as your ifs and go easy on the newlines.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
int age, sex, youngest, oldest, M, F, age1, age2, age3, age4, age5, totalage, totalcount;
cin >> age >> sex >> youngest >> oldest >> M >> F >> age1>> age2 >> age3 >> age4 >> age5 >> totalage >> totalcount;
do
{
cout << "Enter sex \n" << sex <<endl;
if (sex == M || sex == F);
else cout << "Invalid sex entered \n" << endl;
cout << "Enter age of attendee \n" << age <<endl;
if (age < 0)
cout << "Invalid age \n" << endl;
break;
if (age >=0 && age <= 18)
++age1;
if (age >= 19 && age <= 30)
++age2;
if (age >= 31 && age <= 40)
++age3;
if(age >=41 && age <=60)
++age4;
if (age >=61 && age <=100)
++age5;
if (age < youngest)
youngest += age;
if (age > oldest)
oldest +=age;
++ totalcount;
++ totalage;
} while (age > 0 );
double average = totalage/totalcount;
cout << "The average age was \n" << average << endl;
cout << "The youngest person was \n" << youngest << endl;
cout << "The oldest person was \n" << oldest << endl;
return 0;
}
Is this a tad bit better?
When I compile this program, it does not allow me to input any values.
a) Line 18 still has ';' after the if. Get rid of it!
b) Line 21: an if (or any control structure) that has multiple statements in its body needs { } to show which belong to the if and which don't.
I am so close! Here is whats happening, I have made the changes you suggested. When I first compile the program, it waits for me to press any random key before moving on to asking "Enter Sex". After that, a little window pops up saying that the program has stopped working and it gives me three options to either check online for a solution, debug the program or close the program. Any idea why..
controlstructure this rant is not at you, I'm sure your just starting and everyone has to learn so you are doing great but this is ridiculous I'm 15 and I could write that program in less than 10 minutes but no college or university in town will even let me sit in on one of their basic c++ courses! grrrrrr.
ps. what course are you taking?
(if I came off as rude it was not intentional I really do think he is doing great for a beginner)