#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
usingnamespace std;
int main ()
{
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<<endl
<<" The purpose of this program is to prompt the user to \n "
<<"enter a number of salaries, then display the average salary, all\n "
<<"salaries above the average, and the percentage of salaries \n"
<< " above the average.\n ";
cout<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
constint SIZE = 30;
double salary = 0, total = 0, average = 0, percent, aboveaverage;
double salaries[SIZE];
int i = 0, counter = 0, high=0, count;
cout << endl;
cout<<setprecision(2)<<fixed<<showpoint;
while ((salary != -1) && (counter < SIZE)) {
cout << "Please enter a salary, -1 to finish entering: ";
cin >> salary;
if (salary != -1) {
salaries[counter] = salary;
counter++;
}
}
if (counter == 0)
cout << "\nNo salaries were entered!\n\n";
else {
// Total
for (i = 0; i < counter; i++)
total += salaries[i];
// Compute and display average of grades
cout << "\n\n Average of entered:\n";
cout<<setprecision(2)<<fixed<<showpoint;
average = total /counter;
cout << " $" << average;
cout<< "\n\n Above average entered: \n";
for(int i = 0; i < counter; i++) //above average
if (salaries[i] > average)
aboveaverage = salaries[i];
cout<< " $"<<aboveaverage << "\n";
cout<<endl;
percent = aboveaverage / counter * 100.00;
cout<<setprecision(2)<<fixed<<showpoint;
cout << "\nPercentage of salaries above average: \n" << " " <<percent << "% \n";
cout<<endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
}
you have absolutely no {} on your above average section.
perhaps like this
1 2 3 4 5 6 7
for()
{
if()
{
cout;
}
}
honestly I find it best practice to ALWAYS put {} on condition, loop, and similar statements even if they only do 1 line of code. Then you KNOW you did it right, and if you change it later, it is STILL right (add a second line, for example).
and on top of that aboveaverage is a value, not a counter.
you need to count the ones above average if you want a %.
that is, if 5 items are above av, you want 5/total
but what you have is
somearraylocation[whatever]/total
#include <iostream>
int main()
{
int n_xs = 0; // what number of exes do we have?
intconst max_xs = 10; // what is the maximum number of exes we're allowed to have?
int xs[max_xs];
double sum_xs = 0.0;
for (; n_xs < max_xs && std::cin >> xs[n_xs]; ++n_xs)
sum_xs += xs[n_xs];
{ // If the user tries to supply more exes than we can handle,
// we must either fail or produce the wrong answer. It's better to fail.
int ignore;
if (std::cin >> ignore)
{
std::cerr << "data set too large\n";
return 1;
}
}
doubleconst average = sum_xs / n_xs;
int n_greater_than_average = 0;
for (int i = 0; i < n_xs; ++i)
if (xs[i] > average) ++n_greater_than_average;
std::cout << 100.0 * (static_cast<double>(n_greater_than_average) / n_xs) << '\n';
}
The problem is that aboveaverage / counter is an int dividing an int.
In C++-world, 1 / 2 gives you 0. With integer division, any fractional part is thrown away. But 1.0 / 2 is an int dividing a double. That gives you 0.5.
To convert an int into a double, write static_cast<double>(my_int)
As in line 29 of my post.