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
Too many mistakes in your program.
goto
.scanf("%c\n", &answer);
,%c
expects achar
but you are passing it achar (*)[2]
.char reply[2]={"y"};
, which is not a valid C syntax.reply
is declaredchar
array thenif(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.
You can't compare strings with
==
in C, so this is wrong:You need to use
strcmp()
:strcmp()
requires both arguments to be null-terminated strings. You're never adding a null terminator toanswer
; you should initialize it as:Or, instead of using strings for
reply
and answer, you could declare them as single characters:Then you can use
==
to compare them.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.