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?

Dani AI

Generated

A few practical options that fit the constraint of "no arrays/vectors yet" are worth calling out, since the thread already mentions them: suggested a fixed maximum, mentioned dynamic allocation, and posted a count-first for-loop example. Both a count-first loop (ask for number of people, loop that many times) and a sentinel loop (keep reading until a special value signals "done") avoid arrays while letting the user control how many records are entered. The key is to keep running totals and a counter, then compute averages from those totals.

A concise sentinel-style pattern (uses running totals, validates input, and avoids integer truncation):

#include <iostream>
#include <limits>

int main() {
    double sumH = 0.0, sumW = 0.0, sumS = 0.0;
    int count = 0;
    double h, w, s;

    std::cout << "Enter height in cm (positive) or 0 to finish: ";
    while (std::cin >> h && h > 0.0) {
        std::cout << "Shoe size: ";
        while (!(std::cin >> s)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid. Shoe size: ";
        }
        std::cout << "Weight: ";
        while (!(std::cin >> w)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid. Weight: ";
        }
        sumH += h; sumS += s; sumW += w; ++count;
        std::cout << "Enter next height (or 0 to finish): ";
    }

    if (count == 0) { std::cout << "No data entered.\n"; return 0; }

    std::cout << "Average height: " << (sumH / count) << "\n"
              << "Average shoe size: " << (sumS / count) << "\n"
              << "Average weight: " << (sumW / count) << "\n";
    return 0;
}

Notes and troubleshooting (gaps from earlier replies):

  • Use floating-point types for sums/averages to avoid integer truncation (the code from declares averages as ints, which will lose fractions).
  • Initialize accumulators to zero. Check for division-by-zero before computing averages.
  • Validate input and clear the stream on bad reads (shown above).
  • Choose a sentinel that cannot be a valid data value (negative heights work because they are invalid; 0 is OK if heights/weights cannot legitimately be zero).
  • If later lessons cover arrays/vectors, switch to storing values when median/variance or per-person reporting is required.

This approach keeps the logic simple, compiles with standard C++, and lines up with the classroom restriction while addressing the common pitfalls discussed in the thread.

Recommended Answers

All 5 Replies

It will give us a better idea if you could post your current code here or are you just asking for suggestions on this?

You could do that through dynamic allocation or setting maximums.

It will give us a better idea if you could post your current code here or are you just asking for suggestions on this?

I am just asking for suggestions as to how to go about it. I am trying to think of the best ways of doing it without using arrays or vectors cause we haven't gone over it in class. I want to have it mostly thought out first before I start writing the code.

Set a maximum of 100 people. I'm sure that's more than anyone simply testing your program will enter, and if they try to enter 101 a simple error message will prevent it.

Not exactly sure if my code is perfect (coding on the fly, here) but I believe this should work.

#include <iostream>
using namespace std;

int main()
{
	int max, height, shoeSize; weight, avgHeight, avgShoe, avgWeight;
         int sumHeight = 0, sumShoe = 0, sumWeight = 0;
	
	cout << "Please enter in the number of people you would like to enter data for: ";
	cin >> max;
	
	for(int i = 1; i <= max; i++)
	{
		cout << "Please enter in person " << i << "'s height: ";
		cin >> height;
		sumHeight += height;
		
		cout << "Please enter in person " << i << "'s shoe size: ";
		cin >> shoeSize;
		sumShoe += shoeSize;
		
		cout << "Please enter in person " << i << "'s weight: ";
		cin >> weight;
		sumWeight += weight;
	}
	
	avgHeight = sumHeight/max;
	avgShoe = sumShoe/max;
	avgWeight = sumWeight/max;
	
	cout << endl;
	cout << "The average height is: " << avgHeight << endl;
	cout << "The average shoe size is: " << avgShoe << endl;
	cout << "The average weight is: " << avgWeight << endl << endl;
	
	return 0;
}
commented: If you think it should work you shouldn't post it. We do NOT do homework for people. That's called CHEATING!! -4
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.