The 15th line is what I cant fix. Please be kind enough to look at my code and diagnose the issue. I am new to programming and will appreciate anyone pointing me in the right direction.
#include <stdio.h>
#include <stdlib.h>
#define TEAMS 200
#define RUNNERS 10000
#define LENGTH 20
#define TEAMSIZE 50
FILE *fptr;
fptr = fopen("myfile.txt","w");
void getdetails();
struct person {
char name[LENGTH];
int number;
int age;
int event;
float money;
float time;
}p;
struct team {
char tname[LENGTH];
int nummembers;
float money;
struct person members[TEAMSIZE];
}t;
int main() {
int c,flag=0,i=0,j,k=0;
printf("\n---------------------------------------------------");
printf("\n---------------------------------------------------");
printf("\nHeader Specification");
while(flag==0) {
printf("\n1.Individual Registration");
printf("\n2.Team Registration");
printf("\n3.Running Events");
printf("\n4.Donation Totals");
printf("\n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c) {
case 1:
printf("\n For Individual Registration");
printf("\n1.Early Registration");
printf("\n2.regular Registration");
int ch;
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch) {
case 1:
printf("\n For Early Registration");
i=i+1;
getdetails(i);
break;
case 2:
printf("\n For Early Registration");
i=i+1;
getdetails(i);
break;
default:
printf("\n not valid");
break;
}
break;
case 2:
printf("\n For Team Registration");
printf("\n Enter team name:");
scanf("%s",t.tname);
printf("\n Enter team participant number:");
scanf("%d",&t.nummembers);
k=k+1;
for(j=1;j<=t.nummembers;j++) {
getdetails(k);
}
break;
case 5:
flag=1;
break;
}
}
return 0;
}
void getdetails(int i) {
printf("Enter your name:");
scanf("%s",p.name);
printf("Enter your age:");
scanf("%d",&p.age);
printf("Enter the event:");
scanf("%d",&p.event);
printf("Enter the donation amount:");
scanf("%f",&p.money);
if(fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr,"\n%s register for\t%dk race\tand the number is%d.",p.name,p.event,i);
fclose(fptr);
}
A runtime-executable statement, like
fptr = fopen("myfile.txt","w");
cannot reside in global scope. It has to reside in block scope, i.e., in some function body.
Related
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)
i was trying to create a program that manages car fixing records,and everything was working fine until case 6 of this code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int day;
int month;
int year;
}date;
typedef struct{
char pie_name[50];
int pie_num;
float pie_price;
}pie_st;
typedef struct{
long car_num;
char car_type[20];
char car_colour[20];
char owner_name[50];
long phone_num;
}own_cars_st;
typedef struct{
int management_num;
long car_num;
date date_of_management;
int pieces_num;
pie_st pie_rec[1000];
float management_fees;
float total_price;
}management_st;
void check_file(FILE *ptf);
void add_car_rec();
void add_management_rec();
int delete_car_rec(long search);
int search_own(char owner[]);
int car_man(long search);
int main(){
int option,c=0,found=1;
long search;
char owner[50];
while(c==0)
{
printf("\nplease enter your choice:\n");
printf("1.add new car record\n");
printf("2.add new car management record\n");
printf("3.delete a car record by number\n");
printf("4.view all car numbers for a specific owner\n");
printf("5.display all management records\n");
printf("6.view a specific car's management records and the total cost\n");
printf("7.view the most used gear piece's number\n");
printf("8.view a list of all cars that had management after a specific date\n");
printf("9.view all cars with changed tires\n");
printf("10.exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
add_car_rec();
break;
case 2:
add_management_rec();
break;
case 3:
printf("please enter the number of the car you want to delete its number:\t");
scanf("%ld",&search);
found=delete_car_rec(search);
if(found==1)
printf("there's no record available for this number!\n");
else
printf("record has been deleted!\n");
break;
case 4:
printf("please enter the name of the owner:\t");
fflush(stdin);
scanf("%[^\n]s",owner);
found=search_own(owner);
if(found==1)
printf("owner was not found!\n");
break;
case 5:
break;
case 6:
printf("please enter the number of the car you want to view its management records:\t");
scanf("%ld",&search);
printf("7");
car_man(search);
printf("8");
if(found==1)
printf("this car number has no management records!\n");
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
c=10;
break;
default:
printf("please choose one of the available options\n");
}
}
return 0;
}
void add_car_rec(){
own_cars_st new_car,old_cars[100];
printf("please enter the car information:\n");
printf("car number:\t");
scanf("%ld",&new_car.car_num);
printf("car type:\t");
fflush(stdin);
scanf("%[^\n]s",new_car.car_type);
printf("car colour:\t");
fflush(stdin);
scanf("%[^\n]s",new_car.car_colour);
printf("owner name:\t");
fflush(stdin);
scanf("%[^\n]s",new_car.owner_name);
printf("phone number:\t");
scanf("%ld",&new_car.phone_num);
FILE *ptf;
int n=0,i,j,fc;
ptf=fopen("owners and cars.bin","ab+");
if(ptf!=NULL){
fseek(ptf,0,2);
fc=ftell(ptf);
rewind(ptf);
if(fc==0)
{
fwrite(&new_car,sizeof(own_cars_st),1,ptf);
fclose(ptf);
}
else{
while(1==fread(&old_cars[n],sizeof(own_cars_st),1,ptf))
{
n++;
}
for(i=0;i<n+1;i++)
{
if(new_car.car_num<old_cars[i].car_num)
{
for(j=n+1;j>i;j--)
{
old_cars[j]=old_cars[j-1];
}
break;
}
}
old_cars[i]=new_car;
fclose(ptf);
ptf=fopen("owners and cars.bin","wb");
check(ptf);
for(i=0;i<n+1;i++)
{
fwrite(&old_cars[i],sizeof(own_cars_st),1,ptf);
}
fclose(ptf);
}
}
}
void add_management_rec(){
FILE *ptf;
int i,count=0,empty;
float sum=0;
management_st new_manage_rec,last_manage_rec;
printf("please enter the new management record information:\n");
printf("fixed car's number:\t");
scanf("%ld",&new_manage_rec.car_num);
printf("date of management:\n");
printf("day:\t");
scanf("%d",&new_manage_rec.date_of_management.day);
printf("month:\t");
scanf("%d",&new_manage_rec.date_of_management.month);
printf("year:\t");
scanf("%d",&new_manage_rec.date_of_management.year);
printf("the number of the used pieces types:\t");
scanf("%d",&new_manage_rec.pieces_num);
printf("please enter each type's information:\n");
for(i=0;i<new_manage_rec.pieces_num;i++)
{
printf("piece #%d\n",i+1);
printf("piece name:\t");
scanf("%s",new_manage_rec.pie_rec[i].pie_name);
printf("number of used pieces:\t");
scanf("%d",&new_manage_rec.pie_rec[i].pie_num);
printf("piece's price:\t");
scanf("%f",&new_manage_rec.pie_rec[i].pie_price);
sum+=new_manage_rec.pie_rec[i].pie_price*(float)new_manage_rec.pie_rec[i].pie_num;
}
printf("management fees:\t");
scanf("%f",&new_manage_rec.management_fees);
new_manage_rec.total_price=new_manage_rec.management_fees+sum;
ptf=fopen("management.bin","ab+");
if(ptf!=NULL)
{
fseek(ptf,0,2);
empty=ftell(ptf);
rewind(ptf);
if(empty==0){
new_manage_rec.management_num=1;
fwrite(&new_manage_rec,sizeof(management_st),1,ptf);}
else
{
fseek(ptf,-sizeof(management_st),2);
fread(&last_manage_rec,sizeof(management_st),1,ptf);
new_manage_rec.management_num=last_manage_rec.management_num+1;
fwrite(&new_manage_rec,sizeof(management_st),1,ptf);
}
}
fclose(ptf);
}
void check(FILE *ptf){
if(ptf==NULL)
{
printf("error!\n");
exit -1;
}
}
int delete_car_rec(long search){
FILE *ptf;
int count=0,i,j,f=1;
own_cars_st avai_cars[100];
ptf=fopen("owners and cars.bin","rb");
check(ptf);
while(fread(&avai_cars[count],sizeof(own_cars_st),1,ptf)==1)
{
count++;
}
fclose(ptf);
if(count==1)
{
if(search==avai_cars[0].car_num)
{
f=0;
ptf=fopen("owners and cars.bin","wb");
fclose(ptf);
}
}
else
{
for(i=0;i<count;i++)
{
if(search==avai_cars[i].car_num)
{
f=0;
for(j=i+1;j<count;j++)
{
avai_cars[i]=avai_cars[j];
}
}
}
ptf=fopen("owners and cars.bin","wb");
check(ptf);
for(i=0;i<count;i++)
fwrite(&avai_cars[i],sizeof(own_cars_st),1,ptf);
fclose(ptf);
}
return f;
}
int search_own(char owner[]){
FILE *ptf;
int found=1,count=0,i;
own_cars_st avai_cars[100];
ptf=fopen("owners and cars.bin","rb");
check(ptf);
while(1==fread(&avai_cars[count],sizeof(own_cars_st),1,ptf))
count++;
fclose(ptf);
for(i=0;i<count;i++)
{
if(strcmp(avai_cars[i].owner_name,owner)==0)
{
found=0;
printf("car#%d:\t%ld\n",i+1,avai_cars[i].car_num);
}
}
return found;
}
int car_man(long search){
printf("*\n");
FILE *ptf;
management_st avai_man[100];
int i,count=0,found=1,j;
float total_cost=0;
printf("0\n");
ptf=fopen("management.bin","rb");
check(ptf);
printf("1\n");
while(1==fread(&avai_man[count],sizeof(management_st),1,ptf))
count++;
fclose(ptf);
printf("2\n");
for(i=0;i<count;i++)
{
if(search==avai_man[i].car_num)
{
found=0;
printf("date of management:\n");
printf("day:\t");
printf("%d",avai_man[i].date_of_management.day);
printf("month:\t");
printf("%d",avai_man[i].date_of_management.month);
printf("year:\t");
printf("%d",avai_man[i].date_of_management.year);
printf("the number of the used pieces types:\t");
printf("%d",avai_man[i].pieces_num);
printf("please enter each type's information:\n");
for(j=0;j<avai_man[i].pieces_num;j++)
{
printf("piece #%d\n",i+1);
printf("piece name:\t");
printf("%s",avai_man[i].pie_rec[j].pie_name);
printf("number of used pieces:\t");
printf("%d",avai_man[i].pie_rec[j].pie_num);
printf("piece's price:\t");
printf("%f",avai_man[i].pie_rec[j].pie_price);
}
printf("management fees:\t");
printf("%f",avai_man[i].management_fees);
}
total_cost+=avai_man[i].management_fees;
}
printf("3\n");
printf("total cost for this car:\t%f",total_cost);
return found;
}
the program seems to crash when calling the function car_man,ive writted few printf to trace when the program crashes and it only reaches printf("7\n").
any help would be appreciated ^.^
p.s. there's no warnings or any thing in the compiler
management_st avai_man[1000];
That is a really big array to be creating on the stack; you may be running out of stack-space.
Consider moving the array to dynamically allocated memory, or making it a static.
I can't find the solution on how to view the data that we have entered before in a function that is used to enter information. Also, I'm using struct, not normal declaration and variable.
Here's the flow, I choose case 1 which will call addbook function where I will insert my data then later I will choose case 4 where the data that I just insert now supposed to be display on the screen but unfortunately it won't display the data. So, what should I do? It would be grateful if you guys could help me
This is my coding
struct book
{
char title[50];
char author[50];
int quantity;
int price;
};
struct book a;
void addbook();
//void searchbook();
//void deletebook();
void viewbook();
//void updatebook();
void returnfunc();
int main()
{
char choice;
do
{
printf("\n\n\t1.Add Book\n");
printf("\n\t2.Search Book\n");
printf("\n\t3.Delete Book\n");
printf("\n\t4.View Book\n");
printf("\n\t5.Update Book\n");
printf("\n\t6.Exit\n");
printf("\n\nPlease enter your choice :");
scanf("%d",&choice);
switch (choice)
{
case 1 :addbook();
break;
//case 2 :searchbook();
break;
//case 3 :deletebook();
break;
case 4 :viewbook();
break;
//case 5 :updatebook();
break;
case 6 :printf("THANK YOU !!");
break;
default :printf("Wrong Choice.Please enter Again");
break;
}
}
while(choice!=6);
return 0;
}
void addbook()
{
printf("\n============= PLEASE ADD NEW BOOK DETAILS ================");
int quantity;
FILE *fp;
printf("\n\nNumber of book to insert:");
scanf("%d", &quantity);
struct book a[quantity];
int i;
fp=fopen("Bibek.dat","ab+");
for(i=0;i<quantity;i++)
{
printf("Title:");
scanf("%s", &a[i].title);
printf("Author:");
scanf("%s", &a[i].author);
printf("Price:");
scanf("%d", &a[i].price);
printf("\n");
}
printf("\n\n---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---\n\n");
}
//void searchbook()
//{
//printf("========SEARCH THE BOOK========");
//}
//void deletebook()
//{
//}
void viewbook(void)
{
FILE *fp;
int i=0,j;
system("cls");
printf("*********************************Book List*****************************");
printf(" TITLE AUTHOR QTY PRICE");
j=4;
fp=fopen("Bibek.dat","rb");
while(fread(&a,sizeof(a),1,fp)==1)
{
printf("%s",a.title);
printf("%s",a.author);
printf("%d",a.quantity);
printf("%d",a.price);
printf("\n\n");
j++;
i=i+a.quantity;
}
printf("Total Books =%d",i);
fclose(fp);
returnfunc();
}
void returnfunc(void)
{
{
printf(" Press ENTER to return to main menu");
}
a:
if(getch()==13) //allow only use of enter
main();
else
goto a;
}
//void updatebook()
//{
//}
The changes you need are rather easy and/or in the comments. Please try it. You can modify my attempts to fix your code.
#include <stdio.h>
#include <stdlib.h>
struct book {
char title[50];
char author[50];
int quantity;
int price;
};
struct book a;
void addbook();
//void searchbook();
//void deletebook();
void viewbook();
//void updatebook();
void returnfunc();
int main() {
int choice;
do {
printf("\n\n\t1.Add Book\n");
printf("\n\t2.Search Book\n");
printf("\n\t3.Delete Book\n");
printf("\n\t4.View Book\n");
printf("\n\t5.Update Book\n");
printf("\n\t6.Exit\n");
printf("\n\nPlease enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1 :
addbook();
break;
//case 2 :searchbook();
break;
//case 3 :deletebook();
break;
case 4 :
viewbook();
break;
//case 5 :updatebook();
break;
case 6 :
printf("THANK YOU !!");
break;
default :
printf("Wrong Choice.Please enter Again");
break;
}
}
while (choice != 6);
return 0;
}
void addbook() {
printf("\n============= PLEASE ADD NEW BOOK DETAILS ================");
int quantity;
FILE *fp;
printf("\n\nNumber of book to insert:");
scanf("%d", &quantity);
struct book a[quantity];
int i;
fp = fopen("Bibek.dat", "ab+");
for (i = 0; i < quantity; i++) {
printf("Title:");
scanf("%s", a[i].title);
printf("Author:");
scanf("%s", a[i].author);
printf("Price:");
scanf("%d", &a[i].price);
printf("\n");
}
fwrite(a, sizeof(a) , 1, fp);
fclose(fp);
printf("\n\n---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---\n\n");
}
void viewbook(void) {
FILE *fp;
int i = 0, j;
//system("cls");
printf("*********************************Book List*****************************");
printf(" TITLE AUTHOR QTY PRICE\n");
j = 0;
fp = fopen("Bibek.dat", "rb");
while (fread(&a, sizeof(a), 1, fp) == 1) {
printf("%s ", a.title);
printf("%s ", a.author);
//printf("%d", a.quantity);
printf("%d ", a.price);
printf("\n\n");
j++;
i = i + a.quantity;
}
printf("Total Books =%d", j);
fclose(fp);
returnfunc();
}
void returnfunc(void) {
printf(" Press ENTER to return to main menu");
}
Test
/a.out
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :1
============= PLEASE ADD NEW BOOK DETAILS ================
Number of book to insert:2
Title:foo
Author:carol
Price:2
Title:bar
Author:mallory
Price:3
---NEW BOOK DETAILS WAS SUCCESFULLY ADDED---
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :4
*********************************Book List***************************** TITLE AUTHOR QTY PRICE
foo carol 2
bar mallory 3
Total Books =2 Press ENTER to return to main menu
1.Add Book
2.Search Book
3.Delete Book
4.View Book
5.Update Book
6.Exit
Please enter your choice :
// I'm having issues compiling my program. Honestly, i'm a new programmer and i'm not really sure how to use certain things within my program. Can someone check my program and give me the corrections please or better thoroghly explain it ti me? It's neede for monday..//
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
int main()
{
//File declarations//
FILE*Log;
FILE*Inventory;
FILE*Username;
char fpass_word[10]="invent";
char fusername[10]="";
//Declarations for variables//
int main;
int sub_main;
int s=0;
int s_goods;
int p_goods;
int in_goods;
int c_goods;
double Unit_price;
int item_quantity;
int invoice_num;
char pass_word1[10]=" ";
char pass_word[10]="invent";
char username[10]="inventor";
char username1[10]=" ";
char Supplier[12]=" ";
char Items_name[12]=" ";
char Invoice_date[10]=" ";
//Declarations for variables//
int u=0;
int count=0;
int option;
int choice;
int choice1;
int m=0;
int Save;
int New_inventory;
int Update_inventory;
int Print;
int Close_Program;
int t_sales;
int t_purchases;
double m_sales[4]={30000.00,50000.00,100000.00,120000.00};
}
Log=fopen("Invent.txt","w")
if(Log==NULL)
printf("File does not exist");
}
else
{
fprintf(Log,"%s",pass_word);
fclose(Log);
}
user=fopen("Username.txt","w")
if(user==NULL)
{
printf("File does not exist");
}
else
{
fprintf(user,"%s",username);
fclose(user);
}
printf("__________________________________________________________\n\n");
printf("************Please login to your account!************\n\n");
printf("__________________________________________________________\n\n");
printf("Please enter your username: \n");
scanf("%s",username);
user=fopen("Username.txt","r")
{
if(user==NULL)
{
printf("File does not exist");
}
else
{
fprintf(user,"%s",fusername);
fclose(user);
}
choice1=strncmp(username,fusername,10);
printf("Please enter password: \n");
scanf("%s",pass_word1);
Log=fopen("Invent.txt","r")
if(Log==NULL)
{
printf("File does not exist");
}
else
{
fscanf(Log,"%s",fpass_word);
fclose(Log);
}
choice=strncmp(pass_word1,fpass_word,10);
while (choice!=0 && count<3)
{
printf("*************************************************************************************\n\n");
printf("!!!!!!!!!!!!!!!!!!!!!!!!Please re-enter your login info!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
printf("*************************************************************************************\n\n");
printf(" Please enter username: \n");
scanf("%s",&username);
choice=strncmp(username,username1,10);
printf("Please enter password!\npassword:");
scanf("%s",pass_word);
choice=strncmp(pass_word,pass_word1,10);
count=count++;
//menu function!!!
getch();
system("cls");
}//login page
printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
printf("\n******************** Welcome to the INVENT BIZ main page!********************\n\n");
printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
printf (">>>>>>>>>>>>>>> Please select an option you desire:<<<<<<<<<<<<<<<\n\n");
printf("1: New inventory\n");
printf("2: Update inventory\n");
printf("3: Print\n");
printf("4: Save \n");
printf("5: Close Program\n");
printf("Please select an option: \n");
scanf("%d",&option);
//menu screen
while(option!=6)
{
switch(main)
{
case 1:
printf("New inventory");
Inventory=fopen("New Inventory.txt","w")
if (Inventory==NULL)
printf("This File is empty!");
}
else
{
fprintf("Please enter invoice date:\t\n");
fprintf("Please enter Supplier:\t\n");
fprintf("Please enter Item name:\t\n");
fprintf("Please enter quantity of items:\t\n");
fprintf(" Please enter invoice number:\t\n");
fprintf("Please enter Unit Price:\t\n");
}
fclose(Inventory);
// Data entered for inventory//
switch(sub-main)
{
case 10:
printf("Please enter sales for each month:%d",t_sales);
printf(" Total Sales\n");
scanf("%d",&t_sales);
break;
case 3:
printf("Print");
Inventory=fopen("New Inventory.txt","r")
fscanf(Inventory,"%d",Invoice_date);
fscanf(Inventory,"%s",Supplier);
fscanf(Inventory,"%s",Items_name);
fscanf(Inventory,"%d",&item_quantity);
fscanf(Inventory,"%d",&invoice_num);
fscanf(Inventory,"%d",&Unit_price);
fclose(Inventory);
break;
case 4:
printf("Save");
Inventory=fopen("Inventory1.txt","w");
if(Inventory==NULL)
{
printf("This file empty!!!");
}
else
{
printf("File saved");
}
fclose(Inventory);
case 5:
printf("Close Program");
exit(main);
break;
} // end switch
system("cls");
printf (">>>>>>>>>>>>>>> Please select an option you desire!!!<<<<<<<<<<<<<<<\n\n");
printf("1: New inventory\n");
printf("2: Update inventory\n");
printf("3: Print\n");
printf("4: Save \n");
printf("Please select an option: \n");
scanf("%d",&option);
}
}
system("cls");
getch();
return ();
}
You should really be more specific and not just throw down an entire program and say you want to someone to clean it up for you.
Thank being said I noticed right away you had a few improperly placed curly braces. Your very first if statement is missing an opening brace and further down you have an unnecessary opening brace after user=fopen("Username.txt", "r")
Beyond that indentation is completely off throughout and your switch statements make it really hard to follow what it is you're trying to accomplish with them.
My advice is to read through the compiler errors and locate each issue one by one, and if you're having a problem you can't solve then be specific and post the errors you're getting with your program.
#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.