如何在 C 中重新启动程序?

How can I restart the program in C?

大家好,我是 C 的新手,我写了一个简单的程序。如果用户选择错误,我想重新启动程序,代码如下:

#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');


}

所以我在这里的意思是,我想在else块中插入代码以重新启动程序并给用户另一个时间。如果能让他在1和2之间再选一个就好了

如果您有任何建议或改进,请随时发表评论。 谢谢:)

你需要的是do while循环选择代码:

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)

您已经有一个循环可以重试,您可以重复使用该循环来再次获取 choice 的用户输入。因此,如果用户输入 choice 而不是 1 或 2,则可以设置 choices = Y 并重新执行循环。无需用户输入。

代码如下。

#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');
}

好吧,如果阅读这篇文章的人出于更重要的原因(例如异常处理程序,几分钟前像我一样)实际上必须重新启动他们的程序,那么在 C 中有一种可移植的方法,就像这样:

#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
  }
  //...
}

注意如果有更好的方法最好不要使用。使用 setjmp 势必会产生极其烦人的错误。如果您别无选择,请记住,有些数据可能会保留它在第一次调用 setjmp 之前的值,有些可能不会,所以不要假设任何东西。