C goto loop does not work

2019-08-18 14:39发布

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

with

  if(answer==reply)
     goto x

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

标签: c goto
3条回答
甜甜的少女心
2楼-- · 2019-08-18 15:18

Too many mistakes in your program.

  1. You have used goto.
  2. In the statement scanf("%c\n", &answer);, %c expects a char but you are passing it a char (*)[2].
  3. You declared char reply[2]={"y"};, which is not a valid C syntax.
  4. However if reply is declared char array then if(answer==reply) is completely wrong.

Conclusion:

Go through a good tutorial on C or read a good book to know about basic syntax of C.

查看更多
趁早两清
3楼-- · 2019-08-18 15:31

You can't compare strings with == in C, so this is wrong:

if(answer==reply)

You need to use strcmp():

if (strcmp(answer, reply) == 0)

strcmp() requires both arguments to be null-terminated strings. You're never adding a null terminator to answer; you should initialize it as:

char answer[] = { '\0', '\0' };

Or, instead of using strings for reply and answer, you could declare them as single characters:

char reply = 'y';
char answer;

Then you can use == to compare them.

查看更多
成全新的幸福
4楼-- · 2019-08-18 15:34

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 :)

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));    
}
查看更多
登录 后发表回答