### not sure how make -1 the exit key for my loop? 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?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include #include #include #include using namespace std; int main () { cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" <> salaries [count]; if (salaries == -1) // if the user enters no data { cout< average) { aboveaverage = salaries[count]; } cout<<" the above average is:"; cout<<" \$" <
Last edited on ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include // vector: https://cal-linux.com/tutorials/vectors.html double average( const std::vector& salaries ) // invariant : !salaries.empty() { double total = 0 ; // range based loop: https://www.stroustrup.com/C++11FAQ.html#for for( double s : salaries ) total += s ; return total / salaries.size() ; } int main() { std::vector salaries ; double sal = 0 ; while( std::cout<< "Please enter a salary, non-positive to finish entering: " && std::cin >> sal && sal > 0 ) salaries.push_back(sal) ; if( !salaries.empty() ) { const double avj = average(salaries) ; std::size_t count_above_average = 0 ; for( double s : salaries ) if( s > avj ) ++count_above_average ; const double pct_above_average = count_above_average * 100.0 / salaries.size() ; // 100.0: avoid integer division std::cout << std::fixed << std::setprecision(2) // fixed format with two digits after he decimal point << "average: " << avj << '\n' << "percent above average " < Using c-style array rather than a vector (which is better), then possibly:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include 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; const auto 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; } const auto percent {noAbove * 100.0 / count}; std::cout << "\nPercentage of salaries above average: " << percent << "% \n"; std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; }``````

Last edited on line 36-38 what does c represent??? Im not sure I can use size_t
Last edited on The size_t is just an another integer type: https://en.cppreference.com/w/c/types/size_t
Topic archived. No new replies allowed.