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've been working on a group program for a systems programming class which will run functions that someone at a bank would use. These functions include add bank customers, output balance of a customer, and so on. The problem lies with the add customer function.
This function will run in an infinite loop in terminal and Putty, but when I run it as a standalone program in XCode the loop quits. Is there anything wrong with the program that I, my group members AND our professor are overlooking?
#include <stdio.h>
#include <string.h>
#include "./bank.h"
void add(FILE *fp)
{
int i=0;
char firstName[20];
char lastName[20];
float camount;
int prompt;
float samount;
float mamount;
fseek(fp,0,2);
do
{
printf("\nEnter first name: ");
scanf("%s", firstName);
strcpy(bank[i].firstName, firstName);
printf("\nEnter last name: ");
scanf("%s",lastName);
strcpy(bank[i].lastName, lastName);
printf("\nEnter checking account balance: ");
scanf("%f", &camount);
bank[i].checking = camount;
printf("\nEnter savings account balance: ");
scanf("%f", &samount);
bank[i].savings = samount;
printf("\nEnter money market account balance: ");
scanf("%f", &mamount);
bank[i].moneyMarket = mamount;
fwrite(&bank[i],1, sizeof(struct BankInfo),fp);
i++;
printf("Enter 1 to enter another name and 0 to quit: ");
scanf("%d", &prompt);
printf("%d\n", prompt);
} while(prompt == 1);
fclose(fp);
return;
}
Screenshot of output once Makefile is executed:
Sidenote: This function does write onto the file that we are passing which is great but we still need for it to break out of the loop.
EDIT: code for the main function:
#include
#include
#include
#include "bank.h"
#include "definitions.h"
int main()
{
FILE *fp;
int selection;
fp=fopen("bankInfo.dat","ab+");
selection=menu();
while(selection !=6)
{
switch(selection)
{
case 1:
add(fp);
break;
case 2:
// outputBalance(fp);
break;
case 3:
delete(fp);
break;
case 4:
update(fp);
break;
case 5:
// display(fp);
break;
case 6:
exit(0);
break;
default:
printf("Invalid selection\n");
break;
}
}
fclose(fp);
return 0;
}
You are assigning selection only once. This should fix your issue:
while(true)
{
selection=menu();
switch(selection)
{
case 1:
add(fp);
break;
case 2:
// outputBalance(fp);
break;
case 3:
delete(fp);
break;
case 4:
update(fp);
break;
case 5:
// display(fp);
break;
case 6:
fclose(fp);
exit(0);
break;
default:
printf("Invalid selection\n");
break;
}
}
return 0;
1) I'm stuck with inserting and displaying the user inputs. I'm confused when to use fwrite, whether within the addRecord function or within the insert function.
2) Should I write it after I input or after the insertion?
Anyhow, my code looks like this:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
struct book{
char title[30];
char author[20];
char isbn[14];
char genre[10];
char publisher[30];
int year;
char synopsis[150];
struct book *left;
struct book *right;
};
typedef struct book catalog;
catalog *root;
FILE *fx,*fy;
int structsize=sizeof(catalog);
void addBooks();
void removeBooks();
void modify();
void searchBook();
void perTitle();
void perAuthor();
void perISBN();
void perGenre();
void displayCatalog(catalog *root);
int ifexists(char *x);
void insert(catalog *root,catalog *x);
catalog remove(catalog *root,catalog *x);
void menu();
void gotoxy(int x,int y);
void main(){
root=NULL;
fx=fopen("CAT.dat","rb+");
if(fx==NULL){
fx=fopen("CAT.dat","wb+");
if(fx==NULL){
gotoxy(22,10);
printf("No records to show...");
getch();
menu();
}
}
menu();
}
void menu(){
int choice;
system("cls");
gotoxy(22,5);
printf("----------WELCOME TO THE CARD CATALOG----------");
gotoxy(22,7);
printf("1] Add a book");
gotoxy(22,8);
printf("2] Remove a book");
gotoxy(22,9);
printf("3] Modify a book");
gotoxy(22,10);
printf("4] Search a book");
gotoxy(22,11);
printf("5] Display the catalog");
gotoxy(22,12);
printf("6] Exit");
gotoxy(22,14);
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: addBooks();
break;
case 2: removeBooks();
break;
case 3: modify();
break;
case 4: searchBook();
break;
case 5: displayCatalog(root);
break;
case 6: exit(0);
}
}
void addBooks(){
char n;
catalog *ptr;
ptr=(catalog*)malloc(sizeof(catalog));
fx=fopen("CAT.dat","ab+");
fseek(fx,0,SEEK_END);
system("cls");fflush(stdin);
gotoxy(22,8);
printf("Title: ");
scanf("%[^\n]s",ptr->title);fflush(stdin);
gotoxy(22,9);
printf("Author: ");
scanf("%[^\n]s",ptr->author);fflush(stdin);
gotoxy(22,10);
printf("Genre: ");
scanf("%[^\n]s",ptr->genre);fflush(stdin);
gotoxy(22,11);
printf("ISBN: ");
scanf("%[^\n]s",ptr->isbn);fflush(stdin);
gotoxy(22,12);
printf("Publisher: ");
scanf("%[^\n]s",ptr->publisher);fflush(stdin);
gotoxy(22,13);
printf("Year: ");
scanf("%d",&ptr->year);fflush(stdin);
gotoxy(22,14);
printf("Synopsis: ");
scanf("%[^\n]s",ptr->synopsis);fflush(stdin);
ptr->left=NULL;
ptr->right=NULL;
if(root==NULL){
root=ptr;
} else {
insert(root,ptr);
}
gotoxy(22,18);
printf("Add another (y/n)? ");
scanf("%c",&n);
if(n=='y' || n=='Y'){
addBooks();
}
fclose(fx);
getch();
menu();
}
void insert(catalog *root,catalog *x){
fx=fopen("CAT.dat","ab+");
fseek(fx,0,SEEK_END);
if((x->isbn) < (root->isbn)){
if(root->left==NULL){
root->left=x;
} else {
insert(root->left,x);
}
}
if((x->isbn) > (root->isbn)){
if(root->right==NULL){
root->right=x;
} else {
insert(root->right,x);
}
}
fwrite(&x,structsize,1,fx);
fclose(fx);
getch();
}
void removeBooks(){
catalog *ptr,*temp;
int x;
char title[20],y;
system("cls");
if(root==NULL){
gotoxy(22,10);
printf("No records to show...");
getch();
menu();
}
gotoxy(22,10);
printf("Enter title of book to delete: ");
scanf("%s",title);
ptr=root;
while(ptr!=NULL){
if(strcmp(ptr->title,title)==0){
gotoxy(22,11);
printf("The book is in the catalog.");
gotoxy(22,12);
printf("Title: %s",root->title);
gotoxy(22,13);
printf("Author: %s",root->author);
} else {
gotoxy(22,11);
printf("No book with that title in the catalog.");
getch();
menu();
}
if(strcmp(ptr->title,title)==0){
gotoxy(22,14);
printf("Remove book record? (y/n) ");
if(getch()=='y'){
remove(root,ptr);
menu();
} else {
menu();
}
}
}
}
catalog remove(catalog *root,catalog *x){
catalog *temp;
char y;
if(x==root){
temp=root;
free(temp);
root=NULL;
} else if(x->isbn < root->isbn){
*root->left=remove(root->left,x);
} else if(x->isbn > root->isbn){
*root->right=remove(root->right,x);
} else if(root==NULL){
gotoxy(22,10);
printf("Nothing to remove...");
getch();
menu();
}
printf("The book has been removed.");
printf("Remove another book? (y/n) ");
scanf("%c",&y);
return *x;
}
void modify(){
catalog *ptr;
int num;
char title[20];
system("cls");
if(root==NULL){
gotoxy(22,10);
printf("No records exist...");
getch();
menu();
}
printf("Enter title of book to be modified: ");
scanf("%s",title);
ptr=root;
while(ptr!=NULL){
if(strcmp(ptr->title,title)==0){
printf("Input new information.");
printf("Title: ");
scanf("%s",root->title);
printf("Author: ");
scanf("%s",root->author);
printf("ISBN: ");
scanf("%d",root->isbn);
printf("Publisher: ");
scanf("%s",root->publisher);
printf("Year: ");
scanf("%d",root->year);
printf("Synopsis: ");
scanf("%s",root->synopsis);
printf("The book's information has been modified.");
} else {
printf("No book found.");
break;
}
}
getch();
menu();
}
void searchBook(){
int choice;
char title[20], author[20];
int num;
system("cls");
if(root==NULL){
printf("No records to show...");
getch();
menu();
}
gotoxy(22,10);
printf("-----SEARCH A BOOK-----");
gotoxy(22,12);
printf("1] By title");
gotoxy(22,13);
printf("2] By author");
gotoxy(22,14);
printf("3] By ISBN");
gotoxy(22,15);
printf("4] Back to menu");
gotoxy(22,18);
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: perTitle();
break;
case 2: perAuthor();
break;
case 3: perISBN();
break;
case 4: perGenre();
break;
case 5: menu();
}
}
void perTitle(){
catalog *ptr;
char title[20], temp[20], ans;
system("cls");
gotoxy(22,15);
printf("Enter book title: ");
scanf("%s",title);fflush(stdin);
ptr=root;
while(ptr!=NULL){
while(strcpy(temp,ptr->title)){
if(strcmp(temp,title)==0){
system("cls");
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);fflush(stdout);
break;
} else {
gotoxy(22,10);
printf("No records to show...");
}
}
printf("Try another? (y/n) ");
scanf("%c",&ans);fflush(stdin);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
}
void perAuthor(){
catalog *ptr;
char author[20],au[20],ans;
system("cls");
gotoxy(22,15);
printf("Enter author's name: ");
scanf("%s",author);fflush(stdin);
ptr=root;
while(ptr!=NULL){
while(strcpy(au,ptr->title)){
if(strcmp(au,author)==0){
system("cls");
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
}
}
void perISBN(){
catalog *ptr;
char isbn[20];
char ans;
system("cls");
gotoxy(22,15);
printf("Enter book ISBN: ");
scanf("%s",isbn);fflush(stdin);
ptr=root;
while(ptr->isbn==isbn){
ptr=ptr->left;
if(ptr==NULL)
menu();
}
system("cls");
if(ptr!=NULL){
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
if(ans=='y' || ans=='Y'){
searchBook();
}
getch();
menu();
}
void perGenre(){
catalog *ptr;
char genre[20],ans;
char gen[10];
system("cls");
gotoxy(22,15);
printf("Enter book title: ");
scanf("%s",genre);
ptr=root;
while(strcmp(ptr->genre,genre)!=0){
ptr=ptr->left;
if(ptr==NULL)
menu();
}
system("cls");
if(ptr!=NULL){
gotoxy(22,10);
printf("That book is in the catalog.");
gotoxy(22,11);
printf("Title: %s",ptr->title);
gotoxy(22,12);
printf("Author: %s",ptr->author);
gotoxy(22,13);
printf("ISBN: %d",ptr->isbn);
gotoxy(22,14);
printf("Genre: %s",ptr->genre);
gotoxy(22,15);
printf("Publisher: %s",ptr->publisher);
gotoxy(22,16);
printf("Year: %d",ptr->year);
gotoxy(22,17);
printf("Synopsis: %s",ptr->synopsis);
} else {
gotoxy(22,10);
printf("No records to show...");
}
printf("Try another? (y/n) ");
scanf("%c",&ans);
switch(ans){
case 'y': searchBook();
break;
case 'n': menu();
}
}
void displayCatalog(catalog *root){
catalog *ptr;
system("cls");
fx=fopen("CAT.dat","rb+");
rewind(fx);
ptr=root;
while(fread(&ptr,structsize,1,fx)==1){
if(root->left!=NULL){
displayCatalog(root->left);
}
printf("Title: %s\n",ptr->title);
printf("Author: %s\n",ptr->author);
printf("ISBN: %s\n",ptr->isbn);
printf("Genre: %s\n",ptr->genre);
printf("Publisher: %s\n",ptr->publisher);
printf("Year: %d\n",ptr->year);
printf("Synopsis: %s\n",ptr->synopsis);
if(root->right!=NULL){
displayCatalog(root->right);
}
break;
}
fclose(fx);
getch();
menu();
}
void gotoxy(int x,int y){
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
1) You have either turned off all the compiler warnings or you have elected to ignore about 100 compiler warnings.
You should start over, set compiler warning to maximum, or at least 4. Write the program so it compiles with zero warnings.
Few notable errors:
scanf("%d",root->isbn);
isbn is character array, so this should read scanf("%s",root->isbn);
scanf("%d", root->year);
scanf requires address of variable, not copy. It should be scanf("%d", &root->year);
You have many other errors, for example in void modify you try to access uninitialized pointer ptr.
2) You have this gotoxy function, it makes things look very nice, but you should remove it, given that your program does not work at all. If you get your program working without error then you can put back these decorative functions.
3) Try to avoid global variables. FILE *fx, *fy; specially cause problems. You have file handles which are left open, then you try to open them again in a different mode.
catalog *root; is not allocated. The whole program seems to rest on this variable, this will go nowhere. For example
void perTitle()
{
catalog *ptr;
...
ptr = root;
while (ptr != NULL)
{
while (strcpy(temp, ptr->title))
{
...
}
}
}
This may look like a search function but it's just while(0){}, it doesn't do anything.
4) It seems you are managing the records in file, and also storing them in a linked list. This gets very confusing. You should do one or the other. I recommend you store the records in file. Read them in to linked list. And then save them in to file when finished.
5) Use more functions to avoid typing the same thing. For example write a new function showBook
void showBook(catalog *ptr)
{
printf("Title: %s\n", ptr->title);
printf("Author: %s\n", ptr->author);
printf("ISBN: %s\n", ptr->isbn);
printf("Genre: %s\n", ptr->genre);
printf("Publisher: %s\n", ptr->publisher);
printf("Year: %d\n", ptr->year);
printf("Synopsis: %s\n", ptr->synopsis);
}
You can reuse this function in perTitle(), perAuthor(), perISBN(), perGenre() and elsewhere.
6) Initialize data to zero, example:
catalog *ptr = malloc(sizeof(catalog));
memset(ptr, 0, sizeof(catalog));
Start with a small program. Add a few simple function, test it, add more functions. You may want to write a separate test program to test linked lists. Example
void getBook(catalog *ptr)
{
printf("Title:\n");
scanf("%s", ptr->title);
printf("Author:\n");
scanf("%s", ptr->author);
printf("Genre:\n");
scanf("%s", ptr->genre);
printf("ISBN:\n");
scanf("%s", ptr->isbn);
printf("Publisher:\n");
scanf("%s", ptr->publisher);
printf("Year:\n");
scanf("%d", &ptr->year);
printf("Synopsis:\n");
scanf("%s", ptr->synopsis);
}
void addBooks()
{
catalog cat;
memset(&cat, sizeof(catalog), 0);
getBook(&cat);
FILE* fx = fopen("CAT.dat", "ab+");
fseek(fx, 0, SEEK_END);
fwrite(ptr, structsize, 1, fx);
fclose(fx);
printf("Add another (y/n)?\n");
char n;
scanf("%c", &n);
if (n == 'y' || n == 'Y')
addBooks();
_getch();
menu();
}
void displayCatalog()
{
system("cls");
FILE* fx = fopen("CAT.dat", "rb+");
catalog cat;
while (fread(&cat, sizeof(catalog), 1, fx) == 1)
{
showBook(&cat);
break;
}
fclose(fx);
_getch();
menu();
}
void menu()
{
int choice;
system("cls");
printf("----------WELCOME TO THE CARD CATALOG----------\n");
printf("1] Add a book\n");
printf("2] Remove a book\n");
printf("3] Modify a book\n");
printf("4] Search a book\n");
printf("5] Display the catalog\n");
printf("6] Exit\n");
printf("Enter your choice: \n");
scanf("%d", &choice);
switch (choice)
{
case 1: addBooks(); break;
case 2: removeBooks(); break;
case 3: modify(); break;
case 4: searchBook(); break;
case 5: displayCatalog(); break;
case 6: exit(0);
}
}
int main()
{
FILE *f = fopen("CAT.dat", "rb+");
if (f == NULL)
{
f = fopen("CAT.dat", "wb+");
if (f == NULL)
{
printf("Cannot create new file...\n");
return 0;
}
}
fclose(f);
menu();
return 0;
}
i have this code. I am getting linker error. I don't know what it is this linker error. please help me. I am using turbo c++ compiler for this code.
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>
struct contact
{
long ph;
char name[20],add[20],email[30];
};
struct contact list;
char query[20],name[20];
FILE *fp, *ft;
int i,n,ch,l,found;
void main()
{
start:
system("cls"); /* ************Main menu *********************** */
printf("\n\t **** Welcome to a0 contact Manager ****");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n \t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 0:
printf("\n\n\t\tAre you sure u want to exit?");
break;
/* *********************add new contacts************ */
case 1:
system("cls");
fp=fopen("contact.dll","a");
for(;;)
{
fflush(stdin);
printf("To exit enter blank space in the name input\nName (Use identical):");
scanf("%[^\n]",&list.name);
if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0)
break;
fflush(stdin);
printf("Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,fp);
}
fclose(fp);
break;
/* *********************list of contacts************************* */
case 2:
system("cls");
printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tPhone No\t Address\t\tE-mail ad.\n=================================================================\n\n");
for(i=97;i<=122;i=i+1)
{
fp=fopen("contact.dll","r");
fflush(stdin);
found=0;
while(fread(&list,sizeof(list),1,fp)==1)
{
if(list.name[0]==i || list.name[0]==i-32)
{
printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name,list.ph,list.add,list.email);
found++;
}
}
if(found!=0)
{
printf("=========================================================== [%c]-(%d)\n\n",i-32,found);
getch();
}
fclose(fp);
}
break;
/* *******************search contacts********************** */
case 3:
system("cls");
do
{
found=0;
printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name ofcontact to search: ");
fflush(stdin);
scanf("%[^\n]",&query);
l=strlen(query);
fp=fopen("contact.dll","r");
system("cls");
printf("\n\n..::Search result for '%s' \n===================================================\n",query);
while(fread(&list,sizeof(list),1,fp)==1)
{
for(i=0;i<=l;i++)
name[i]=list.name[i];
name[l]='\0';
if(stricmp(name,query)==0)
{
printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t:%s\n",list.name,list.ph,list.add,list.email);
found++;
if(found%4==0)
{
printf("..::Press any key to continue...");
getch();
}
}
}
if(found==0)
printf("\n..::No match found!");
else
printf("\n..::%d match(s) found!",found);
fclose(fp);
printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t");
scanf("%d",&ch);
}
while(ch==1);
break;
/* *********************edit contacts************************/
case 4:
system("cls");
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
fflush(stdin);
printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:");
scanf("%[^\n]",name);
while(fread(&list,sizeof(list),1,fp)==1)
{
if(stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
}
fflush(stdin);
printf("\n\n..::Editing '%s'\n\n",name);
printf("..::Name(Use identical):");
scanf("%[^\n]",&list.name);
fflush(stdin);
printf("..::Phone:");
scanf("%ld",&list.ph);
fflush(stdin);
printf("..::address:");
scanf("%[^\n]",&list.add);
fflush(stdin);
printf("..::email address:");
gets(list.email);
printf("\n");
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
/* ********************delete contacts**********************/
case 5:
system("cls");
fflush(stdin);
printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:");
scanf("%[^\n]",&name);
fp=fopen("contact.dll","r");
ft=fopen("temp.dat","w");
while(fread(&list,sizeof(list),1,fp)!=0)
if (stricmp(name,list.name)!=0)
fwrite(&list,sizeof(list),1,ft);
fclose(fp);
fclose(ft);
remove("contact.dll");
rename("temp.dat","contact.dll");
break;
default:
printf("Invalid choice");
break;
}
printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
goto start;
case 0:
break;
default:
printf("Invalid choice");
break;
}
}
I am facing this error.
Undefined symbol _system in module in c.
The Undefined symbol _system linker error means that the linker cannot find any (static) library code to link against your system("cls") calls. In the case of Turbo C++ this probably means you have to specify an additional library in your project configuration.
If you would use a contemporary C compiler such as GCC or Clang (which are freely available for many systems), you will not have this problem.
#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.