C goto循环不起作用 [英] C goto loop does not work

查看:131
本文介绍了C goto循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习C,我复制了一个代码来计算VAT>我对其进行了修改,以重新计算用户是否回答为是",然后退出.令我惊讶的是,它的行为很奇怪,如果答案是肯定的,它应该从头开始,要求用户输入物料成本.相反,它期望在按下y之后立即输入成本. 下面的代码;

I am trying to learn C, and I copied a code which calculates VAT> I modified it to recalculate if the user answers yes and exit if the answer in no. To my surprise it behaves in a strange way in that if the answer is yes, it is supposed to go to the beginning as ask the user to enter the item cost. Instead it expects the cost to entered immediately after y is pressed. The code below;

    /* VAT Calculation*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

float price_with_vat(float cost, float vat, int quant);

main()
{
    float cost, vat, full_price;
    int quant,count=0;
    char answer[2];
    char reply[2]={"y"};
    x:
    count=count+1;
    printf("count= %d\n",count);
    printf("Please enter the item cost:\n\n");
    scanf("%f", &cost);
    printf("\nPlease enter the quantity of product:\n");
    scanf("%d", &quant);
    printf("\nquantity=%d\n",quant);
    /* exit(0); */
    printf("\nPlease enter the VAT percentage:\n");
    scanf("%f", &vat);
    printf("\ncost=%6.2f quant=%d vat=%6.2f\n",cost, quant, vat);
    /* exit(0); */
    full_price = price_with_vat(cost, vat, quant);
    printf("The total price is %6.2f\n\n",full_price);
    printf("\nDo you want to perform another transaction? (y/n)\n");

    scanf("%c\n", &answer);
    if(answer==reply)
    {
      system("cls");
      main();
    }
    else
    return 0;
}



     float price_with_vat(float cost, float vat, int quant)


i replace the part

      if(answer==reply)
      {
          system("cls");
          main();
      }
      else

使用

  if(answer==reply)
     goto x

我知道在C语言(以及在Fortran语言中)不建议使用goto构造.我有一个使用do-while循环的变体.它的行为相同. 任何想法为什么这种行为? Zilore Mumba

I know the goto construct is discouraged in C (and also in Fortran). I have a variant which uses a do-while loop. It behaves the same. Any ideas why this behaviour? Zilore Mumba

推荐答案

如其他注释和答案所述,所提供的代码存在多个问题,例如:
1) 通常不需要使用goto,很少使用.
2) 从main()内部调用main().错误.
3) 在程序主体中执行流没有得到很好的控制,
导致您描述的意外行为.
4) 比较技术都是错误的.读取==strcmp()!=等.

As explained in other comments and answers, there are several issues with the code presented, such as:
1) Use of goto is generally unnecessary, rarely used.
2) Calling main() from within main(). Mistake.
3) Execution flow is not well controlled within main body of program,
resulting in the unexpected behavior you described.
4) Comparison techniques are all wrong. Read up on ==, strcmp(), != etc.

以下是类似代码的示例,该代码应更具可预测性,说明如何与用户执行简单的对话框并显示结果.希望对您有所帮助:)

Following is an example of similar code that should perform more predictably, illustrating how to execute a simple dialog with a user, and displaying results. I hope this will help you :)

注意:我的环境不需要我#include <conio.h>,因此我将其删除.您可能需要为环境重新添加它.

Note: my environment did not require me to #include <conio.h>, so I removed it. You may have to add it back in for your environment.

/* VAT Calculation*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

float price_with_vat(float cost, float vat, int quant);

int main(void)
{
    float cost, vat, full_price;
    int quant,count=0;
    char answer[2]={""};
    float running_total=0.0;
    count = 0;
    //dialog section of code
    while(answer[0] != 'n')
    {
    //get price, quantity and VAT
        printf("Please enter the item cost:");
        scanf("%f", &cost);
        printf("\nPlease enter the quantity:");
        scanf("%d", &quant);
        printf("\nPlease enter VAT percentage:");
        scanf("%f", &vat);
        count++;
    //results section   
        printf("\n\nCost: %6.2f\nQuantity: %d\nVAT percent: %6.2f", cost, quant, vat);
        full_price = price_with_vat(cost, vat, quant);
        running_total += full_price;
        printf("The total price is %6.2f\n\n",full_price);
        printf("\nRunning total is currently: %6.2f for %d items.\n",running_total, count);
    //request if continue
        printf("\nDo you want to perform another transaction? (enter \"y\" or \"n\")\n");
        scanf("%s", answer);
    }
    return 0;
}


float price_with_vat(float cost, float vat, int quant)
{
    return cost*((1.0)+vat)*((float)(quant));    
}

这篇关于C goto循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