dimanche 2 décembre 2018

Chars wont compare correctly

This is what I have written so far:

int main(void) {
   int playerJerseys[5];
   int playerRatings[5];

   printf("Enter player 1's jersey number:\n");
   scanf("%d",&playerJerseys[0]);
   printf("Enter player 1's rating:\n\n");
   scanf("%d",&playerRatings[0]);
   printf("Enter player 2's jersey number:\n");
   scanf("%d",&playerJerseys[1]);
   printf("Enter player 2's rating:\n\n");
   scanf("%d",&playerRatings[1]);
   printf("Enter player 3's jersey number:\n");
   scanf("%d",&playerJerseys[2]);
   printf("Enter player 3's rating:\n\n");
   scanf("%d",&playerRatings[2]);
   printf("Enter player 4's jersey number:\n");
   scanf("%d",&playerJerseys[3]);
   printf("Enter player 4's rating:\n\n");
   scanf("%d",&playerRatings[3]);
   printf("Enter player 5's jersey number:\n");
   scanf("%d",&playerJerseys[4]);
   printf("Enter player 5's rating:\n\n");
   scanf("%d",&playerRatings[4]);

   printf("ROSTER\n");
   printf("Player 1 -- Jersey number: %d, Rating: %d\n",playerJerseys[0],playerRatings[0]);
   printf("Player 2 -- Jersey number: %d, Rating: %d\n",playerJerseys[1],playerRatings[1]);
   printf("Player 3 -- Jersey number: %d, Rating: %d\n",playerJerseys[2],playerRatings[2]);
   printf("Player 4 -- Jersey number: %d, Rating: %d\n",playerJerseys[3],playerRatings[3]);
   printf("Player 5 -- Jersey number: %d, Rating: %d\n\n",playerJerseys[4],playerRatings[4]);

   char selection;
   printf("MENU\n");
   printf("u - Update player rating\n");
   printf("a - Output players above a rating\n");
   printf("r - Replace player\n");
   printf("o - Output roster\n");
   printf("q - Quit\n\n");
   printf("Choose an option:\n");
   scanf("%c",&selection);

   if(selection == 'q'){
      return 0;  
   }
   else if(selection == 'u'){
      int userJersey;
      int userRating;
      printf("Enter a jersey number:\n");
      scanf("%d",&userJersey);
      for(int i = 0; i < 5; ++i){
         if(playerJerseys[i] == userJersey){
            printf("Enter a new rating for player:\n");
            scanf("%d",&userRating);
            playerRatings[i] = userRating; 
         }
      }
   }
   else if(selection == 'o'){
      printf("ROSTER\n");
      printf("Player 1 -- Jersey number: %d, Rating: %d\n",playerJerseys[0],playerRatings[0]);
      printf("Player 2 -- Jersey number: %d, Rating: %d\n",playerJerseys[1],playerRatings[1]);
      printf("Player 3 -- Jersey number: %d, Rating: %d\n",playerJerseys[2],playerRatings[2]);
      printf("Player 4 -- Jersey number: %d, Rating: %d\n",playerJerseys[3],playerRatings[3]);
      printf("Player 5 -- Jersey number: %d, Rating: %d\n\n",playerJerseys[4],playerRatings[4]);
   }
   return 0;
}

When this input is provided:

84 7
23 4
4 5
30 2
66 9
u
4
6
o
q

This is the output:

Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 5
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9

MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit

Choose an option:

The desired output is:

ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 6
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9

MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit

Choose an option:

When the u is provided as input the subsequent numbers are supposed to change the rating of one of the players, but for me its not working. I'm not sure if i just don't know how to compare chars? or my if-statement and else-if statments are set up incorrectly? Any advice is appreciated.

Aucun commentaire:

Enregistrer un commentaire