Functions Question

I am writing this program and think I am close. The only problem is the output has some funny hashtag and the highest number output is incorrect. Any advice would be appreciated...

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  /*
  Name: 
  Copyright: 
  Author: 
  Date: 13/04/14 11:13
  Description: 

This program will allow 6 people to enter their ratings from 1 to 5
of a product. The program will read the scores and determine the average scores
as well as the highest and lowest scores. The program will implement functions 
that will perform the following tasks:
     
1. Accept the data - function should prompt the user for score. 
   Error checking should validate that it is in the range of 1 to 5. 
   Function should be called for each of the ratings
   
2. Determine the highest rating - return the highest rating of the values passed
   to the function
   
3. Determine the lowest rating - return the lowest rating of the values passed
   to the function
   
4. Average the scores - display the average score of he values passed to the 
   function.
   
*/

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes

//score
double score( );

// highest rating
double highestRating();

//lowest rating
double lowestRating();

//average scores
double averageScores();

double maximum=0,minimum=1,number,sum=0;

double rating;
float average;
 
int main()
{
   
    //calling 1st function to get rating
    cout << score() << endl;
    
    //calling 2nd function to get and display highest rating
    cout << highestRating() << endl;
    
    //calling 3rd function to get and display lowest rating
    cout << lowestRating() << endl;
    
    //calling final function to calculate and display the average rating
    cout << averageScores() << endl;
    
    system("pause");
    return 0;
}

//********************************
// The score function allows     *
// user to enter ratings.        *
//********************************


double score()
{
       for (int count = 1; count <= 6; count++)
       {
       cout << "Enter the Rating: " << count << ": ";
       cin >> rating;
       sum+=rating; //add to sum 
       // salesperson validation number of sales entered
       
       if ((rating <= 1) || (rating > 5))
          {
          cout << "Rating is invalid may not be less than 1 or more "
          << "than 5.\n"
          << "Enter the Rating: ";
          cin >> rating;
          sum+=rating; //add to sum 
          }
       }
        
}
              
//******************************************
// The highestRating function calculates   *
// and diaplays the Highest rating given   *
//******************************************

double highestRating()
{
       maximum=(rating>maximum)?rating:maximum; //conditional statement,
       //if entered number is greater than our max,its new max
       cout <<"Highest Rating is : "<<maximum<<endl;
       
}

//******************************************
// The lowestRating function calculates    *
// and diaplays the Lowest rating given    *
//******************************************
double lowestRating()
{
       minimum=(rating<minimum)?rating:minimum;//condiitonal statement,
       //if entered number is smaller than our min,its new min
       cout <<"Lowest Rating is: "<<minimum<<endl;
}

//******************************************
// The averageScores function calculates    *
// and diaplays the average rating given    *
//******************************************


double averageScores()
{
       average=float(sum)/float(6);
       cout <<"Their average is : "<<average<<endl;
}



closed account (2UD8vCM9)
Your functions aren't returning anything and you're trying to cout the return values of your functions.

Edit: Also for your scores function you can just type in all 0's, it will tell you that it's invalid input, but it still accepts it.
Last edited on
I think this would be a better program. I made change to:
1:Return type of the functions
2:Made rating an array
3:Made maximum, minimum, and the ratings an int variable since the choice must be among 1,2,3,4, and 5
4. How the main functions access the functions.
5.Moved the maximum and minimum function calling to into the function for when the user is entering the ratings.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
#include <iostream>
using namespace std;

// Function prototypes

//score
void score( );

// highest rating
void highestRating(int);

//lowest rating
void lowestRating(int);

//average scores
void averageScores();

int maximum=0, minimum=6,sum=0;

int rating[6];
float average;
 
int main()
{
   
    //calling 1st function to get rating
    score();
    
    //calling final function to calculate
    averageScores();
    
    cout<<maximum<<" "<<minimum<<" "<<average;
    system("pause");
    return 0;
}

//********************************
// The score function allows     *
// user to enter ratings.        *
//********************************


