C++ allowing the program to accept repetitive inputs

Hi. I am a beginner in programming. I am trying to make a program in which the heights of N people will be input in centimeters. How do I write a short code that will allow the program to accept repetitive (same) input heights for infinite number of times?
Use a loop.

1
2
3
4
5
6
while (true)
{
    std::cout << "Enter height in cm: ";
    double height;
    std::cin >> height;
}


Of course, doing this an infinite number of times conflicts with doing it N times.
If you need to do something N times, a for loop is more clear.
1
2
3
4
for (int i = 0; i < N; i++)
{
    // ...
}


https://cplusplus.com/doc/tutorial/control/
Last edited on
if you do not know how many N is, you can give the user a way out.
1
2
3
4
5
6
do
{
   your code
   cout << " do you want to go again\n"
   cin >> answer;
}while(answer == 'y'); //or similar 


or in the above while true loop, you can have
if(something)
break; //stops the forever while loop.
1
2
3
4
double height;
while(std::cin >> height){
//...
}
Topic archived. No new replies allowed.