Compilation Error in C program in CodeBlocks - c

I intend to use a C program which would povide Banking Services including
Adding a Bank Account
Update Information of existing account
Viewing a Customer's past transactions
Viewing details of an account
Deleting a Customer's Account
Viewing existing accounts
For this I have made functions to be called at appropriate places namely(in order of service listed above),
menu() – This function displays the menu or welcome screen to perform different banking activities mentioned below.
new_acc() – This function creates a new customer account. It asks for some personal and banking details of the customer such as name, date of birth, citizenship number, address and phone number. You can enter the amount to deposit and choose one type of deposit account – saving, current, fixed for 1 year, fixed for 2 years or fixed for 3 years.
view list() – With this function, you can view the customer’s banking information such as account number, name, address and phone number provided while creating the account.
edit() – This function has been used for changing the address and phone number of a particular customer account.
transact() – With this function, you can deposit and withdraw money to and from a particular customer account.
erase() – This function is for deleting a customer account.
see() – This function shows account number, name, date of birth, citizenship number, age, address, phone number, type of account, amount deposited and date of deposit. It also displays the amount of interest corresponding to a particular account type.
File handling has been used for almost all functions. File has been used to store data related to new account, transaction, editing of account information and viewing of account information. I haven’t used file handling for the menu, interest calculation and password.
NOTE: Password to access the program is g5rox
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main_exit;
void menu();
struct date
{
int month,day,year;
};
struct {
char name;
int acc_no,age;
char address[60];
char aadhar[12];
double phone[10];
char acc_type[10];
float amt;
struct date dob;
struct date deposit;
struct date withdraw;
}add,upd,check,rem,transaction;
float interest(float t,float amount,int rate)
{
float SI;
SI=(rate*t*amount)/100.0;
return (SI);
}
void fordelay(int j)
{ int i,k;
for(i=0;i<j;i++)
k=i;
}
void new_acc()
{
int choice;
FILE *ptr;
ptr=fopen("record.dat","a+");
account_no:
system("cls");
printf("\t\t\t\xB2\xB2\xB2\xB2 ADD RECORD \xB2\xB2\xB2\xB2");
printf("\n\n\nEnter today's date(mm/dd/yyyy):");
scanf("%d/%d/%d",&add.deposit.month,&add.deposit.day,&add.deposit.year);
printf("\nEnter your #Account number:");
scanf("%d",&check.acc_no);
while(fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if (check.acc_no==add.acc_no)
{printf("Sorry, Account no. already in use!");
fordelay(1000000000);
goto account_no;
}
}
add.acc_no=check.acc_no;
printf("\nEnter your #Name:");
scanf("%s",&add.name);
printf("\nEnter the #DateOfBirth(mm/dd/yyyy):");
scanf("%d/%d/%d",&add.dob.month,&add.dob.day,&add.dob.year);
printf("\nEnter the #Age:");
scanf("%d",&add.age);
printf("\nEnter the #Address:");
scanf("%s",&add.address);
printf("\nEnter your #Aadhar number:");
scanf("%s",&add.aadhar[12]);
printf("\nEnter the #Mobile number: ");
scanf("%lf",&add.phone);
printf("\nEnter the #Amount to deposit:Rs");
scanf("%f",&add.amt);
printf("\nType of account:\n\t#Saving\n\t#Current\n\t#Fixed1(for 1 year)\n\t#Fixed2(for 2 years)\n\t#Fixed3(for 3 years)\n\n\tEnter your choice:");
scanf("%s",&add.acc_type);
//
fprintf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
fclose(ptr);
printf("\nKudos! Account created successfully!");
add_invalid:
printf("\n\n\n\t\tEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if(main_exit==0)
close();
else
{
printf("\nInvalid!\a");
goto add_invalid;
}
}
void view_list()
{
FILE *view;
view=fopen("record.dat","r");
int test=0;
system("cls");
printf("\nACC. NO.\tNAME\t\t\tADDRESS\t\t\tPHONE\n");
while(fscanf(view,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
printf("\n%6d\t %10s\t\t\t%10s\t\t%.0lf",add.acc_no,add.name,add.address,add.phone);
test++;
}
fclose(view);
if (test==0)
{ system("cls");
printf("\nNO RECORDS!!\n");}
view_list_invalid:
printf("\n\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if(main_exit==0)
close();
else
{
printf("\nInvalid!\a");
goto view_list_invalid;
}
}
void edit(void)
{
int choice,test=0;
FILE *old,*newrec;
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
printf("\nEnter the account no. of the customer whose info you want to change:");
scanf("%d",&upd.acc_no);
while(fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,&add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if (add.acc_no==upd.acc_no)
{ test=1;
printf("\nWhat information do you want to change?\n1.Address\n2.Phone\n\nEnter your choice(1 for address and 2 for phone):");
scanf("%d",&choice);
system("cls");
if(choice==1)
{printf("Enter the new address:");
scanf("%s",upd.address);
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,upd.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
system("cls");
printf("Changes saved!");
}
else if(choice==2)
{
printf("Enter the new phone number:");
scanf("%lf",&upd.phone);
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,upd.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
system("cls");
printf("Changes saved!");
}
}
else
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test!=1)
{ system("cls");
printf("\nSorry, Record not found!!\a\a\a");
edit_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
edit();
else
{printf("\nInvalid!\a");
goto edit_invalid;}
}
else
{printf("\n\n\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
}
void transact(void)
{ int choice,test=0;
FILE *old,*newrec;
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
printf("Enter the account no. :");
scanf("%d",&transaction.acc_no);
while (fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if(add.acc_no==transaction.acc_no)
{ test=1;
if(strcmpi(add.acc_type,"fixed1")==0||strcmpi(add.acc_type,"fixed2")==0||strcmpi(add.acc_type,"fixed3")==0)
{
printf("\a\a\a\n\nSorry, but YOU CANNOT DEPOSIT OR WITHDRAW CASH IN FIXED ACCOUNTS!!!!!");
fordelay(1000000000);
system("cls");
menu();
}
printf("\n\nDo you want to\n1.Deposit\n2.Withdraw?\n\nEnter your choice(1 for deposit and 2 for withdraw):");
scanf("%d",&choice);
if (choice==1)
{
printf("Enter the amount you want to deposit:Rs ");
scanf("%f",&transaction.amt);
add.amt+=transaction.amt;
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
printf("\n\nKudos! Deposited successfully!");
}
else
{
printf("Enter the amount you want to withdraw:Rs ");
scanf("%f",&transaction.amt);
add.amt-=transaction.amt;
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
printf("\n\nKudos! Withdrawn successfully!");
}
}
else
{
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
}
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test!=1)
{
printf("\n\nSorry, Record not found!!");
transact_invalid:
printf("\n\n\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==0)
transact();
else if (main_exit==1)
menu();
else if (main_exit==2)
close();
else
{
printf("\nInvalid!");
goto transact_invalid;
}
}
else
{
printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
}
void erase(void)
{
FILE *old,*newrec;
int test=0;
old=fopen("record.dat","r");
newrec=fopen("new.dat","w");
printf("Enter the #account_no. you want to delete:");
scanf("%d",&rem.acc_no);
while (fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if(add.acc_no!=rem.acc_no)
fprintf(newrec,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
else
{test++;
printf("\nKudos! Record deleted successfully!\n");
}
}
fclose(old);
fclose(newrec);
remove("record.dat");
rename("new.dat","record.dat");
if(test==0)
{
printf("\nSorry, Record not found!!\a\a\a");
erase_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
erase();
else
{printf("\nInvalid!\a");
goto erase_invalid;}
}
else
{printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else
close();
}
}
void see(void)
{
FILE *ptr;
int test=0,rate;
int choice;
float time;
float intrst;
ptr=fopen("record.dat","r");
printf("Do you want to check with\n#1 Account no\n#2 Name\nEnter your choice:");
scanf("%d",&choice);
if (choice==1)
{ printf("Enter the #account_number:");
scanf("%d",&check.acc_no);
while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if(add.acc_no==check.acc_no)
{ system("cls");
test=1;
printf("\n#Account NO.:%d\n#Name:%s \n#DOB:%d/%d/%d \n#Age:%d \n#Address:%s \n#Aadhar No:%s \n#Phone number:%.0lf \n#Type Of Account:%s \n#Amount deposited:$ %.2f \n#Date Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,
add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
if(strcmpi(add.acc_type,"fixed1")==0)
{
time=1.0;
rate=9;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
}
else if(strcmpi(add.acc_type,"fixed2")==0)
{
time=2.0;
rate=11;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d of every month. Cheers!",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
printf("\n\nSorry, You will get no interest.\a\a");
}
}
}
}
else if (choice==2)
{ printf("Enter the #name:");
scanf("%s",&check.name);
while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year)!=EOF)
{
if(strcmpi(add.name,check.name)==0)
{ system("cls");
test=1;
printf("\n#Account No.:%d\n#Name:%s \n#DOB:%d/%d/%d \n#Age:%d \n#Address:%s \n#Aadhar No:%s \n#Phone number:%.0lf \n#Type Of Account:%s \n#Amount deposited:$%.2f \n#Date Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.aadhar,add.phone,
add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
if(strcmpi(add.acc_type,"fixed1")==0)
{
time=1.0;
rate=9;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
}
else if(strcmpi(add.acc_type,"fixed2")==0)
{
time=2.0;
rate=11;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d. Cheers!",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
printf("\n\n You will get $.%.2f as interest on %d of every month. Cheers!",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
printf("\n\nSorry, You will get no interest\a\a");
}
}
}
}
fclose(ptr);
if(test!=1)
{ system("cls");
printf("\nRecord not found!! Please Check again\a\a\a");
see_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
see();
else
{
system("cls");
printf("\nInvalid!\a");
goto see_invalid;}
}
else
{printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);}
if (main_exit==1)
{
system("cls");
menu();
}
else
{
system("cls");
close();
}
}
void close(void)
{
printf("\n\n\n\nThis Program is developed by Group#5|C1");
}
void menu(void)
{ int choice;
system("cls");
system("color 2");
printf("\n\n\t\t\t\tVISHWAKARMA BANKING SERVICES");
printf("\n\n\n\t\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 >| WELCOME TO THE MAIN MENU |< \xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\n\n\t\t1.Create new account\n\t\t2.Update information of existing account\n\t\t3.For transactions\n\t\t4.Check the details of existing account\n\t\t5.Removing existing account\n\t\t6.View customer's list\n\t\t7.Exit\n\n\n\n\n\t\t Enter your choice:");
scanf("%d",&choice);
system("cls");
switch(choice)
{
case 1:new_acc();
break;
case 2:edit();
break;
case 3:transact();
break;
case 4:see();
break;
case 5:erase();
break;
case 6:view_list();
break;
case 7:close();
break;
}
}
int main()
{
char pass[10],password[10]="g5rox";
int i=0;
printf("\n\n\t\tEnter the #password to login:");
scanf("%s",pass);
if (strcmp(pass,password)==0)
{printf("\n\nPassword Match!\n\n\nLoading Program");
for(i=0;i<=6;i++)
{
fordelay(100000000);
printf(".");
}
system("cls");
menu();
}
else
{ printf("\n\nOops! Wrong password!!\a\a\a");
login_try:
printf("\nEnter 1 to try again and 0 to exit:");
scanf("%d",&main_exit);
if (main_exit==1)
{
system("cls");
main();
}
else if (main_exit==0)
{
system("cls");
close();}
else
{printf("\nInvalid!");
fordelay(1000000000);
system("cls");
goto login_try;}
}
return 0;
}
This program gives an compilation error in the midst when using any one of the services without providing any reason. I am unable to understand it either.
Compilation Error
A Detailed Soultion would be highly appreciated. Thanks!

You need to re-write this:
while(fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
//...
}
If fscanf() returns EOF that means the operation encountered an error, so your loop is terminated only when reading triggers an error.
Attempting to read "foobar" into an int variable is not an error, it's a matching failure.
-- added example ----
Eg... when the input buffer contains something other than a digit, this is an infinite loop
// assume input buffer contains "14/42\n"
unsigned sum = 0, i, j;
while (scanf("%u%u", &i, &j) != EOF) {
sum += i + j;
}
The first time through the loop, scanf() assigns 14 to i, meets the slash in input which triggers a matching failure and so returns with value 1. 1 is not EOF so the body of the loop executes (j is still unininitialized!!!) and the loop repeat with "/42" in the input buffer.
The 2nd time through the loop, the matching failure occurs with the very first character scanf() sees: the slash is not a valid character for "%u" so scanf return a value of 0. 0 is not EOF so the loop goes on forever.
Reading with fgets() consumes all of the input
char buf[100]; // large enough
unsigned sum = 0, i, j;
while (fgets(buf, sizeof buf, stdin)) {
// if (parse(buf, &i, &j) == 0) {
// sum += i + j;
// } else {
// printf(stderr, "error in input\n");
// }
}

Related

The following C code shows that : format '%s' expects argument of type 'char *', but argument 3 has type int

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.

C Language String copy in if else stament

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ticket,tnotran,tran;
char pass[40],desti[2],cla,destina[20],clana[20];
float tfare,cfare,nfare,gtnfare;
int main(int argc, char *argv[]) {
printf("\n\n\t\tTicket NO.: ");
scanf("%d",&ticket);
while (ticket != 0){
head();
con();
system("pause");
system("cls");
netcomp();
out();
system("pause");
system("cls");
printf("\n\n\t\tTicket NO.: ");
scanf("%d",&ticket);
}
printf("\n\n\t\tTotal No. of Transactions: %d", tran);
printf("\n\n\t\tGrand Total of Net Fare: %f", gtnfare);
}
head(){
printf("\n\t\tPassenger's Name: ");
scanf("%s",&pass);
printf("\n\t\tDestination Code: ");
scanf("%s",&desti);
printf("\n\t\tClass Code: ");
scanf("%s",&cla);
}
con(){
if(strcpy(desti, "HK")||strcpy(desti, "hk")){
tfare= 8500;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Hong Kong");
}
else if(strcpy(desti, "BK")||strcpy(desti, "bk")){
tfare= 7500;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Bangkok");
}
else if(strcpy(desti, "KL")||strcpy(desti, "kl")){
tfare= 7900;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Kualala Lumpur");
}
else if(strcpy(desti, "TP")||strcpy(desti, "tp")){
tfare= 8300;
printf("\n\t\tTicket Fare: %.2f", tfare);
strcpy(destina,"Taipe");
}
else{
printf("\n\t\tInvalid Input");
tfare=0;
}
if(cla== 'E'|| cla=='e'){
cfare= 1500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"Economy");
}
else if(cla== 'T'|| cla=='t'){
cfare= 2500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"Tourist");
}
else if(cla== 'F'|| cla=='f'){
cfare= 3500;
printf("\n\t\tClass Fare: %.2f", cfare);
strcpy(clana,"First Class");
}
}
netcomp(){
nfare = tfare+ cfare;
gtnfare +=nfare;
tran += 1;
}
out(){
printf("\n\n\t\tTicket NO.: %d", ticket);
printf("\n\t\tPassenger's Name: %s", pass);
printf("\n\t\tDestination: %s", destina);
printf("\n\t\tClass: %s", clana);
printf("\n\t\tNet Fare: %f", nfare);
}
I was creating a program that is suppose to be a travelling payment with fares and class and totals as such. it runs now the only problem is the if else statement in destination which is i require 2 characters, when i enter the other destination codes e.g kl,bk or tp it only outputs the statement in hk not the rest
You probably want to replace strcpy with strcmp
strcpy(desti, "HK") will copy the text "HK" into desti the function will succeed and since strcpy returns a pointer to the destination it will be a non-zero value which wil execute the first if statement everytime.
The man page of strcpy
Note that strcmp returns 0 when they are equal.
More information on strcmp
Also note that
You cannot do strcpy(desti, "HK"); into a char desti[2], because you didn't allocate room for the null terminator. You would have to do char desti[3]; if you wish to use it as a string.

Why my C program crash

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.

Debug assertion failed for editing and deleting records

I am a relatively new programmer currently learning the C language at college. For my project I have been tasked to design a library system for students, I have been experiencing a debug assertion failed-error whenever I try to delete the same book that has been recently edited. Can somebody help me on this question?
PS: I have to submit the program in a few days' time, so I need some help! D:
Here's my code for editing books:
void EditBookInformation()
{
int found = 0;
char user_input[5];
system("cls");
printf("You have selected Edit Book Information \n");
printf("Please enter the Book ID to edit the book: \n");
scanf_s(" %s", &user_input, 5);
fflush(stdin);
fopen_s(&BookList, "record.txt", "rb+");
fopen_s(&BookList2, "newrecord.txt", "wb+");
fopen_s(&Logsheet, "log.txt", "a+");
rewind(BookList);
rewind(BookList2);
while (fread(&Books, sizeof(Books), 1, BookList) != NULL)
{
if (strcmp(user_input, Books.Book_ID) == 0)
{
found = 1;
printf("Book has been found! \n");
printf("\nBook ID:%s \n", Books.Book_ID);
printf("Title:%s \n", Books.Title);
printf("Edition:%s \n", Books.Edition);
printf("Year of publication:%s \n", Books.Year_of_publication);
printf("Shelf location:%s \n", Books.Shelf_location);
printf("Price in RM:%s \n", Books.Price);
break;
}
else
{
fwrite(&Books, sizeof(Books), 1, BookList2);
}
}
if (!found)
{
printf("Book not found! \n");
_fcloseall();
system("pause");
system("cls");
main();
}
char confirm;
printf("\nDo you want to edit this book?");
scanf_s("%c", &confirm);
fflush(stdin);
if (confirm == 'y')
{
printf("New Title:");
scanf_s("%[^\n]s", &Books.Title, 50);
while (getchar() != '\n');
printf("New Edition:");
scanf_s("%s", &Books.Edition, 6);
while (getchar() != '\n');
NewYearInput:
printf("New Year of publication:");
scanf_s("%s", &Books.Year_of_publication, 5);
while (getchar() != '\n');
int length = strlen(Books.Year_of_publication);
int digit = 0;
if (length == 4)
{
for (; digit < length; digit++)
if (!isdigit(Books.Year_of_publication[digit]))
break;
}
if (digit != 4)
{
printf("Wrong input! Please enter 4 digits for year! \n");
system("pause>nul");
goto NewYearInput;
}
printf("New Shelf Location:");
scanf_s("%s", &Books.Shelf_location, 5);
while (getchar() != '\n');
printf("New Price in RM:");
scanf_s("%s", &Books.Price, 5);
while (getchar() != '\n');
fprintf(Logsheet, "Book edited: %s \n", Books.Title);
fseek(BookList, ftell(BookList) -sizeof(Books), SEEK_SET);
fwrite(&Books, 1, sizeof(Books), BookList);
fclose(BookList);
fclose(Logsheet);
printf("Book has been edited! \n");
printf("Press any key to return to main menu \n");
system("pause>nul");
system("cls");
main();
}
else if (confirm == 'n')
{
printf("You have cancelled your operation! \n");
_fcloseall();
system("cls");
main();
}
}
And here is my code for deleting books:
void DeleteBookByBookID()
{
int found = 0;
char user_input1[5];
system("cls");
printf("You have selected Delete Book \n");
printf("Please enter the Book ID that you want to delete the book \n");
scanf_s("%s", &user_input1, 5);
fflush(stdin);
fopen_s(&BookList, "record.txt", "rb+");
fopen_s(&BookList2, "newrecord.txt", "wb+");
fopen_s(&Logsheet, "log.txt", "a+");
rewind(BookList);
rewind(BookList2);
while (fread(&Books, sizeof(Books), 1, BookList) != NULL)
{
if (strcmp(user_input1, Books.Book_ID) == 0)
{
printf("Book found! \n");
found = 1;
}
else
{
fwrite(&Books, sizeof(Books), 1, BookList2);
}
}
if (!found)
{
printf("Book ID not found! \n");
_fcloseall();
system("pause");
system("cls");
main();
}
fclose(BookList);
fclose(BookList2);
char confirm;
printf("Do you want to delete this book? \n");
scanf_s(" %c", &confirm);
fflush(stdin);
if (confirm == 'y')
{
remove("record.txt");
rename("newrecord.txt", "record.txt");
fprintf(Logsheet, "Book deleted: %s \n", Books.Title);
printf("Book has been deleted! \n");
printf("Press any key to return to main menu \n");
system("pause>nul");
system("cls");
main();
}
else if (confirm == 'n')
{
printf("You have cancelled your operation! \n");
fclose(BookList);
fclose(BookList2);
system("cls");
main();
}
fclose(Logsheet);
}
If a book is not found, you are calling main. But main should not be called by the user. It is called by the system and is the start of your program. Maybe you mean return?
You are reading from BookList and if the record read is not the one you want, you write it to BookList2. Once the record is found, you edit it and then write it to BookList2 and then you close the files and rename the new file to the old file. But what happens to all the remaining records in the old file? Shouldn't you copy those too to the new file?
See also the comments to your post for other helpful sugestions.

How to accept space in string in C in the given code

Sorry about the previous code, I did not correctly interpreted the situation.
What's happening is the string is being stored with spaces but when i am trying to read it afterwards, The output flickers continuously (its like its reading it over and over again and printing it infinitely on itself.)
The records are stored in a file named record.dat
I am inserting image showing file contents stored after I input a new record in the file (Everything gets stored correctly here)
![These are the contents of record.dat right after i insert the new record]--> the image --> http://i.stack.imgur.com/YxGEd.png
and now when I try to look the details of this record on my application I get this.
![Output showing the record details but not entirely correct as " Finch" is missing] -- >> the image -- >> http://i.stack.imgur.com/zgmGY.png
and after this if i close the application and then start it again and try to read the previous data it shows this:
![This output is also flickering like before but now even the data is gone]-->> the image -->> http:// i.stack.imgur.com/ 4q0qb.png
(and just in case you are wondering -- the data is still there in the record.dat file -- all of it which i entered when i prevously created the record.)
I hope the problem is more clear now.....
void see(void)
{
FILE *ptr;
int test=0,rate;
int choice;
float time;
float intrst;
char c;
ptr=fopen("record.dat","r");
printf("Do you want to check by\n1.Account no\n2.Name\nEnter your choice:");
//Selection Choice with Validation
int once = 0;
do
{
if(once!=0)
{
printf("\nThe choice is invalid.\nEnter 1 for account number and 2 for name");
}
once = once+1;
} while ((scanf("%d%c", &choice, &c)!=2 || c!='\n') && clean_stdin());
if (choice==1)
{
//Account Number with Validation
once = 0;
do
{
if(once!=0)
{
printf("\nThe above account number is invalid.\nEnter an account number (numeric value only):");
}
else
{
printf("\nEnter an account number:");
}
once = once+1;
} while ((scanf("%d%c", &check.acc_no, &c)!=2 || c!='\n') && clean_stdin());
while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if(add.acc_no==check.acc_no)
{ system("cls");
test=1;
printf("\nAccount NO.:%d\nName:%s \nDOB:%d/%d/%d \nAge:%d \nAddress:%s \nCitizenship No:%s \nPhone number:%.0lf \nType Of Account:%s \nAmount deposited:$ %.2f \nDate Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,
add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
if(strcmpi(add.acc_type,"fixed1")==0)
{
time=1.0;
rate=9;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
}
else if(strcmpi(add.acc_type,"fixed2")==0)
{
time=2.0;
rate=11;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
printf("\n\nYou will get no interest\a\a");
}
}
}
}
else if (choice==2)
{
printf("Enter the name:");
scanf("%s",&check.name);
while (fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
{
if(strcmpi(add.name,check.name)==0)
{ system("cls");
test=1;
printf("\nAccount No.:%d\nName:%s \nDOB:%d/%d/%d \nAge:%d \nAddress:%s \nCitizenship No:%s \nPhone number:%.0lf \nType Of Account:%s \nAmount deposited:$%.2f \nDate Of Deposit:%d/%d/%d\n\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,
add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
if(strcmpi(add.acc_type,"fixed1")==0)
{
time=1.0;
rate=9;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+1);
}
else if(strcmpi(add.acc_type,"fixed2")==0)
{
time=2.0;
rate=11;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+2);
}
else if(strcmpi(add.acc_type,"fixed3")==0)
{
time=3.0;
rate=13;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d/%d/%d",intrst,add.deposit.month,add.deposit.day,add.deposit.year+3);
}
else if(strcmpi(add.acc_type,"saving")==0)
{
time=(1.0/12.0);
rate=8;
intrst=interest(time,add.amt,rate);
printf("\n\nYou will get $.%.2f as interest on %d of every month",intrst,add.deposit.day);
}
else if(strcmpi(add.acc_type,"current")==0)
{
printf("\n\nYou will get no interest\a\a");
}
}
}
}
fclose(ptr);
if(test!=1)
{ system("cls");
printf("\nRecord not found!!\a\a\a");
see_invalid:
printf("\nEnter 0 to try again,1 to return to main menu and 2 to exit:");
scanf("%d",&main_exit);
system("cls");
if (main_exit==1)
menu();
else if (main_exit==2)
close();
else if(main_exit==0)
see();
else
{
system("cls");
printf("\nInvalid!\a");
goto see_invalid;}
}
else
{printf("\nEnter 1 to go to the main menu and 0 to exit:");
scanf("%d",&main_exit);}
if (main_exit==1)
{
system("cls");
menu();
}
else
{
system("cls");
close();
}
}
The recommended solution is not to use scanf since scanf is also vulnerable to buffer overflows. In any case, you are not actually doing any formatting with the scanf call. You can use fgets to read the input into a string and specify the maximum number of characters that you allow. Use something like the following:
printf("\nEnter the name: ");
fgets(add.name, MAX_NAME_SZ, stdin);
You can use the %[^...] format specifier to read spaces into a string. Here's an example program that hopefully gives you something to work from.
#include <stdio.h>
int main()
{
char const* line = "This is a string, 10";
char text[50];
int number;
// Read up to 49 characters not including ',' to text.
sscanf(line, "%49[^,], %d", text, &number);
printf("%s, %d\n", text, number);
return 0;
}
Output:
This is a string, 10
I think you should provide an example of a failing situation or at least clean up the code and only give us the buggy part.
I see a lot of printf use without \n at the end which I know can be a problem (which may aswell be completely out of the subject here).
Since I've never used scanf I can't tell where your error is but maybe if you reduced your code to the acquisition of the user's input (-> the scanf part) then an attempt to display it (-> the part where your string gets truncated as it meets the first space) we could easily help you fix that problem faster and would be nicer to read.

Resources