void score()
{
       for (int count = 0; count < 6; count++)
       {
            cout << "Enter the Rating: " << count+1 << ": ";
            cin >> rating[count];
            // salesperson validation number of sales entered
         
            if ((rating[count] < 1) || (rating[count] > 5))
            {
                cout << "Rating is invalid may not be less than 1 or more "
                << "than 5.\n"
                << "Enter the Rating: " << count+1 << ": ";
                cin >> rating[count];
           }
        sum+=rating[count];
       
       //calling highestrating to get and display highest rating
        highestRating(rating[count]);
        
        //calling 3rd function to get and display lowest rating
        lowestRating(rating[count]);
       }
       
        
}
              
//******************************************
// The highestRating function calculates   *
// and diaplays the Highest rating given   *
//******************************************

void highestRating(int highcheck)
{
       if (highcheck>maximum)
        maximum=highcheck;//if entered number is greater than our max,its new max
}

//******************************************
// The lowestRating function calculates    *
// and diaplays the Lowest rating given    
//******************************************
void lowestRating(int lowcheck)
{
	if (lowcheck<minimum)
        minimum=lowcheck;//if entered number is smaller than our min,its new min
}

//******************************************
// The averageScores function calculates    *
// and diaplays the average rating given    *
//******************************************


void averageScores()
{
       average=float(sum)/float(6);
}
My bad fix that cout in the main and change it to
cout<<"Maximum: "<<maximum<<endl;
cout<<"Minimum: "<<minimum<<endl;
cout<<"Average: "<<average<<endl;

or you can edit it to your liking
@geosnipes
Your code is good, but since it specifies the function returns they can't be void.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
  Name: 
  Copyright: 
  Author: 
  Date: 13/04/14 11:13
  Description: 

This program will allow 6 people to enter their ratings from 1 to 5
of a product. The program will read the scores and determine the average scores
as well as the highest and lowest scores. The program will implement functions 
that will perform the following tasks:
     
1. Accept the data - function should prompt the user for score. 
   Error checking should validate that it is in the range of 1 to 5. 
   Function should be called for each of the ratings
   
2. Determine the highest rating - return the highest rating of the values passed
   to the function
   
3. Determine the lowest rating - return the lowest rating of the values passed
   to the function
   
4. Average the scores - display the average score of he values passed to the 
   function.
   
*/

#include <iostream>
#include <iomanip>
using namespace std;

const int ARR_SIZE = 6;

double score(double sum, double *arr, int size);
double highestRating(double *arr, int size, double maximum);
double lowestRating(double *arr, int size, double minimum);
double averageScores(double sum, int size);

int main()
{
	double min = 6, max = 1, sum = 0;
	double ratingSum, highest, lowest, average;
	double numArray[ARR_SIZE] = {0};
	
	ratingSum = score(sum, numArray, ARR_SIZE);
	highest = highestRating(numArray, ARR_SIZE, max);
	lowest = lowestRating(numArray, ARR_SIZE, min);
	average = averageScores(ratingSum, ARR_SIZE);
	
	cout << "Highest Rating is: " << highest << endl;
	cout << "Lowest Rating is: " << lowest << endl;
	cout << "Their average is: " << average << endl;
	
	return 0;
}

double score(double sum, double *arr, int size)
{
	for(int count = 1; count <= 6; count++)
	{
		cout << "Enter the Rating (1-5): " << count << " : ";
		double rate;
		cin >> rate;
		if ((rate >= 1) && (rate <= 5))
		{
			
			sum += rate;
			arr[count -1] = rate;
		}
		else
		{
			cout << "Not between 1 and 5. Try again!\n";
			count -= 1;
		}
		
	}
	return sum;
}

