I am using "dev cpp" and i'm writing some codes in c
While I'm running the code below, after entering all data in the function 1, the exe file just crash.
there are not error shown before i finish entering all the data ad press enter
What is happening?
// hotel system *work in progress*//
#include<stdio.h>
#include<stdlib.h>
struct book
{
int bookno[20];
char travellername[20];
char destination[20];
char hotelname[20];
char checkin[20];
char checkout[20];
int guestno[20];
char type[20];
float fee;
}b;
void add();//Add new booking
void all(); //view all booking
//void mod(); modify booking
//void search(); search booking
//void del(); delete booking
void main()
{
int choose;
do{
printf("\n *** Welcome to Hong Kong Hotek booking Record and Management System 2017 ***\n");
printf("\n *** This system is developed by CCIT4020 Class No.NL-?? Group No.?? ***");
printf("\n\n\n--<Basic functions>-- \n");
printf("\n1. Add New Hotel Booking Record(s): \n");
printf("\n2. Display All Hotel Booking Records: \n");
printf("\n3. Modify Hotel Booking Record(s): \n");
printf("\n4. Search Hotel Booking Record(s): \n");
printf("\n5. Delete Hotel Booking Record(s): \n");
printf("\n0. Quit: \n");
printf("\nWhat is your option (0-5)? ");
scanf("%d",&choose);
switch (choose)
{
case 1 :
add();
break;
case 2:
all();
break;
//case 3:
// mod();
//break;
//case 4:
// search();
//break;
//case 5:
// del();
//break;
case 0:
exit(0);
break;
default:
printf("Invalid choice! Please enter again!");
break;
}
}while(choose!=0);
}
void add()
{
FILE *fp;
struct book b;
printf("Hotel Booking number: ");
scanf("%s",b.bookno);
printf("Name of Traveller: ");
scanf("%s",b.travellername);
printf("Destination: ");
scanf("%s",b.destination);
printf("Name of Hotel: ");
scanf("%s",b.hotelname);
printf("Check-in Schedule: ");
scanf("%s",b.checkin);
printf("Check-out Schedule: ");
scanf("%s",b.checkout);
printf("Number of Guests: ");
scanf("%s",b.guestno);
printf("Room Type: ");
scanf("%s",b.type);
printf("Total Fee: ");
scanf("%s",b.fee);
fp=fopen("data.txt","a");
if(fp == NULL)
{
printf("There are no data file! please create one!");
}
else
{
fprintf(fp,"%s \n %s \n %s \n %s \n %s \n %s \n %s \n %s \n %s",b.bookno,b.travellername,b.destination,b.hotelname,b.checkin,b.checkout,b.guestno,b.type,b.fee);
printf("One Record Added!");
}
printf("\n");
fclose(fp);
}
void all()
{
char choose;
FILE *fp;
fp = fopen("data.txt","r");
if(fp == NULL)
{
printf("There are no data file!");
exit(1);
}
else
{
system("clear");
while( ( choose = fgetc(fp) ) != EOF )
printf("%c",choose);
}
fclose(fp);
}
Read your compiler messages:
The problem is here:
scanf("%s",b.fee);
The format specifier is %s, but b.fee is a float.
You need this:
scanf("%f", &b.fee);
There are likely more problems like that one. Check them out by yourself. Each scanfformat specifier must match the variable.
Related
I am a beginner in C and I'm facing this error in Xcode when I'm writing my C code. I've seen the other questions here on StackOverflow but it doesn't seem to solve my problem. I think it has something to do with linking libraries but I'm not sure how to do that.
This is my code below:
#include<stdio.h>
#include<stdlib.h>
void add_task(void);
void view_task(void);
void manage_task(void);
int main()
{
char main_choice;
system("cls");
printf("Welcome to your Task Management Sytem\n");
printf("What would you like to do today?\n A: Add New Task\n B: View Task \n C: Manage Tasks\n");
printf("\nEnter your choice:");
scanf("%c", &main_choice);
add_task();
view_task();
manage_task();
switch(main_choice)
{
case 'A':
{
printf("\n--------------ADD A NEW TASK-------------");
add_task();
break;
}
case 'B':
{
printf("\n--------------VIEW A TASK-------------");
view_task();
break;
}
case 'C':
{
printf("\n--------------MANAGE TASKS-------------");
manage_task();
break;
}
default:
printf("INVALID INPUT");
break;
}
}
void add_task()
{
char name[20];
char category[20];
char info[20];
char date[20];
char status[20];
printf("\nTo Add a new task enter the details below\n");
printf("Name of Task:");
scanf(" %s", name);
printf("Category of Task:");
scanf(" %s", category);
printf("Information about task:");
scanf(" %s", info);
printf("Due Date of Task(dd/mm/yyyy):");
scanf(" %s", date);
printf("Status of Task\n TD = To-Do\n IP = In Progress\n CT = Completed Task\nEnter Status:");
scanf(" %s", status);
}
void view_task()
{
char task_choice;
printf("View Tasks by:\nA:Due Date\nB:Category\nC:Status\nD:View All Tasks");
scanf(" %c", &task_choice);
if (task_choice == 'A')
{
char date_choice;
printf("A:View Tasks in ascending order\nB: View Tasks in descending order\nEnter your choice:");
scanf(" %c", &date_choice);
}
}
void manage_task()
{
printf("What do you want to change");
}
.........................................................................................
Something is wrong with the functionality of the code. As in the screenshot below it'skipping storing some information for the employees. Hence it makes it incomplete. I can't fathom what's the underlining issue.
This is a screenshot of how it displays:
Only the name, sex and address are been stored. Sometimes it randomly stores all information.
A look at the code:
printf("\nEnter designation of the employee: ");
scanf("%s", e.dsgn);
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%d", &e.empID);
fputs(e.name, fptr);
fputs(&e.sex, fptr);
fputs(e.adrs, fptr);
fputs(e.dsgn, fptr);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
The struct:
struct employee
{
char name[50];
char sex{7};
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
tolower()
};
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void exit();
FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
exit(1);
}
}
//Explain the reason for this?
//recsize = (long int) sizeof(e);//
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
fflush(stdin);
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
exit(1);
break;
default:
puts("Choice is incorrect!!");
break;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
printf("\nEnter the sex of the employee (M/m or F/f): ");
scanf("%c",&e.sex);
switch(e.sex)
{
case 'M':
case 'm':
printf("\nMale.\n");
break;
case 'F':
case 'f':
printf("\nFemale.\n ");
break;
default:
printf("Unspecified Sex.");
}
printf("\nEnter the address of the employee: ");
scanf( "%s", e.adrs);
printf("\nEnter designation of the employee: ");
scanf("%s", e.dsgn);
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%d", &e.empID);
fputs(e.name, fptr);
fputs(&e.sex, fptr);
fputs(e.adrs, fptr);
fputs(e.dsgn, fptr);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
// fwrite(&e,recsize,1,fptr);
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
//fflush(stdin);//
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while( tolower(next) != 'n' );
fclose(fptr);
}
void list ()
{
printf("-------------------------------");
printf("\nEmployee Details: \n---------------------------------\n");
printf("Name : %s\n",e.name);
printf("Address : %s\n",e.adrs);
printf("Sex : %c\n",e.sex);
printf("Designation : %s\n",e.dsgn);
printf("Age : %d\n",e.age);
printf("Salary : %.2f\n",e.slry);
printf("Employee-ID : %d\n",e.empID);
}
void edit ()
{
char next;
do
{
printf("Enter the employee name to be edited: ");
scanf("%49[^\n]", empname);
while(fread(&e,recsize,1,fptr)==1)
{
if(strcmp(e.name,empname) == 0)
{
printf("\nEnter new name,sex,address,designation,age,salary,employee ID ");
scanf("%s %c %s %s %d %f %d",e.name,&e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
fseek(fptr,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fptr);
break;
}
}
printf("\nEdit another record(y/n)");
next = getche();
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb");
while(fread(&e,recsize,1,fptr) == 1)
{
if(strcmp(e.name,empname) != 0)
{
fwrite(&e,recsize,1,ftemp);
}
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt");
rename("Temp.dat","ems.txt");
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
int ch; while( ( ch = getchar() ) != EOF && ch != '\n' ){;}
next = getche();
}while( tolower(next) != 'n' );
}
The scanf("%s", ...) will just take the string value till the next whitespace, rest are truncated. To avoid that, better use fgets(var, sizeof(var), stdin).
printf("\nEnter the address of the employee: ");
scanf( "%s", e.adrs); // this
printf("\nEnter designation of the employee: ");
scanf("%s", e.dsgn); // this
To:
printf("\nEnter the address of the employee: ");
fseek(stdin, 0, SEEK_END); // ADD THIS TO AVOID SKIP
fgets(e.adrs, sizeof(e.adrs), stdin); // this
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin); // this
Notice that I've mentioned to add a line:
fseek(stdin, 0, SEEK_END);
This will prevent the the skip problem and is better than fflush(stdin) which tends to Undefined Behavior (UB).
Then you will get the correct output format:
Enter the name of the employee: ABC Someone
Enter the sex of the employee (M/m or F/f): m
Male.
Enter the address of the employee: Address 123
Enter designation of the employee: Manager
Enter age of the employee: 120
Enter basic salary of the employee: -10
Enter the employee's ID: 007
Do you want to input more? (y/n): n
An important note: The fseek() will not work on terminals. On a Unix system, if standard input is a terminal, the terminal is not a seek-able device, so the proposed fseek() would do nothing. If the input device was a file, it would, of course, seek to the end of the file.
The following code shows that format '%s' expects argument of type 'char *', but line 139 has type 'int'. I don't see any mistakes in variable types. This issue is present at the scanf. Along with this issue there are 3 more related ones. I keep trying and getting this very same error. I ask if someone can please kindly assist?
This is the struct:
struct employee
{
char name[50];
char sex;
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
This is the entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void ext();
FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];
int main()
{
//FILE * fptr, *ft;
int choice;
//fptr = fopen("ems.txt","rb+");
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
ext(1);
}
}
//Explain the reason for this?
//recsize = (long int) sizeof(e);//
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
fflush(stdin);
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
ext(1);
break;
default:
puts("Choice is incorrect!!");
continue;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
gets(e.name);
printf("\nEnter the sex of the employee (M/m or F/f): ");
gets(&e.sex);
printf("\nEnter the address of the employee: ");
gets(e.adrs);
printf("\nEnter designation of the employee: ");
gets(e.dsgn);
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%d", &e.empID);
fputs(e.name, fptr);
fputs(&e.sex, fptr);
fputs(e.adrs, fptr);
fputs(e.dsgn, fptr);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
// fwrite(&e,recsize,1,fptr);
fflush(stdin);
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while(next !='n');
fclose(fptr);
}
void list ()
{
/* what is going on here??? */
while(fread(&e,recsize,1,fptr)==1)
{
printf("\n%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID);
}
getche();
return ;
}
void edit ()
{
char next;
do
{
printf("Enter the employee name to be edited: ");
scanf("%s", empname);
while(fread(&e,recsize,1,fptr)==1)
{
if(strcmp(e.name,empname) == 0)
{
printf("\nEnter new name,sex,address,designation,age,salary,employee ID ");
scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
fseek(fptr,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fptr);
break;
}
}
printf("\nEdit another record(y/n)");
next = getche();
fflush(stdin);
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb");
while(fread(&e,recsize,1,fptr) == 1)
{
if(strcmp(e.name,empname) != 0)
{
fwrite(&e,recsize,1,ftemp);
}
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt");
rename("Temp.dat","ems.txt");
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
next = getche();
}while(next !='n');
}
You are not following the format properly, ie: while printing a character you are using %s, like: here printf("\n%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID); you are using %s for e.sex which is merely a character.
And used %s for taking char inputs like e.sex via scanf several times, like this: scanf("%s %s %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);, use %c instead.
Fix these formatting both while taking input and printing output in a formatted way and see.
You can take help from printf man page and scanf man page for details.
The following code shows that there is an undefined reference to 'ext'. I'm not very adept in C. I really need a solution. This issue is present at line 37. Then there are 2 related errors at line 80. One is the one I previously mentioned and the other: "error:1d returned 1 exit status." I keep trying and getting this very same thing. I ask if someone can please kindly assist?
First problem:
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
ext(1);
}
}
Second problem:
case 5:
puts("Exit was chosen");
ext(1);
break;
Structure here:
struct employee
{
char name[50];
char sex;
char adrs[50];
char dsgn[25];
int age,empID;
float slry;
};
Entire code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <stdbool.h>
#include <windows.h>
#include "struct.h"
void insert();
void list();
void edit();
void del();
void ext();
FILE * fptr, *ftemp;
struct employee e;
long int recsize;
char empname[50];
int main()
{
int choice;
fptr = fopen("ems.txt", "r+");
if (fptr == NULL)
{
printf("Can't find file! Attempting to create file... \n");
fptr = fopen("ems.txt","w+");
if(fptr == NULL)
{
printf("Can't create file. Exiting...");
ext(1);
}
}
//Explain the reason for this?
//recsize = (long int) sizeof(e);//
while(1)
{
printf("*******************************\n");
printf("\nEmployee management system");
printf("\n1. Insert employee information");
printf("\n2. List all employee information");
printf("\n3. Edit employee information");
printf("\n4. Delete employee information");
printf("\n5. Exit");
printf("\n\n*****************************\n");
printf("\n\n Enter your choice: ");
scanf("%d", &choice);
fflush(stdin);
switch(choice)
{
case 1:
puts("Insert was chosen");
insert();
break;
case 2:
puts("List was chosen");
list();
break;
case 3:
puts("Edit was chosen");
edit();
break;
case 4:
puts("Delete was chosen");
del();
break;
case 5:
puts("Exit was chosen");
ext(1);
break;
default:
puts("Choice is incorrect!!");
continue;
}
}
return 0;
}
void insert()
{
char next;
do
{
printf("********************************************************** \n");
printf("\nEnter the name of the employee: ");
fgets(e.name, sizeof(e.name), stdin);
printf("\nEnter the sex of the employee (M/m or F/f): ");
fgets(&e.sex, sizeof(e.sex), stdin);
printf("\nEnter the address of the employee: ");
fgets(e.adrs, sizeof(e.adrs), stdin);
printf("\nEnter designation of the employee: ");
fgets(e.dsgn, sizeof(e.dsgn), stdin);
printf("\nEnter age of the employee: ");
scanf("%d", &e.age);
printf("\nEnter basic salary of the employee: ");
scanf("%f", &e.slry);
printf("\nEnter the employee's ID: ");
scanf("%d", &e.empID);
fputs(e.name, fptr);
fputs(&e.sex, fptr);
fputs(e.adrs, fptr);
fputs(e.dsgn, fptr);
fprintf(fptr, "%d \n%f \n%d \n", e.age, e.slry, e.empID);
// fwrite(&e,recsize,1,fptr);
fflush(stdin);
printf("\nDo you want to input more? (y/n): ");
next = getche();
printf("\n");
}
while(next !='n');
fclose(fptr);
}
void list ()
{
/* what is going on here??? */
while(fread(&e,recsize,1,fptr)==1)
{
printf("\n%s %c %s %s %d %.2f %d",e.name,e.sex,e.adrs,e.dsgn,e.age,e.slry,e.empID);
}
getche();
return ;
}
void edit ()
{
char next;
do
{
printf("Enter the employee name to be edited: ");
scanf("%s", empname);
while(fread(&e,recsize,1,fptr)==1)
{
if(strcmp(e.name,empname) == 0)
{
printf("\nEnter new name,sex,address,designation,age,salary,employee ID ");
scanf("%s %c %s %s %d %f %d",e.name,&e.sex,e.adrs,e.dsgn,&e.age,&e.slry,&e.empID);
fseek(fptr,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fptr);
break;
}
}
printf("\nEdit another record(y/n)");
next = getche();
fflush(stdin);
}
while(next != 'n');
return ;
}
void del()
{
char next;
do
{
printf("\nEnter name of employee to delete: ");
scanf("%s",empname);
ftemp = fopen("Temp.dat","wb");
while(fread(&e,recsize,1,fptr) == 1)
{
if(strcmp(e.name,empname) != 0)
{
fwrite(&e,recsize,1,ftemp);
}
}
fclose(fptr);
fclose(ftemp);
remove("ems.txt");
rename("Temp.dat","ems.txt");
fptr = fopen("ems.txt", "rb+");
printf("Delete another record(y/n)");
fflush(stdin);
next = getche();
}while(next !='n');
}
Are you sure you've posted an [mcve]? You declare void ext();, which is equivalent to void ext(int), which is how you should have declared it, because the old empty () form predates the standards (it's ancient) and causes no end of confusions. So you've declared it, but you have not defined it anywhere. If it were extern void ext(int), then you would not get the compiler error, but you would probably get a linker error, as you haven't written the ext function and the linker won't be able to find it.
If you really intended to use the stdlib function exit, then you should remove the void ext() declaration and recompile.
Update related to additional comments entered while I was distractedly entering the above:
This is just an FYI. You can create aliases for function names, using preprocessor macros:
#define ext(errorCode) exit(errorCode)
#include <stdio.h>
#include <stdlib.h> //for the clear screen function
#include <string.h>
struct customer
{
int custID;
char custName[50];
char custAddress[100];
};
typedef struct customer c;
void load_menu(void);
void customers_menu(void);
void createNew(void); //initialize your file
void add_Customer(c c1[30]); //add a new record to the file
FILE *fp;
int main(void)
{
load_menu();
return 0;
}
void load_menu(void)
{
int choice;
do
{
printf("Customer Orders Main Menu. \n\n");
printf("Please enter your choice: \n");
printf("1. Customer's Menu \n");
printf("2. Orders Menu\n");
printf("3. Product Stock Menu\n");
printf("4. Exit\n");
printf("\n");
if (scanf("%d",&choice)==1)
{
switch(choice)
{
case 1: system ("cls");
customers_menu();
printf("\n");
break;
case 2: system ("cls");
orders_menu();
printf("\n");
break;
case 3: system ("cls");
stock_menu();
printf("\n");
break;
case 4: printf("Quitting program!\n");
break;
default: printf("Invalid choice! Please try again\n");
printf("\n");
break;
}
}
else
{
fflush(stdin);
printf("Characters are invalid, please enter a number: \n ");
choice=0;
}
}while((choice !=4));
}
void createNew(void)
{
FILE *fp;
fp=fopen("Customer.dat", "w");
if (fp==NULL)
printf("File creation failed! \n");
else
{
printf("File created! \n");
fclose(fp);
}
}
void add_Customer (c c1[30])
{
int i, n , cc=0;
FILE *fp;
fp=fopen("Customer.dat", "a");
system("cls");
if(fp==NULL)
{
printf("File Creation Failed!");
}
system("cls");
printf("Enter the number of Customers: ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Customer's ID (numbers only) : ");
scanf("%d", &c1[i].custID);
printf("Customer's Name : ");
gets(c1[i].custName);
printf("Customer's Address : ");
gets(c1[i].custAddress);
fwrite(&c1[i], sizeof(c), 1, fp);
}cc++;
fclose(fp);
}
void recordCount(c c1[30], int *count)
{
add_Customer(c1);
count=0;
count++;
}
void customers_menu(void)
{
int choice;
c c1[30];
int i;
do
{
printf("\n");
printf("Customers Menu \n\n");
printf("Please enter your choice: \n");
printf("1. Add Customer \n");
printf("2.\n");
printf("3.\n");
printf("4. Go back to Main Menu \n");
recordCount (c1, &i);
if (scanf("%d",&choice)==1)
{
switch(choice)
{
case 1: add_Customer(c1);
createNew();
printf("\n");
break;
case 2:
printf("\n");
break;
case 3:
printf("\n");
break;
case 4: printf("Going back to Main Menu\n");
system ("cls");
break;
default: printf("Invalid choice! Please try again\n");
printf("\n");
break;
}
}
else
{
fflush(stdin);
printf("Characters are invalid, please enter a number: \n ");
choice=0;
}
}while((choice !=4));
I have a problem since when I enter the Customers Menu it is staring to execute case 1 immediately (which still doesn't work properly). Can someone help me fix this error please because I tried everything I know and it is still in vain
I think your issue is that in customers_menu() you output the menu, but do not read the selection, instead you call recordCount() which directly calls addCustomer().
After addCustomer() we return the customers_menu() which then calls scanf() for the long gone menu.
A few other notes:
gets() is not good, I suggest you use scanf() (with %s) instead.
Doing a printf() then clearing the screen is a bit pointless.
Error messages should really go to stderr (fprintf(stderr,...)) rather than stdout (printf(...))
You code is a missing trailing }.
cc is added to, but not used.
This problem coming from if (scanf("%d",&choice)==1) because scanf will not return choice. If you enter valid answer (like number), then it returns 1 and switch case work with 1. I think that's the problem.
If you enter char instead of integer, scanf will return 0.