简体   繁体   English

根据C中的用户输入重新执行程序

[英]Re-execute program based on user input in C

Hi i'm trying to learn programming in C on my own and have managed to make a verry, verry simple program that calculates the surface of a circle based on user input. 嗨,我正在尝试自己学习C语言的编程,并设法制作了一个非常简单的程序,可以根据用户输入来计算圆的表面。 However the program runs only one time and then closes it. 但是,该程序仅运行一次,然后将其关闭。 This was initially the intention because it is only for learning but i want to expand on this program to increase my skills/knowledge and hope someone can point me in the right direction. 最初的目的是因为它仅用于学习,但我想扩展该计划以提高我的技能/知识,并希望有人可以指出正确的方向。

What i want to do now is instead of terminating the program after running it once; 我现在想做的不是在运行一次之后终止程序。 i would like to offer the user a choise to either stop the program or to continue it and to calculate a new circle. 我想为用户提供一个选择,使其停止程序或继续执行程序并计算一个新的圆。

I understand that it has to be done with an if else statment with the getchar function but i have some issues wrapping my mind around it on how to put it in a program flow. 我知道必须使用带有getchar函数的if if语句来完成,但是我在将其放入程序流中时遇到了一些问题。 I hope someone can give me some directions on how to tackle this problem or can point me to some documentation that explains this properly. 我希望有人可以给我一些有关如何解决此问题的指导,或者可以指出一些正确解释此问题的文档。

Currently i have this: 目前我有这个:

int main(void){

    float diameter;
    double straal;
    double oppervlakte;
    char ch;

        printf("Type de diameter van de cirkel:\t");
        scanf("%g", &diameter);
        printf("\n");

        straal = diameter / 2;
        oppervlakte = PI * (straal * straal);

        printf("De straal =\t%g \n\n", straal  );
        printf("De oppervlakte =\t%f \n\n" , oppervlakte);

        printf("Druk enter om af te sluiten.");

        scanf("%c",&ch);
        getchar();

 return 0;
}

and im trying to accomplish something like this(below) but i can't get it to work properly (i get the warning that the label "diameter" is not defined while trying to compile it.) 和即时通讯试图完成这样的事情(如下),但我无法使其正常工作(我收到警告,尝试编译时未定义标签“直径”。)

#include <stdio.h>
#define PI 3.14

int main(void){

    float diameter;
    double straal;
    double oppervlakte;
    char ch;

        printf("Type de diameter van de cirkel:\t");
        scanf("%g", &diameter);
        printf("\n");

        straal = diameter / 2;
        oppervlakte = PI * (straal * straal);

        printf("De straal =\t%g \n\n", straal  );
        printf("De oppervlakte =\t%f \n\n" , oppervlakte);

        printf("Druk 'd' om door te gaan of druk enter om af te sluiten.");

        if(getchar() == 'd')
            {
            goto diameter; /* tried to use main(void) here but that also doesnt work */
            }
        else{
            scanf("%c",&ch);
            getchar();
            }

 return 0;
}

i do understand that goto is not the best practise to use but in this case it seemed the easyest way to solve this issue. 我确实知道goto并不是要使用的最佳实践,但是在这种情况下,这似乎是解决此问题的最简单方法。 (and the program is not that complex ofc). (并且程序并不复杂)。 However if im wrong in this please let me also know. 但是,如果我在此方面做错了,也请告诉我。

Option 1: (likely the best choice): use a do..while loop. 选项1 :(可能是最佳选择):使用do..while循环。 Place a do { above your primary code block, and add a } while (<repeat condition>); do {放置在您的主代码块上方,然后添加} while (<repeat condition>); at the end. 在末尾。 The program will run through the code once, check the repeat condition (which will be "did the user enter yes"), and if so repeat, otherwise not. 该程序将运行一次代码,检查重复条件(“用户输入是”),如果重复,则重复一次,否则重复一次。

Option 2: recursively call main() . 选项2:递归调用main() You said you "tried that", but I'm not sure if you tried it by attempting to use a goto or not. 您说过“尝试过”,但是我不确定是否通过尝试使用goto来尝试过。 Just use the line main() to call the function again. 只需使用main()行再次调用该函数即可。 Note that if you do this too many times you can end up with a stack overflow, because the computer keeps track of each call. 请注意,如果这样做次数过多,则可能会导致堆栈溢出,因为计算机会跟踪每个调用。 It takes a lot to have it be a problem, but with enough repeats it can happen. 这是一个问题,需要很多时间,但是如果重复很多,它可能会发生。

You can do something like: 您可以执行以下操作:

while(true) //this is an endless loop
{
  //display a menu like
  1. calc area
  2. [anything else if you want to add in future]
  .
  .
  .
  0. exit

  //take user input (e.g 1 for calculating the area)

  switch(user input)
  {
     case 1:
       //code to calculate area
     break;

     case 2:
       //code for anything else
     break

     case 0:
       exit(0);  //this will terminate the program
   }
}

If you follow this pattern, you can add more options to your program in future. 如果遵循这种模式,将来可以在程序中添加更多选项。 You just need to add a case in your switch statement and include that operation in your menu. 您只需要在switch语句中添加一个case ,然后将该操作包括在菜单中即可。 You can search for menu driven c program to get more details. 您可以搜索menu driven c program以获取更多详细信息。 Try reading about while loop and switch... case statements . 尝试阅读有关while loopswitch... case statements

I actually managed to make work in both ways. 实际上,我设法通过两种方式进行工作。 Thanks for the tips and suggestions. 感谢您的提示和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM