Declaring an array to be string?

I'm writing this program that basically interprets the rottentomatoes website. I am however having a problem declaring if it is rotten or fresh according to the rating the user enters.

I'm outputting it here:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintAll(const string titles[], const int ratings[], int count)
{
	WriteLine('=', 50);
	cout << "PRINT ALL" << endl;
	WriteLine('-', 50);
    
	for(int i = 0; i < count; i++)
	{
        cout << setw(37) << left << titles[i]
        << setw(2) << left << ratings[i] <<  setw(5) << left << "%"
        << setw(4) << right << RatingToString(ratings, count) << endl;
	}
}


And here is my condition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string RatingToString(const int ratings[], int count)
{
    string rank;
    for(int i = 0; i < count; i++)
    {
        if(ratings[i] < 60)
        {
            rank = "ROTTEN";
        }
        else
        {
            rank = "FRESH";
        }
    }
    
    return rank;
}


Here is the output:


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
==================================================
MENU
1. Add Movie
2. Print All 
3. Exit
--------------------------------------------------
Enter 1-3 : 1
Title : Hitch
Rating : 90
==================================================
MENU
--------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
--------------------------------------------------
Enter 1-3 : 2
==================================================
PRINT ALL
--------------------------------------------------
Hitch                                90%    FRESH
==================================================
MENU
--------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
--------------------------------------------------
Enter 1-3 : 1
Title : The Hulk
Rating : 50
==================================================
MENU
--------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
--------------------------------------------------
Enter 1-3 : 2
==================================================
PRINT ALL
--------------------------------------------------
Hitch                                90%    ROTTEN
The Hulk                             50%    ROTTEN
==================================================
MENU
--------------------------------------------------
1. Add Movie
2. Print All 
3. Exit
--------------------------------------------------
Enter 1-3 : 3


My condition works when there is only one movie, but when I add more, it gives it the new movies "ROTTEN" or "FRESH" rank. I don't know if this makes sense and if you guys know of a way to fix this? Thank you.
Last edited on
In your function RatingToString:

The string 'rank' is only declared once and is overwritten by each iteration of your loop. This means that the function returns the result of the last iteration (ratings[count-1]).
Try to adjust your function prototype like this:

 
string RatingToString(int rating)

(of course you need to adjust the function body as well (remove the loop))
and call it like this:

 
cout << RatingToString(ratings[i]);
Topic archived. No new replies allowed.