0

I have to make a game for a programming lession. These are the rules: Choose the amount of credit you want to bet. Choose a number between 0 and 20. If your number is equal to the randomly generated number you will win and gain the amount of money you had bet. If your number is not equal to the randomly generated number you will lose and your bet will be divorced from the amount of credits you have. The part that doesn't work is where the computer asks you if you want to play again. It will just skip the part where it asks and assumes you said no. Teacher couldn't help. Some variables are in estonian so use google translate if you need. The code is here:

#include <stdio.h>
#include <stdlib.h>

//your credits
int krediit = 100;
// your bet
int pakkumine;

int main(void) {
  //these are the rules
  //choose the amount of credit you want to bet. Choose a number between 0 and 20. If your number is equal to the randomly generated number you will win and gain the amount of money you had bet. If your number is not equal to the randomly generated number you will lose and your bet will be divorced from the amount of credits you have.
  printf("1) Vali krediidi kogus, mida soovid panustada\n2) Paku number vahemikus 0 kuni 20.\n3) Kui sinu pakutud number langeb kokku arvuti poolt valitud numbriga, siis võidad!\n\n\n\n\n");

  //asks you if you want to play
  char a;
  printf("Kas te tahate mängida? (j/e) ");
  scanf("%c", &a);
  //runs the game
  if (a == 'j'){
    mang();
  }
  else {
    //if you dont want to play
    printf("Käi perse, sa oled nõme.");
  }
  
  return 0;
}
//the game
int mang(){
  //how much credits are you betting
  int krediidipakkumine;
  printf("Kui palju krediiti te sooviksite panustada\n ");
  scanf("%d", &krediidipakkumine);
  //if your bet is a negative number, it wont let you play
  while (krediidipakkumine < 0){
    printf("Tapa ennast ära\n");
    scanf("%d", &krediidipakkumine);
  }
  //generates the number you had to guess
  int arvutiarv = rand() % 21;
  printf("Pakkuge arv 0 ja 20 vahel\n ");
  scanf("%d", &pakkumine);
  
  
  //cheks if you won or not and gives or takes credits from you
  if (pakkumine == arvutiarv){
    printf("Teie valisite %d ja arvuti valis %d  ", pakkumine, arvutiarv);
    krediit = krediit + krediidipakkumine;
    printf("palju õnne, te võitsite. Teil on nüüd %d krediiti\n ", krediit);
  }
  else {
    printf("Teie valisite %d ja arvuti valis %d  ", pakkumine, arvutiarv);
    krediit = krediit - krediidipakkumine;
    printf("Väga kahju, te kaotasite L + ratio. Teil on nüüd %d krediiti\n\n ", krediit);
  }

  //cheks if you want to play again (doesn't work)
  char b;
  printf("Kas tahate uuesti mängida? (j/e)\n ");
  scanf("%c", &b);
  printf("%c\n", b);
  if (b == 'j'){
    mang();
  }
  else {
    printf("Käi perse, sa oled nõme.");
  }
}

I also tried printing the variable that checks if you want to play again but it prints nothing. Teacher couldn't help. Thanks for any help

Robin
  • 1
  • Try changing `scanf("%c", &a);` to `scanf(" %c", &a);` in the places where `%c` is used in `scanf`. – Tom Karzes Nov 07 '22 at 14:56
  • Unrelated to your scanf problem, you have made the `mang()` function recursive (when the user enters `j` to keep playing). You should instead wrap this code in a loop e.g. `while (replay == 'j') { /* play the game */ }`. It should not be recursive. – jarmod Nov 07 '22 at 14:57

0 Answers0