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?




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace 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";

const int 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";


}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <iomanip>

// vector: https://cal-linux.com/tutorials/vectors.html

double average( const std::vector<double>& 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<double> 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 " <<pct_above_average << '\n' ;
    }
}
Using c-style array rather than a vector (which is better), then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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;

	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.