2

I am currently doing college task to create a simple program using C, to enter and display prices of shirts in store. The problem is about that it doesn't show prices after I enter them in console. I don't really understand what is the problem, because my experience in coding on C ~1 week. Can anyone help with that? Thanks.

int main(void)
{
   float small = 0.0; 
   float medium = 0.0; 
   float large = 0.0;
   float total = 0.0;
   printf("Set Shirt Prices\n");
   printf("================\n");
   printf("Enter the price for a SMALL shirt: $");
   scanf("%f", &small);
   printf("Enter the price for a MEDIUM shirt: $");
   scanf("%f", &medium);
   printf("Enter the price for a LARGE shirt: $");
   scanf("%f\n", &large);
   printf("Shirt Store Price List\n");
   printf("======================\n");
   printf("SMALL : $%f\n", small);
   printf("MEDIUM : $%f\n", medium);
   printf("LARGE : $%f\n", large);
   return 0;
}
ysxapp
  • 21
  • 2
  • 1
    Is the console terminating? You might want to wait for user input at the end. – Irelia Sep 16 '22 at 23:54
  • The newline character `\n` in the last `scanf` is causing `scanf` to wait for more input. After typing the prices, type `bye` and hit enter, and the prices should be displayed. – user3386109 Sep 16 '22 at 23:59
  • Does this answer your question? [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – Andreas Wenzel Sep 17 '22 at 00:01
  • 1
    btw, to print only two digits after the decimal, instead of the default 6 digits, use `%.2f` instead of `%f` – user3386109 Sep 17 '22 at 00:04

1 Answers1

4

This line is problematic:

scanf("%f\n", &large);

It's expecting the user to type a float followed by additional text. Change it to:

scanf("%f", &large);

And if want the end-of-line character, you can prepend it to the next statement.

printf("\nShirt Store Price List\n");
selbie
  • 100,020
  • 15
  • 103
  • 173