Store mulitlpe variables using for loop in function?

Hey,

This is my first time using functions. I am trying to store or compare multiple variables in a function. I need to prompt users 1 through 6 to enter their score of 1 through 5, output highest rating, lowest rating and compute the average. I have been working this code all week trying different things but I don't know if it's really sinking in.

**I do know there is a similar post named "functions question" from 2014. Unable to add comments to it and I am not allowed to use global variables. Have not learned arrays yet but I spent a few hours watching tutorials and reading up on them. I experimented with them a bit, but to no avail... Anyway here is what I have so far. I want to store a value for 'rating' for each user, each time the loop runs.


#include<iostream>

using namespace std;

// FUNCTION PROTOTYPES--------------------------------------------------
void user_rating(int &, double &);
int highest_number(int &);

// MAIN FUNCTION-----------------------------------------------------------------
int main()
{
int user= 0;
double rating = 0;
user_rating(user, rating);
cout << rating << endl;

}


// FUNCTION GATHERS USERS RATINGS AND ERROR CHECK FOR NUMBER BETWEEN 1 AND 5---
void user_rating(int &user, double &rating)
{

for (int user = 1; user < 7; user++)

{
cout << "Enter user " << user << " rating: ";
cin >> rating;

while (rating < 1 || rating > 5)
{
cout << "Users rating is on a scale of 1-5. Enter " << user << " rating: "<< endl;
cin >> rating;
}
}


}
Please post your question in code tags
its harder to read the way you have it posted.
Also you are more likely to get help if you do so.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Thanks for the link, this should be better.

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
#include<iostream>

using namespace std;

// FUNCTION PROTOTYPES-------------------------------------------------------------------------------------
void user_rating(int &, double &);
int highest_number(int &);

// MAIN FUNCTION-------------------------------------------------------------------------------------------
int main()
{
	int user= 0;
	double rating = 0;
	user_rating(user, rating);
	//topRating = highest_number();
	cout << rating << endl;
	
}


// FUNCTION GATHERS USERS RATINGS AND ERROR CHECK FOR NUMBER BETWEEN 1 AND 5-------------------------------
void user_rating(int &user, double &rating)
{
	//int user;
	//double rating; 
	
	for (int user = 1; user < 7; user++)
	
	{
		cout << "Enter user " << user << " rating: ";
		cin >> rating;
			
			while (rating < 1 || rating > 5)
				{
				cout << "Users rating is on a scale of 1-5. Enter " << user << " rating: "<< endl;
				cin >> rating;
				}
	}	
	

}
Looks like a good start to me..What exactly is your question?
For the variable 'rating', I need to store a value for each time the loop runs to compare the inputs, and output the highest, lowest and average values. I thought about doing this as separate variables but to error check each input gets very redundant. Im not too familiar with arrays and we havent covered them in the class yet...
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




int Highest = rating;
int lowest = rating;
int average[5];

while (rating < 1 || rating > 5)
{
	cout << "Users rating is on a scale of 1-5. Enter " << user << " rating: "<< endl;
				cin >> rating;

        if(highest < rating)
        {
               highest = rating;
        }
        else if( smalest > rating)
        {
              smalest = rating;
        }

       for(int i = 0; i < 5; i++)
       {
              average[i] = rating;
       }
}


This will do that for you.

In the first if statement. Say user enters rating to be "3" the first time. That will set both highest and smallest to equal rating. Next rating, say the user enters 10. if 10 is bigger than the previous rating, which was 3, we replace highest with 10. That goes on for the entire loop, and after it Highest will be the biggest value, and smallest will be the smallest value.

As for the average array. Average[5] gives you 5 hotel rooms. The for-loop places each rating entered in each of the hotel rooms. (You will understand this better once you have learned about array. I recommend watching buckys tutorials on youtube, they are excellent for the basics! - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83)

If you want to later print out the average, all you do is this.

