Using a do-while loop to manage input - C++ Statement

C++ examples for Statement:do while

Description

Using a do-while loop to manage input

Demo Code

#include <iostream>
#include <locale>                                  // For tolower() function

int main()/* w  ww .ja  va 2  s . c  o m*/
{
  char reply {};                                   // Stores response to prompt for input
  int count {};                                    // Counts the number of input values
  double temperature {};                           // Stores an input value
  double average {};                               // Stores the total and average
  do
  {
    std::cout << "Enter a temperature reading: ";  // Prompt for input
    std::cin >> temperature;                       // Read input value

    average += temperature;                        // Accumulate total of values
    ++count;                                       // Increment count

    std::cout << "Do you want to enter another? (y/n): ";
    std::cin >> reply;                             // Get response
  } while(tolower(reply) == 'y');
  std::cout << "The average temperature is " << average/count << std::endl;
}

Result


Related Tutorials