String doesnt work

Hi all, i have this assigment, my program has to read in scores from judges, calculate avaryge and put out a winner. i got that working fine but as well as putting out the highest score i have to put out the name of the winner and unfortunatly my program always puts out the last name, not the name of the acctual winner. PLZ help, its driving me crazy.
ps. I changed it to a string winner = name but it puts out nothing

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>

using namespace std;

int main (){
    unsigned short numJudges, numCeleb;
    float  score, addScore, finalScore = 0, min, max, max2;
    string name, winner;

    cout << "\n\n\tEnter number of judges: ";
    cin >> numJudges;
    cout << "\n\n\tEnter number of paricipants: ";
    cin >> numCeleb;

    for (unsigned short i = 1; i <= numCeleb; i++){
        cout << "\n\n\tEnter name of celebrity " << i << " : ";
        cin >> name;

        addScore = 0, min = 6, max = 0;

        for (unsigned short a = 1; a <= numJudges; a++){
            cout << "\n\n\tEnter score from judge number " << a << " : ";
            cin >> score;
            if (score > max)
                max = score;
            if (score < min)
                min = score;
            addScore+=score;
        }

            max2 = finalScore;
            if (finalScore > max2)
                max2 = finalScore;
                winner = name;
            finalScore = (addScore - max - min) / (numJudges - 2);

            cout << "\n\n\tFinal score is " << finalScore << endl;
    }

            cout << "\n\n\t" << winner << " with a score of " << max2
            << " is a winner" << endl << endl;



return 0;
}
What is wrong when the output screen doesnt show the proper output?
closed account (SECMoG1T)
That means you need something else like a small class or a struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

 struct celebrity
  {
     string name ;
     float score;
   }
   
   celebrity mycel;
   std::vector <celebrity> celebvector;  /// will hold all celebrities details

   for (size_t i=0; i <numCeleb; i++)
     {
       cout <<"enter name of celebrity "<<i <<" and their score from judges :";
       cin>> mycel.name>> mycel. score;
       /// more code here
        celebvector.push_back (mycel);// you can get celebrity name from vector later
      }
        
elleh it gives the wright score but wrong name (last name rather than name of the person who got the highest score.

andy thank you very much but we didn't get anywhere near vectors yet so i don't think i should use it, specialy that i don't understand it.

any other options?
my question really is: why does my string 'winner = name' give nothing?
closed account (SECMoG1T)
Now it works , just a few logical errors otherwise it could have worked, you can comparr to your original and see the error

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

using namespace std;

int main ()
{
     unsigned short numJudges, numCeleb;
     float score, addScore, finalScore = 0, min, max, max2=0;
     string name, winner;

     cout << "\n\n\tEnter number of judges: ";
     cin >> numJudges;

     cout << "\n\n\tEnter number of paricipants: ";
     cin >> numCeleb;

     for (unsigned short i = 1; i <= numCeleb; i++)
        {
            cout << "\n\n\tEnter name of celebrity " << i << " : ";
            cin >> name;

            addScore = 0,
            min = 6,
            max = 0;

            for (unsigned short a = 1; a <= numJudges; a++)
                {
                    cout << "\n\n\tEnter score from judge number " << a << " : ";
                    cin >> score;

                    if (score > max)
                        max = score;

                    if (score < min)
                        min = score;

                        addScore+=score;
                }


                    finalScore = (addScore - max - min) / (numJudges - 2);

                    if(i==1)
                    {
                        max2=finalScore;
                        winner=name;
                    }

                    else
                    {
                        if(max2<finalScore)
                           {
                             max2=finalScore;
                             winner=name;
                           }

                    }

                    cout << "\n\n\tFinal score is " << finalScore << endl;
         }

          cout << "\n\n\t" << winner << " with a score of " << max2 << " is a winner" << endl << endl;

           return 0;
    }
Last edited on
It work perfectly Andy, thanks. could you just explain what is if(i==1) doing in line 43. It's just so i understand, thx
closed account (SECMoG1T)
Okay that a condition I added which intializes max2 /*and*/ name to the results of the first contestant . It takes care of the possibility of having only on contestant in the game and by intializing our variables to the results of the first person we are guaranteed to have a winner at the end and those can only be changed at line 49 if there is any other contestant with scores higher than the current score.

Hope you got the idea.
My brain just exploded lol, I'll go through it tomorrow, maybe I will be able to make sense out of it then... #
Thank you for your help
Topic archived. No new replies allowed.