1
2
3
4
5
6
7
8
9
10
       int averageRatings = 0;

      for(int i = 0; i < 5; i++)
      {
               averageRatings = averageRatings +  average[i]

      }
       
      cout << averageRatings ;
Last edited on
Thanks for the help TarikNeaj.

That looks like it works fine but I need to call different functions in main to do each task.
That's why I'm having so much trouble with storing the ratings locally to the function. I need them to be available for use in all of the functions, without global variables. Passing by reference work here? The videos (sakitech) are really helpful and cover mostly everything, however when the problems get a bit more complex, I tend to confuse myself...
Heres what I came up with. The only problem I have is that if I call the highestNumber function before the averageNumber function, I get junk as the highest rating.

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
#include <iostream>
#include <iomanip>

using namespace std;
// FUNTION PROTOTYPES------------------------------------------------------------------------------
void acceptData(float &score);
void averageScore(float a,float b,float c,float d,float e,float f );
int highestRating(float a,float b,float c,float d,float e,float f );
int lowestRating(float a,float b,float c,float d,float e,float f );
// MAIN FUNCTION-----------------------------------------------------------------------------------
int main()
{
	float score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0, score6 = 0;     
	
acceptData(score1);
acceptData(score2);
acceptData(score3); 
acceptData(score4);
acceptData(score5);
acceptData(score6);

averageScore(score1, score2, score3, score4, score5, score6);//order functions and calls correctly

highestRating(score1, score2, score3, score4, score5, score6);
lowestRating(score1, score2, score3, score4, score5, score6);

	system("PAUSE");
    return 0;
	
	
    
}

// FUNTCTION THAT ACCEPTS AND STORES DATA----------------------------------------------------------
void acceptData(float &score)
{

	cout << "Please enter users rating: " << endl;
		cin >> score;

	if (score < 1 || score > 5)
	{
		cout << "Rating must be between 1 and 5. Enter score: " << endl;
		cin >> score;
	}

 
}

// FUNTION THAT COMPUTES THE AVERAGE SCORE---------------------------------------------------------
void averageScore(float a,float b,float c,float d,float e,float f )

{
float average;
	average = (a + b + c + d + e + f)/6;
	cout << "Average rating for the 6 users is: " << average << endl;
}


//FUNCTION RETURNS THE HIGHEST OF THE USERS RATINGS------------------------------------------------
int highestRating(float a,float b,float c,float d,float e,float f)

{
float highest;
cout<<endl;
		if (a > b && a > c && a > d && a > e && a > f)
		{
		highest = a;
		}
		if (b > a && b > c && b > d && b > e && b > f)
		{
		highest = b;
		}
		if (c > a && c > b && c > d && c > e && c > f)
		{
		highest = c;
		}
		if (d > a && d > b && d > c && d > e && d > f)
		{
		highest = d;
		}if (e > a && e > b && e > c && e > d && e > f)
		{
		highest = e;
		}
		if (f > a && f > b && f > c && f > d && f > e)
		{
		highest = f;
		}
	
	cout << "The highest rating is: " << highest << endl;
}


// FUNCTION RETURNS THE LOWEST OF THE USER INPUT---------------------------------------------------
int lowestRating(float a,float b,float c,float d,float e,float f)

{
float lowest;
cout<<endl;
		if (a < b && a < c && a < d && a < e && a < f)
		{
		lowest = a;
		}
		if (b < a && b < c && b < d && b < e && b < f)
		{
		lowest = b;
		}
		if (c < a && c < b && c < d && c < e && c < f)
		{
		lowest = c;
		}
		if (d < a && d < b && d < c && d < e && d < f)
		{
		lowest = d;
		}if (e < a && e < b && e < c && e < d && e < f)
		{
		lowest = e;
		}
		if (f < a && f < b && f < c && f < d && f < e)
		{
		lowest = f;
		}
	
	cout <<"The lowest rating is: " << lowest << endl;
}


 





Topic archived. No new replies allowed.