-4

Hi I'm new to C and I wrote a simple program. I want to restart the program if the user picked the wrong choice, here is the code:

#include <stdio.h>
#include <cs50.h>
int main(void){
    char choices;
    float math, pc, svt, eng, philo;
    do {
        do {
        printf("Enter your math score: ");
        math = GetFloat();
    }
    while( math>20 || math<0);
    do {
        printf("Enter your pc score: ");
        pc = GetFloat();
    }
    while(pc>20 || pc<0);
    do {
        printf("Enter your svt score: ");
        svt = GetFloat();
    }
    while(svt>20 || svt<0);
    do {
        printf("Enter your eng score: ");
        eng = GetFloat();
    }
    while(eng>20 || eng<0);
    do {
        printf("Enter your philo score: ");
        philo = GetFloat();
    }
    while(philo>20 || philo<0);
    printf("Are you pc or sm?\n");
    printf("Write 1 for pc. 2 for sm\n");
    int choice = GetInt();
    if(choice == 1){
         float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
         printf("Your score is %.2f\n", score);
    }
    else if(choice == 2){
        float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
        printf("Your score is %.2f\n", score);
    }
    else{
        printf("You've picked the wrong choice \n");

    }

        printf("Do you want to try it again? (Y/N) ");
        choices = getchar();
        while (choices != '\n' && getchar() != '\n') {};
    } while (choices == 'Y' || choices == 'y');


}

So what I mean here, I want to insert the code in the else block to restart the program and give the user another time. It would be very nice if I can just make him choose again between 1 or 2.

If you have any suggestions or improvement please don't hesitate to comment. Thanks :)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

3 Answers3

2

What you need is do while loop surrounding choice code:

int choice;

do {
    choice = GetInt();
    if (choice == 1) {
         float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
         printf("Your score is %.2f\n", score);
    }
    else if (choice == 2) {
        float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
        printf("Your score is %.2f\n", score);
    }
    else {
        printf("You've picked the wrong choice, try again.\n");
    }
} while(choice < 1 || choice > 2)
Jezor
  • 3,253
  • 2
  • 19
  • 43
0

You already have a loop to try again, you can reuse that loop to get the user input again for choice. So, if the user enters choice other than 1 or 2, you can set choices = Y and redo the loop. without asking for user input.

Code is below.

#include <stdio.h>
#include <cs50.h>
int main(void)
{
  char choices;
  float math, pc, svt, eng, philo;
  do 
  {      
    // Other code here

    int choice = GetInt();
    if(choice == 1){
         float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25;
         printf("Your score is %.2f\n", score);
    }
    else if(choice == 2){
        float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23;
        printf("Your score is %.2f\n", score);
    }
    else{
        printf("You've picked the wrong choice \n");
        choices = 'Y';

    }

    if ((choice == 1) || (choice == 2))
    {
      printf("Do you want to try it again? (Y/N) ");
      choices = getchar();
      while (choices != '\n' && getchar() != '\n') {};
    }
  } while (choices == 'Y' || choices == 'y');
}
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
-1

Well, if somebody reading this has to actually restart their program for a more nontrivial reason (e.g. an exception handler, like me a couple of minutes ago), there is a portable way to to so in C, like so:

#include <setjmp.h> //from C standard library

jmp_buf restart_env;

int main() {
  //some initialization you don't want to repeat
  if(setjmp(restart_env)) {
    //restarted, do whatever
  }
  //the code
}

void evenFromAnotherFunction() {
  //...
  if(something_that_justifies_this_approach) {
    longjmp(restart_env, 1); //this restarts the program
    //unreachable
  }
  //...
}

Note that you had better not use this if there's a better way. Using setjmp is bound to produce extremely annoying bugs. If you have no choice, remember that some data may preserve the value it had before calling setjmp for the first time, and some may not, so don't assume anything.

Aritz Erkiaga
  • 51
  • 1
  • 6