Hello all, I will start off by saying Im in the dark with some aspects of C++, in an intro computer course at college. My question is about how to use char commands for when I have multiple users using the same segment of C++.

ex.

3 users are asked to enter their height etc..... Im not sure how to go about the it for 3, 1 user is easy and I have no problem.

Any thoughts or how to's would be appreciated

Thanks
Pete

Dani AI

Generated

Short answer: char is for single characters or C-style strings, not for numeric values such as a person's height. For multiple people, store heights in a numeric container (for example double or float) and collect them in a fixed-size array or, better, a std::vector so the number of entries can vary.

A typical approach in a simple console program is to prompt sequentially for each person (one program instance, multiple inputs). For a fixed small count use std::array; for a variable count use std::vector. Validate input so non-numeric entries don’t corrupt the stored values. If names are needed, use a small struct pairing std::string and double and store that in a vector.

Example (C++11+):

#include <iostream>
#include <vector>
#include <string>

int main() {
    std::size_t n = 3;           // number of people (could be read from input)
    std::vector<double> heights;
    heights.reserve(n);

    for (std::size_t i = 0; i < n; ++i) {
        double h;
        std::cout << "Enter height for person " << (i + 1) << " (cm): ";
        while (!(std::cin >> h)) {            // simple validation loop
            std::cin.clear();
            std::string junk;
            std::getline(std::cin, junk);
            std::cout << "Please enter a numeric value: ";
        }
        heights.push_back(h);
    }

    std::cout << "\nHeights recorded:\n";
    for (std::size_t i = 0; i < heights.size(); ++i)
        std::cout << (i + 1) << ": " << heights[i] << " cm\n";
}

Notes: was right that an array/container is the right idea, but char is the wrong type for heights and C-style strings need a terminating '\0'. ’s loop idea is fine in principle; in idiomatic modern C++ prefer std::vector and std::cin (or std::getline + std::stod) for safer parsing. For true concurrent multiple users (multiple clients at once) the design changes to a server/client or multi-process model, which is a different topic.

Recommended Answers

All 5 Replies

use an array for your place holder. try doing it like this:
char height[] = new char[3];
this tells char that you are making an array. now, to access this you would do something like this, say to output in the dos-type command prompt of most beggining c++ classes
cout << "Height of 1 is: " << height[0];
the last space in the array is always NULL or \0. they are the same thing. now to just go through the array and print all of them you would do this:
for(i = 0; i < 3; i++){
cout << "Height of " << i << " is: " << height;
}
this would print everything in the array except the last byte which is NULL or \0. later one you will get to dynamic arrays which will be set to \0 or NULL before you start using them. But that is beside the point.

What do you mean 3 users using your program? You're only going to have 1 user to your program, unless it's internet based.

If ya' mean 3 different inputs to teh program:

#include <stdio.h>
#define users 3

int main(void) {
printf("Input %d heights:",users);
int loop = 0;
double temp[users];
while (loop < users) {
scanf("%f",&temp[loop]);
loop++;
}
return 0;
}

Change #define users 3 to #define users x, where x is the number of users your want.

Or something...

Oh no... a While Loop. Run!

paladine, I don't believe your code is in c++. It seems to me like the old structured c.

Well, I prefer printf() to cout. If the guy wants, he can change printf() to cout, and scanf() to cin.

But they do the same thing, so it doesn't really matter.

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.