Store program.exe has stopped working, How can I resolve it?

209 Views Asked by At

I'm trying to compile the following code. When I give input to the program then after pressing enter a popup appears which shows that

store program.exe has stopped working

Note: I am using Windows 8.1

Note: I am working on a program (which used in super stores), which includes the following things:

  • Product code
  • Product name
  • Product price
  • Total bill calculations

It's just the starting.

#include <stdio.h>
int main (void)
{
  int d, code;
  char product[100], price[100];    
  printf("\t\t Welcome to the Metro Store\n\n\n\n Enter your product code: ");
  scanf("%d",code);

  if(code<100)
    printf("Pharmacy\n Name of the Medicine");

  fflush(stdout);

  fgets(product, 100, stdin);
  printf(product);  
  return 0;
}
2

There are 2 best solutions below

3
On

For starters you should try

scanf("%d", &code);

You have to tell scanf where to write to. If you don't specify the ampersand (&), scanf will not know where is should write to.

You should read the docs and definitely a good introduction to pointers. If you don't understand pointers, programming in C and C++ is pointless ;-)

Then you can change your fgets() to scanf( "%s", product );

In this case scanf does not need the & because product is short for &product[0]. This can get rather confusing, so get to grips with pointers before continuing.

0
On
  • First, scanf() expects a pointer type variable as as an argument to the format specifier. Use

    scanf("%d", &code);
                ^^
    
  • Second, do not mix up scanf() and fgets(). Otherwise, the fgets() will end up only consuming the newline left by scanf("%d"..). Try to use fgets() for taking user input to be on safer side. However, if you have to use both, use something like

     scanf("%d", &code);
     int ch; while ((ch = getchar())!= EOF && ch != '\n');
     fgets(product,100,stdin);
    

    to avoid the issue with the leftover newline.