I have to create a program that allows the user to input numbers and then totals the arithmetic mean. I also have to calculate the number of significant digits in the number that the user inputs. Also I have a sentinel value of -1 and provide error messages if a negative integer has been entered. And if a negative integer has been put in followed by the sentinel value I have to provide an error for that as well. So far this is what I got.
#include <iostream>
using namespace std;
int main()
{
int totalEntry = 0;
double total = 0;
int digits = 0;
double num = 0;
while (num != -1)
{
cout << "Enter a positive integer (or -1 to end): " << endl;
cin >> num;
if (num != -1)
{
total += num;
totalEntry++;
}
while (num != -1)
if (num != -1)
{
if (num < -1)
{
cout << "Invalid integer. Please try again." << endl;
cout << "Enter a positive integer (or -1 to end): " << endl;
cin >> num;
}
else if (num == -1);
{
cout << "You must enter at least one positive integer." << endl;
cout << "Enter a positive integer (or -1 to end): " << endl;
cin >> num;
}
}
else
{
total += num;
totalEntry;
}
cout << "You entered " << num << " and it has " << digits << " digits." <<endl;
}
double mean = total / totalEntry;
cout << "You entered " << totalEntry << " numbers." << endl;
cout << "The Sum is: " << total << " and the Mean is: " << mean << endl;
return 0;
}
I don't even know where to begin in getting the significant digits.