double highestRating(double *arr, int size, double maximum)
{
	double max = 0;
	for (int i = 0; i < size; i++)
	{
		if (arr[i] > maximum)
		{
			maximum = arr[i];
			max = arr[i];
		}		
	}
	
	return max;
}
double lowestRating(double *arr, int size, double minimum)
{
	double min = 0;
	for (int i = 0; i < size; i++)
	{
		if (arr[i] < minimum)
		{
			minimum = arr[i];
			min = arr[i];
		}
	}
	
	return min;
}

double averageScores(double sum, int size)
{
	double avg = sum / size;
	return avg;
}
Wow thanks for the tips. I don't think I can use array yet. Any tips on how to work this without the array? I'll work in it tonight some more also.. I really appreciate this.. Definitely helping me learn the code...
closed account (2UD8vCM9)
Using global variables is bad in my opinion, but if you're going to use global variables, i'd just do it this way.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 /*
  Name: 
  Copyright: 
  Author: 
  Date: 13/04/14 11:13
  Description: 

This program will allow 6 people to enter their ratings from 1 to 5
of a product. The program will read the scores and determine the average scores
as well as the highest and lowest scores. The program will implement functions 
that will perform the following tasks:
     
1. Accept the data - function should prompt the user for score. 
   Error checking should validate that it is in the range of 1 to 5. 
   Function should be called for each of the ratings
   
2. Determine the highest rating - return the highest rating of the values passed
   to the function
   
3. Determine the lowest rating - return the lowest rating of the values passed
   to the function
   
4. Average the scores - display the average score of he values passed to the 
   function.
   
*/

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes

//score
void score( );


//average scores
double averageScores();

double maximum=0,minimum=6,number,sum=0;

double rating;
float average;
 
int main()
{
   
    //calling 1st function to get rating
    score();
    
    //calling final function to calculate and display the average rating
    cout << "Average Score:" << averageScores() << endl;
    
	cout << "Minimum Rating:" << minimum << endl;
	cout << "Maximum Rating:" << maximum << endl;

    system("pause");
    return 0;
}

//********************************
// The score function allows     *
// user to enter ratings.        *
//********************************


void score()
{
	int count=0;
	while (count<5)
	{
		cout << "Enter the Rating: " << count << ": ";
                cin >> rating;

		if (rating>=1 && rating<=5)
		{
				sum+=rating;
				count+=1;
				if (rating<minimum)
				{
					minimum=rating;
				}
				if (rating>maximum)
				{
					maximum=rating;
				}
		}

		else {
		  cout << "Rating is invalid may not be less than 1 or more "
          << "than 5.\n";
		}

	}

}

//******************************************
// The averageScores function calculates    *
// and returns the average rating given    *
//******************************************


double averageScores()
{
       average=float(sum)/float(6);
	   return average;
}
Last edited on
Pindrought wrote:
Using global variables is bad in my opinion, but if you're going to use global variables, i'd just do it this way.

You are partially right. Doing non-const global variables is considered a bad habit because you could accidentally change it later in your code without realizing it which can cause all sorts of interesting bugs. I use const global variables whenever possible.
I am actually supposed to use the variables within the function and pass them although I couldn't figure out how to make it work....
closed account (2UD8vCM9)
You have to use the solution using arrays then if you are expected to find the lowest/highest of the inputted numbers with that function.
Last edited on
Any suggestions on how to get the variables to pass to the functions instead of the global variables?
Have you used arrays yet? You could make this problem a whole lot easier using arrays.

You can compare min and max to each value in the array and then assign them accordingly.

Also, you need to assign a value to "average" variable because you used RETURN average on line 108.

cout << "Average Score:" << averageScores() << endl;

What you need to do before writing this line is say:

average = averageScores();

Then you write this:

cout << "Average Score:" << average << endl;

When you call averageScores()... it returns the value, but it doesn't assign it to any variable. When you return a value from a function, you have to assign it to a variable.
Thanks for this tip! I can't use Arrays yet.... I am supposed to use functions for this assignment and not global variables...
I just made the change to call the function with

average = averageScores();
cout << "Average Score:" << average << endl;

makes more sense thx!
Topic archived. No new replies allowed.