C编程:通过循环运行程序吗?

时间:2018-10-22 20:11:21

标签: c loops

我正在尝试编写一个程序来计算您的更改并报告总金额。我能够编写函数来计算更改,但不确定如何使其在循环中运行。还有一件事是,我希望用户在程序询问他们的姓名时按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;
}

3 个答案:

答案 0 :(得分:0)

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

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

如果您明确想要一个循环,我也可以编辑答案以显示该循环,但是对于您而言,我认为它不会使循环变得更加干净。

希望这会有所帮助!

答案 1 :(得分:0)

我建议您使用 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 :(得分:0)

如果您希望程序继续询问用户,则可以简单地使用:

FILE
getline
scanf OR fscanf ...

得到它。

例如

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

这段代码的本质是:

1。从标准输入中读取

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

除非用户要退出或程序退出,否则此代码将永远运行。 当给定命令+ D或命令+ C(即kill命令)时,它将退出。