【问题标题】:C Programming: run a program through a loop?C编程:通过循环运行程序?
【发布时间】:2018-10-22 20:11:21
【问题描述】:

我正在尝试编写一个程序来计算您的更改并报告总金额。我能够编写函数来计算变化,但我不确定如何让它在循环中运行。另一件事是,当程序询问他们的姓名时,我希望用户按 Enter 或返回退出,但我也不确定如何。这是我的第一堂编程课,我正在努力变得更好。感谢您的宝贵时间。

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

float countChange(int quarters, int dimes, int nickles, int pennies);

int main(void) 
{
  int a,b,c,d;
  char yourname[20];
  printf("Your total money is $ %0.2f \n", countChange(12,23,34,45));
  printf("What is your name (Return/Enter to quit)?");
  scanf("%s", yourname);
  printf("\nHow many quarters do you have? \n" );
  scanf("%d", &a);
  printf("\nHow many dimes do you have? \n" );
  scanf("%d", &b);
  printf("\nHow many nickles do you have? \n" );
  scanf("%d", &c);
  printf("\nHow many pennies do you have? \n" );
  scanf("%d", &d);
  printf("All counted, %s has $ %0.2f\n", yourname, countChange(a,b,c,d));
  return 0;
}

float countChange(int quarters, int dimes, int nickles, int pennies)
{
  float QuartersTotal, DimesTotal, NicklesTotal, PenniesTotal, total;
  QuartersTotal= quarters*0.25;
  DimesTotal= dimes*0.10;
  NicklesTotal= nickles*0.05;
  PenniesTotal= pennies*0.01;
  total= QuartersTotal+ DimesTotal+ NicklesTotal+ PenniesTotal;
  return total;
}

【问题讨论】:

  • 你想把哪个部分放入循环中?
  • @CoffeeTableEspresso 我希望程序一直询问用户他们的姓名和他们拥有的硬币数量,直到他们按 Enter 或 Return 退出。谢谢。
  • 好的,我已经将我的答案更新为循环,直到他们不输入他们的名字。希望这会有所帮助!

标签: c loops


【解决方案1】:

我建议你应该使用 fgets 而不是 scanf 来读取任意长度的字符串,这样更安全。 (避免溢出)

while (1)
{
    printf("Your total money is $ %0.2f \n", countChange(12, 23, 34, 45));
    printf("What is your name (Return/Enter to quit)?");

    fgets(yourname, sizeof(yourname), stdin);

    if (yourname[0] == '\n')
        break;

    printf("\nHow many quarters do you have? \n");
    scanf("%d", &a);
    printf("\nHow many dimes do you have? \n");
    scanf("%d", &b);
    printf("\nHow many nickles do you have? \n");
    scanf("%d", &c);
    printf("\nHow many pennies do you have? \n");
    scanf("%d", &d);
    printf("All counted, %s has $ %0.2f\n", yourname, countChange(a, b, c, d));
    getchar();
}

【讨论】:

    【解决方案2】:

    如果您希望程序不断询问用户,您可以简单地使用:

    FILE
    getline
    scanf OR fscanf ...
    

    得到它。

    例如,

    FILE * prompt = stdin;
    int result = 0;
    while ((int)(result = getline(&line, &capacity, prompt)) != -1) {Your code here}
    

    这段代码的本质是:

    1.从标准输入读取

    2。从输入中读取该行,直到它被告知停止

    此代码将永远运行,除非用户想要退出或程序退出。 当给定 command + D 或 command + C 时,它会退出,这是 kill 命令。

    【讨论】:

      【解决方案3】:

      我清理了代码并添加到循环中,希望对您有所帮助:

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <time.h>
      
      float countChange(int quarters, int dimes, int nickles, int pennies)
      {
          float QuartersTotal, DimesTotal, NicklesTotal, PenniesTotal, total;
      
          QuartersTotal= quarters*0.25;
          DimesTotal= dimes*0.10;
          NicklesTotal= nickles*0.05;
          PenniesTotal= pennies*0.01;
          total= QuartersTotal + DimesTotal + NicklesTotal + PenniesTotal;
      
          return total;
      }
      
      void getNumCoins(char *coinType, int *out)
      {
          printf("\nHow many %s do you have?\n", coinType);
          if (scanf("%d", out) <= 0) {
              printf("Please enter a valid integer\n");
              exit(EXIT_FAILURE);
          }
      }
      
      int main(void) 
      {
          int a,b,c,d;
          char yourname[20];
      
          while(1)
          {
              printf("Your total money is $ %0.2f \n", countChange(12,23,34,45));
              printf("What is your name (Return/Enter to quit)?");
              scanf("%s", yourname);
      
              if (yourname[0] == '\n') break;
      
              getNumCoins("quarters", &a);
              getNumCoins("dimes", &b);
              getNumCoins("nickels", &c);
              getNumCoins("pennies", &d);
      
              printf("All counted, %s has $ %0.2f\n", yourname, countChange(a,b,c,d));
          }
          return 0;
      }
      

      如果您明确想要一个循环,我也可以编辑我的答案以显示这一点,但在您的情况下,我认为它不会让它变得更干净。

      希望这会有所帮助!

      【讨论】:

      • 我认为scanf中的%s("%s", out);应该是%d吧?
      • 是的,这是我的错字。很高兴你能明白这一点。
      • 您应该始终检查scanf 是否有解析错误(使用%d 格式说明符很容易发生),并做一些适当的事情,可能是exit(EXIT_FAILURE);*out = 0 或其他。目前,如果用户写“3个季度”作为第一个问题的答案,则此程序具有未定义的行为(未初始化的变量)。
      • 我试图让我的代码尽可能接近给定的原始代码,但最好尽早展示好的做法。
      • yourname[0]yourname 的第一个字符。基本上,如果他们没有输入名字而只是按下回车键,yourname 中唯一的就是输入字符,所以我只是在检查它。如果是这种情况,我们想退出循环,使用break
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      相关资源
      最近更新 更多