jeudi 28 janvier 2021

How to access a variable inside of an if-statement

This is a section of my function where if the scores were shifted (1) then it prints "Shift amount?" after and collects user input to be used in the compute_total(earned,shifted, shift) function:

printf("Were scores shifted (1=yes, 2=no)? ");
    double shifted;
    scanf("%lf", &shifted);
    if(shifted == 1){
      printf("Shift amount? ");
      double(shift);
      scanf("%lf", &shift);
    }else{}

    double total = compute_total(earned, shifted, shift);

However, obviously I cannot access the variable inside of the if-statement, when I bring the variable and scanf for shift outside of the if scope then the program will run but as I enter a digit for shift amount the terminal does not go any further through my program, it gets "stuck".

How should I solve this issue so I can access the variable but still function correctly?

Aucun commentaire:

Enregistrer un commentaire