0

I'd like to make very simple cash register program. I have no idea why my program skips the user input for item2, then it jumped to Quantity2 and Price2, and cash, then the program stop without calculating anything. Please help me. Thanks in advance.

/*Cash register*/
#include <stdio.h>

int main () {
  char Item1 [10], Item2 [10];
  int Price1, Price2, Quantity1, Quantity2, Cash, Total1, Total2, Subtotal, Tax, Total, Change;
  printf ("Welcome to Sejahtera Electronics Store \n");
  
  printf ("Please input your items, quantities, and price of each item: \n");
  printf("Item 1: ");
  fgets (Item1, 10, stdin);
  printf("Quantity: ");
  scanf("%i", &Quantity1);
  printf("Price: ");
  scanf("%i", &Price1);
  printf("Item 2: \n");
  fgets (Item2, 10, stdin);
  printf("Quantity: ");
  scanf ("%i", &Quantity2);
  printf("Price: ");
  scanf ("%i", &Price2);
  printf ("Input your cash: $");
  scanf ("%i", Cash);
  Total1=Quantity1*Price1;
  Total2=Quantity2*Price2;
  Subtotal=Total1+Total2;
  Tax=Subtotal*0.1;
  Total=Subtotal+Tax;
  Change=Cash-Total;
  printf ("Subtotal: $%i", &Subtotal);
  printf ("Tax: $%i", &Tax);
  printf ("Total: $%i", &Total);
  printf ("Change: $%i", &Change);
  return 0;
}

Output: Welcome to Sejahtera Electronics Store Please input your items, quantities, and price of each item: Item 1: wire Quantity: 5 Price: 5 **Item 2: ** *the program skip this part so I can only insert the quantity and price for item 2 Quantity: 5 Price: 5 Input your cash: $50000 *program stops here, it supposed to calculate the subtotal, tax, total, and change

  • 2
    `scanf` does *not* remove whitespace after scanning, so `fgets` will see a new-line as very first character to read, assuming an empty line. – Aconcagua Dec 13 '22 at 09:11
  • There are further duplicates for this question, e.g. [here](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) or [here](https://stackoverflow.com/questions/61642572/using-fgets-after-scanf). – Aconcagua Dec 13 '22 at 09:14
  • I'm sorry, I have no problem with scanf. Only the second fgets and it stop before calculating the program. Thank you – Nanami Kento Dec 13 '22 at 09:19
  • 1
    Read my comment or the duplicates again: Sure, the `scanf` *does* work fine, but it doesn't consume the *trailing* newline. This is then what `fgets` sees first and will consider this an empty line – while a subsequent `fgets` would then read the input you want to have at first one already (`fgets` *does* consume the newline). On the other hand `scanf` *does* consume any *leading* whitespace, which is the reason for that two subsequent calls to `scanf` *do* work in contrast. – Aconcagua Dec 13 '22 at 09:23
  • Thank you so much. I removed all the fgets (user input for item name), so I only have quantities and prices user inputs and the calculator works fine. – Nanami Kento Dec 13 '22 at 09:31
  • Well, `scanf` is not ideal anyway – as soon as user input is involved you should always consider invalid one. You should at very least check the return value of `scanf`, but that doesn't catch all kinds of invalid input. Better: Use *always* `fgets` to scan entire lines of input, than parse each line read via `sscanf`. This makes it easier to test if all the input asked for has been consumed, e.g. like `if(sscanf(inputLine, "%d %n", &value, &count) == 1 && inputLine[count] == 0) { /* valid */ } else { /* invalid */ }`. Note the space within the format string, consuming any trailing whitespace. – Aconcagua Dec 13 '22 at 09:59
  • It should be the other way around. Forget ```scanf``` exists and use ```fgets``` to read a whole line and parse accordingly. See this for some reasons why: https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html#:~:text=Rule%201%3A%20scanf()%20is,%2C%20using%20the%20%25d%20conversion. – Harith Dec 13 '22 at 10:05

0 Answers0