0

I'm not that familiar to C but its the required language so I apologize for the messy code.

So basically I made a program where you can choose multiple items using a function containing a switch statement in it, then it prompts you back to said function if you wanted to order more.

I used an if else statement to calculate if the payment is enough or not. That's where the problem is. Even if I input a value higher than the total price, it still treats it as if the inputted value is not enough. I need to calculate the VAT as well but i can't make a variable for it as the totalPayment variable is not constant.

#include <stdio.h>

double asadoS=40;
double asadoL=55;
double bolaBolaS=40;
double totalPrice;
double cash;
double change;
int choice;
int choice2;

void func()
{
   scanf("%d",&choice);

   switch(choice){
   case 1:
     totalPrice=totalPrice+asadoS;
     break;
   case 2:
     totalPrice=totalPrice+asadoL;
     break;
   case 3:
     totalPrice=totalPrice+bolaBolaS;
     break;
   default:
     printf("Option not available");
     break;
   }
   prompt();
}

void prompt()
{
   printf("Would you like to order more?");
   printf("1.YES,2.NO");
   scanf("%d",&choice2);

   if(choice2==1){
     func();
   }
   else if(choice2==2){
     printf("Your total is: %f",totalPrice);
   }
   else{
     printf("invalid input!");
   }
}

main()
{
   printf("Welcome to 8/11");
   printf("\nEnter the corresponding number of the items you choose");
   printf("\nSiopao: \n 1.Asado(small)= 40php 2.Asado(Large) = 55php \n 
   3.Bola-bola(s)");

   func();
   prompt();

   printf("Please enter your payment");
   printf("%f",totalPrice);
   scanf("%f",&cash);

   if(cash>=(totalPrice)){
     printf("Your change is %f",(cash-totalPrice));
   }
   else if(cash<(totalPrice)){
     printf("Insufficient funds. Please try again");}
   else{
     printf("a");
   }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    Indentation doesn't really matter for the compiler, but for people it's most helpful to help read and understand the code. Please edit your question to try to use some indentation. – Some programmer dude Sep 06 '19 at 12:55
  • As for your problem, now is probably a very good time to learn about *debugging* and how to use a *debugger* to step through your code line by line to see what it really does while monitoring variables and their values. – Some programmer dude Sep 06 '19 at 12:56
  • fixed the indentation a bit. Sorry about that. As for the debugger, it doesn't seem that it shows anything that points toward the wrong value. – lover of the feet Sep 06 '19 at 13:03
  • 2
    One possible problem is the use of recursion instead of loops. The function `func` should not be calling `prompt`. And `prompt` should not be calling `func`. Try to rethink how it could be done using loops instead. And please try to avoid global variables which could be another source of your problem. – Some programmer dude Sep 06 '19 at 13:06
  • I tried printing out the total price of the items to make sure, and it shows the right value. Now for the payment value, it always reverts to "insufficient funds". so I tried putting in printf("Your change is %f",(cash-totalPrice)); in the else if(cash<(totalPrice)) statement, just to see what it actually shows. And it just prints the totalPrice disregarding the formula for the change altogether – lover of the feet Sep 06 '19 at 13:16
  • 1
    There is an error in `scanf("%f",&cash);`, which for a `double` should be `scanf("%lf",&cash);` . – Weather Vane Sep 06 '19 at 14:05
  • I see, changed it to int from double and it works now. Thanks guys! – lover of the feet Sep 06 '19 at 14:39
  • OT: regarding; `double asadoS=40; double asadoL=55; double bolaBolaS=40;` These statements are initializing `double` variables with `integer` literals. Suggest: `double asadoS=40.0; double asadoL=55.0; double bolaBolaS=40.0;` – user3629249 Sep 07 '19 at 05:42
  • OT: regarding statements like: `scanf("%d",&choice);` The `scanf()` family of functions returns the number of successful 'input format conversion specifiers' To assure that function was successful, should check the returned value (not the parameter values). In the statements in the posted code, any returned value other than 1 indicates some error occurred. – user3629249 Sep 07 '19 at 05:47
  • regarding: `printf("\nSiopao: \n 1.Asado(small)= 40php 2.Asado(Large) = 55php \n *newline* 3.Bola-bola(s)");` This does not compile. suggest: `printf("\nSiopao:\n " *newline*" 1.Asado(small)= 40php\n *newline* "2.Asado(Large) = 55php\n" *newline* "3.Bola-bola(s)\n");`As this honors the page right margin and cleanly compiles – user3629249 Sep 07 '19 at 05:51
  • regarding: `main()` This causes the compiler to output a warning message. Note: there are only two valid signatures for `main()` They are: `int main( void )` and `int main( int argc, char * argv[] )` – user3629249 Sep 07 '19 at 05:55
  • regarding: `printf("a");` and similar calls to `printf()`: All this does is place the text into the `stdout` stream buffer, it does not pass the text to the terminal until the program exits. To correct this, so the text is immediately passed to the terminal, end the format string with '\n' – user3629249 Sep 07 '19 at 06:00
  • Note: not all values can be exactly represented in `float` and `double` variables. Therefore, suggest allowing a bit of error when making comparisons with those kinds of variables. see [is floating point broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – user3629249 Sep 07 '19 at 06:04

0 Answers0