Im trying to make -1 the exit key for the user but it stores -1 into the array. Ive
tried making the entire thing a while or do-while loop, nope. Still cannot figure it out. Is the answer right in front of me?
#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 NUM_SALARIES = 5;
int salaries[NUM_SALARIES];
int count, sumcount;
double percent, average, aboveaverage = salaries[0], total = 0.0;
cout<<setprecision(2)<<fixed<<showpoint;
for (count = 0; count < NUM_SALARIES; count++) // obtain salaries fron user
{
cout<< "Please enter a salary, -1 to finish entering: ";
cin>> salaries [count];
if (salaries[0] == -1) // if the user enters no data
{
cout<<endl;
cout<< "No salaries were entered!\n\n";
exit(-1);
}
}
for (sumcount=0; sumcount< NUM_SALARIES; ++sumcount) // calculate total salaries
{
total += salaries[sumcount];
}
cout<<endl<<endl;
average = total / count; //calculate average
cout<< "Average salary: \n";
cout<<" $" <<average<<endl;
cout<<endl;
for (count = 0; count<NUM_SALARIES; ++count) // calculate above average
{
if (salaries[count]> average)
{
aboveaverage = salaries[count];
}
cout<<" the above average is:";
cout<<" $" <<aboveaverage<<endl;
}
percent = aboveaverage / count ; // calculate percentage of above average salaries.
cout<<endl;
cout<<"Percentage of salaries above average: \n";
cout<<" "<<percent<< "% \n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
#include <iostream>
#include <iomanip>
int main() {
std::cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<< "\nThe 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 "
<< "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
constexpr size_t NUM_SALARIES {5};
int salaries[NUM_SALARIES] {};
double total {};
size_t count {};
std::cout << std::setprecision(2) << std::fixed << std::showpoint;
for (; count < NUM_SALARIES; total += salaries[count++]) {
std::cout << "Please enter a salary, -1 to finish entering: ";
std::cin >> salaries[count];
if (salaries[count] == -1)
break;
}
if (count == 0)
return (std::cout << "No salaries entered\n"), 0;
constauto average {total / count};
size_t noAbove {};
std::cout << "\nAverage salary: $" << average << '\n';
std::cout << "Salaries above the average are: ";
for (size_t c {}; c < count; ++c)
if (salaries[c] > average) {
std::cout << '$' << salaries[c] << ' ';
++noAbove;
}
constauto percent {noAbove * 100.0 / count};
std::cout << "\nPercentage of salaries above average: " << percent << "% \n";
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}