Im having trouble with an exercise. I am supposed to ask the user if they want to input a real number. If they answer yes then prompt them. I have that part, but I do not know how to get the average from the numbers entered
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userAns; int i;
cout << "Would you like to enter a real number? ";
cin>> userAns;
while (userAns!="No" && userAns!="no")
{
cout<< "Enter a real number: ";
cin>>i;
cout<< "Would you like to enter a real number? ";
cin>> userAns;
*/ now I do not know where to go from here because I am getting the average up until the user enters no as an answer*/]
The average is the (sum of items) / (total number of items).
Start by having a third variable, int sum = 0; Add to the sum every time the user enters a number into your "i" variable, in the loop.
Next, you'll want another variable to keep track of the total number of items. Initialize it to 0 as well, and add 1 to it every time the user enters a number.
After the loop, divide sum by total. The one "gotcha" here is that dividing two integers does integer division, so you have to convert one of the numbers to a floating-point number,
e.g. cout << "Average: " << ( (double)sum / num_items) << '\n';
Also, make sure you don't divide by 0 (if the user never enters a single number!).
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string userAns;
double i; // <-- double overcomes any integer division problems
double sum{0};
int counter{0};
while(
cout << "Would you like to enter a real number? " &&
cin >> userAns &&
(userAns != "No" && userAns!= "no")
)
{
cout << "Enter a real number: ";
cin >> i;
sum += i;
counter++;
}
if(counter == 0)
cout << "No numbers entered so no average\n";
else
cout << sum/counter << '\n';
return 0;
}
Would you like to enter a real number? y
Enter a real number: 2
Would you like to enter a real number? y
Enter a real number: 5
Would you like to enter a real number? y
Enter a real number: 7
Would you like to enter a real number? No
4.66667
Program ended with exit code: 0
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <limits> // <--- Added.
//#include <string>
usingnamespace std;
int main()
{
char userAns{}; // <--- Changed.
int num{};
cout << "Would you like to enter a real number (y/n)? ";
cin >> userAns;
while (std::tolower(userAns) != 'n')
{
cout << "Enter a real number: ";
cin >> num;
cout << "Would you like to enter a real number (y/n)? ";
cin >> userAns;
}
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
This is a place to start from. With this you can add what has already been discussed.
The question you have to answer is if you need integer division or if you need the output of average to have a decimal number?
Inside the while loop consider this:
1 2 3 4 5 6 7 8
if (num < 1)
{
std::cout << "\n Number must be greater than 0!\n\n";
}
elseif (num)
{
count++; // <--- Does not count an entry of 0.
}
By initializing your variables, especially "userAns" line 13 can be changed and line 14 would not be needed because the while condition would evaluate to true the first time it is checked.
You can shorten the code by putting the loop condition in the middle of the loop. Conceptually, that's where it belongs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
usingnamespace std;
int
main()
{
string userAns;
int i;
while (true) {
cout << "Would you like to enter a real number? ";
cin >> userAns;
if (userAns == "No" || userAns == "no") {
break;
}
cout << "Enter a real number: ";
cin >> i;
}
}