Help with program possibly involving some loops!

So i need to create a win32 console program that basically asks a user to input certain information about random people...ie: height, shoe size, weight. after the user has entered the information for the first person, the user is then prompted to enter the same information for another person. eventually when the user is done entering the info for all the people, the program needs to output the averages of all the inputs (average height, weight, shoe size). It would make complete sense for me if i knew exactly how many people there were total, because i could define the variables separately for each person...but how can I program this so the user has the control of how many people are entered, but my program still prints out the correct average values?
This probably isn't the best way to do this, but you could do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    #include <iostream>

    using namespace std;

    enum STATS { HEIGHT, SHSIZE, WEIGHT };

    int main()
    {
        int Amount = 0, Stats = 3;
        int People[Amount][Stats];

        cout << "How many people?: ";
        cin >> Amount;

        for(int j = 0; j < Amount; j++)
        {
            cout << "\nPerson " << j+1 << "\n";

            for(int i = 0; i < Stats; i++)
            {
                switch(i)
                {
                    case HEIGHT: cout << "Height: "; break;
                    case SHSIZE: cout << "Shoe Size: "; break;
                    case WEIGHT: cout << "Weight: "; break;
                }

                cin >> People[j][i];
            }
        }
    }


Then you would add your functions etc.
That is the hard way.
1
2
3
4
5
6
7
 
struct  Person
{
     int height;
      int weight;
     float shoesize;
};


//Now create vector<Person> in main
//prompt for input, loop, assigning to person
//push the person onto vector
//calculate averages
Thank you so much for the quick reply! I have a couple questions regarding your example code...

int People[Amount][Stats]; // what do the square brackets around Amount and Stats do?

switch (i) // what does switch do?

last question is what is case and what is break? I have been up to date so far on my readings for my class, so excuse my noobness since these are probably basic things that come up later in my book.

Oh sorry. int People[Amount][Stats] is a multi-dimensional array. It's basically a big box full of cubby holes where you can store data.
- http://www.cplusplus.com/doc/tutorial/arrays/

switch is basically an alternate way of writing if/elseif/else statements. It's more limiting than if statements, but is easier to read. case: x and break; are part of the switch.
- http://www.cplusplus.com/doc/tutorial/control/ (near the bottom)


EDIT:
That is the hard way.

I assumed that if he didn't know how to do this, he wouldn't know how to use vectors / classes.
Last edited on
I appreciate all of your help on this, but im sorry that most of it is going over my head. I have not learned vectors/classes, and I need to find out how to do this without using arrays because we haven't gotten that far in the course material.
It seems strange to get a task like this without being able to use classes/vectors/arrays etc.

What C++ do you know so far (ie. what are you allowed to use for this problem)?
this is what I pretty much know so far...

Output to the user
Escape sequences
User input
Variables
Casting
Basic arithmetic, mod operator
Developing algorithms
Boolean values
Strings
Conditional statements
Loop statements

using what i have learned so far, i am just having trouble figuring out how store the information the user inputs for each person and recalling it later in the program whenever the user decides to stop entering more people.
Last edited on
In that case, you don't need to keep any of the data - you just need the totals.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    #include <iostream>

    using namespace std;

    int main()
    {
        int People, Height, Weight, ShSize, TotalHeight = 0, TotalWeight = 0, TotalShSize = 0;

        cout << "\nHow many people are there?\n> ";
        cin >> People;

        for(int i = 0; i < People; i++) // Runs the code through for every person
        {
            cout << "\n";
            cout << "Person " << i+1 << "'s Height: ";
            cin >> Height;
            cout << "Person " << i+1 << "'s Weight: ";
            cin >> Weight;
            cout << "Person " << i+1 << "'s ShSize: ";
            cin >> ShSize;

            TotalHeight += Height; // This means: TotalHeight = TotalHeight + Height;
            TotalWeight += Weight;
            TotalShSize += ShSize;
        }

        // ...
    }

Now you have all the information you need in those three total variables. It should be a simple task to get the averages now (:
Last edited on
that makes a lot more sense to me now! could i put "do" loops inside the "for" loop? one other thing is i have to reprompt the user to enter valid information if they don't for each variable. I already have my do loops written for that case, but would i simply put them in the for loop?
You can put loops within loops yeah, that's called nesting. You can't really protect against much in the early stages, but you can do something like "if Height > 500 || Height < 1".
is there a way to code this so that the user doesn't say at the beginning how many people they are entering? my program is pretty specific, and it says that the user inputs the Height, weight, and shoe size for the first person...and is only then prompted "do you want to enter another student?" if yes, the program asks for the same inputs from the user for the 2nd person and so on and on. once the user selects not to enter another student, the program prints out the maximums and averages for all the people entered. Maybe i should have been more clear on that earlier...i just want to make sure im piecing it all together correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    #include <iostream>

    using namespace std;

    int main()
    {
        int Choice, People = 1, Height, Weight, ShSize, TotalHeight = 0, TotalWeight = 0, TotalShSize = 0;

        for(int i = 0; i < People; i++) // Runs the code through for every person
        {
            cout << "\n";
            cout << "Person " << People << "'s Height: ";
            cin >> Height;
            cout << "Person " << People << "'s Weight: ";
            cin >> Weight;
            cout << "Person " << People << "'s ShSize: ";
            cin >> ShSize;

            TotalHeight += Height; // This means: TotalHeight = TotalHeight + Height;
            TotalWeight += Weight;
            TotalShSize += ShSize;

            cout << "\nIs there another person?\n1) Yes  2) No\n> ";
            cin >> Choice;

            if(Choice == 1){
                People++; // adds another person.
                continue; // jumps back to the start of the loop.
            } else {
                // No or Invalid Input..
                break; // breaks out of the for loop.
            }
        }

        // ...
    }
thank you so much! that makes sense
Topic archived. No new replies allowed.