1

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.

Cheeseriot
  • 11
  • 1
  • Please try to rephrase your question to include a *minimum* working example. There's a lot of unnecessary code there. Try to distill it down to just the character comparison. – falsePockets Dec 03 '18 at 04:08
  • I don’t see a difference in the outputs, are they correct? Also did you run the code line by line in a debugger checking the variables and seeing what happens? – Sami Kuhmonen Dec 03 '18 at 04:18
  • 1
    After updating playing rating the program exits. I do not see any loop over the "MENU". How "This is the output" displayed? – H.S. Dec 03 '18 at 04:22
  • OT: when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful – user3629249 Dec 03 '18 at 04:30
  • please post a [mcve] that shows the problem. Other wise how are we to help you – user3629249 Dec 03 '18 at 04:32
  • suggest checking the 'selection' via a `switch()` and `case` statements.. Amongst other things, this will go a long way toward the readability of the code – user3629249 Dec 03 '18 at 04:36
  • Did you try to debug the code? Your `scanf("%c", &selection);` return the `\n` character after first run and then the code ends. See answer by Zarak1 to fix this. Then you need a loop, to be able to print new values. – Julo Dec 03 '18 at 04:36
  • 1
    regarding: `scanf("%c",&selection);` where have you allowed for the '\n' to be absorbed/skipped Suggest: `if( scanf(" %c",&selection) != 1) { fprintf( stderr, "failed to input menu selection\n" );` Note the space infront of the `%c` so any 'white space` is consumed – user3629249 Dec 03 '18 at 04:41

1 Answers1

2

Try giving white space before %c, that works. Like

scanf(" %c", &selection);
Zarak1
  • 103
  • 10