arun483 arun483 - 2 months ago 29
C Question

C program crashing on scanf

I got a simple project from university I was working on,, it was about Super Market Billing System. So I started working on but every time I give input as a user the program crashes

#include<stdio.h>
#include<conio.h>
void welcome();
void list();
int main()
{
welcome();
}
void welcome()
{
int ch;
printf("\t\t\tSuper Market\n");
printf("Make Your choice\n");
printf("1.Go Shopping\n2.Check Bill\n3.Check Offers");
scanf("%d",ch);
switch (ch)
{
case 1: list();
default : printf("wrong choice");
}
}
void list()
{
system("cls");
printf("1.Item First");
getch();
}


What am I doing wrong?

Answer

You forget to give &

convert this

scanf("%d",ch);

to this

scanf("%d",&ch);

Suggestion:

you need to use break in your case. Otherwise, control flow will "fall through" into succeeding cases

case 1: 
   list();
   break;