So for my computer science class I need to create a program that allows a user to input an indefinite number of numbers (as many as they want), and then print the average of the numbers. But, the user is supposed to enter "CTRL+X" to indicate they are done inputting numbers. I have no idea of how to go about doing this.
My class's problem statement is as follows:
Write a program that allows a user to input a stream of numbers and computes their average. The user can enter as many numbers as she wants, signaling the end of the stream by pressing <CTRL> X. Use main() to instruct the user and print the answer . Use a function to take in the user inputs, keep a running total and a count, compute the average and return it.
#include <iostream>
int main()
{
constexprint ctrl_x = 24 ; // or whatever it is on your implementation
int cnt = 0 ;
double total = 0 ;
double number ;
while( std::cout << "number? " && std::cin && std::cin.peek() != ctrl_x )
{
if( std::cin >> number )
{
++cnt ;
total += number ;
}
else
{
std::cout << "please enter a number or ctrl-x to quit\n" ;
std::cin.clear() ; // clear the error state
}
std::cin.ignore( 1000, '\n' ) ; // empty the input buffer
}
if( cnt > 0 ) std::cout << "average is: " << total / cnt << '\n' ;
}