I'm making a c program for a simple library but for some reason the program keeps crashing right on startup. Like there's a menu that is displayed but it doesn't even appear it just crashes. can any one help me?
struct library
{
char name[50];
int id;
int qty;
}books[50],copy[50],delet[50],sort[50];
int i=0;
FILE *mybooks;
int main()
{
int choice; char ans;
int id;
int qty;
int s,o=0,j=0;
char name[50];
mybooks=fopen("D:\\mybooks.txt","r");
if (mybooks == NULL) printf("Error. File not found.");
else
{
while(!feof(mybooks))
{
fscanf(mybooks,"%[^\n] %d %d",books[i].name,&books[i].id,&books[i].qty);
strcpy(copy[i].name,books[i].name);
copy[i].id=books[i].id;
copy[i].qty=books[i].qty;
i++;
}
fclose(mybooks);
}
printf("Welcome to the Library.\n");
do
{
printf("Please choose an option:\n");
printf("1.Insert a book\n");
printf("2.Delete a book\n");
printf("3.Search a book by ID\n");
printf("4.Search a book by name\n");
printf("5.Display all books (sorted by name)\n");
printf("6.Display all books (unsorted)\n");
scanf("%d",&choice);
switch (choice){
case 1:
printf("You will need to enter a name, ID, and quantity of the book.\n");
printf("please enter book name:");
fflush(stdin);
fgets(name,sizeof name,stdin);
printf("please enter book ID:");
scanf("%d",&id);
printf("please enter book quantity:");
scanf("%d",&qty);
InsertBook(name,id,qty);
printf("your book has been added successfully\n");
break;
case 2:
printf("Please enter book ID:");
scanf("%d",&id);
DeleteBook(id);
printf("book successfully deleted.\n");
break;
case 3:
printf("Please enter ID of Book:");
scanf("%d",&id);
s=LinearSearch(id,j);
if (s>=0)
{
printf("Book Found.\n");
printf("Name:%s",books[s].name);
printf("ID:%d\n",books[s].id);
printf("Quantity:%d\n",books[s].qty);
}
else
printf("Sorry, the book doesn't exist.\n");
break;
case 4:
printf("Please enter name of book:");
fflush(stdin);
gets(name);
sorting();
s=BinarySearch(name,0,i);
printf("Book Found.\n");
printf("ID:%d\n",sort[s].id);
printf("Quantity:%d\n",sort[s].qty);
break;
case 5:
sorting();
while (o<i);
{
printf("%s\n",sort[o].name);
o++;
}
printf("\n");
break;
case 6:
while(o<i)
{
printf("%s",books[o].name);
o++;
}
break;
default:
printf("Invalid Choice. Please try again.\n");
break;
}
printf("do you want to choose another option?(y/n) ");
scanf(" %c",&ans);
}while(ans == 'y');
}
(I'm using functions for the library but I don't think they are causing any problem since i didn't call them yet.)
edited the question to add the structure
You have tested an end of file condition before performing any i/o on the input file. And then, when you perform the i/o with fscanf(), you did not test the result to see if the variables were successfully read.
What is happening is that scanf() is probably failing and you do not reach the end of the loop, i.e., the EOF condition. You get stuck in there until one of your assignments (strcpy(copy[i].name,books[i].name);, copy[i].id=books[i].id; or copy[i].qty=books[i].qty;) will finally cause an overflow.
To verify this, run the code in a debugger.
Using fscanf() is very tricky, always test it thoroughly.
Related
I am trying to make a simple option menu that is executed within a do- while loop. The menu has 5 different options. I am currently trying to test the the get initial option but when I choose the case 'b' the function does not even execute and the option menu reappears again
#include "mathprogram.h"
int main (void)
{
char menu_option,initials;
int difficulty;
printf(" EDUCATIONAL MATH PROGRAM!!!\n");
printf("------------------------------------------\n\n");
do{
printf("Main Menu\n");
printf("a. Learn about how to use program.\n");
printf("b. Enter your initials (3 individual letters).\n");
printf("c. Difficulty Selection.\n");
printf("d. Start a new sequence of problems.\n");
printf("e. Save and quit.\n");
printf(" Please enter an option from the main menu: ");
scanf("%c",&menu_option);
switch(menu_option){
case 'a':
//Learn_to_use();
break;
case 'b':
initials=get_intials();
break;
case'c':
printf("case c");
//difficulty = get_difficulty();
break;
case'd':
break;
case'e':
break;
default:
printf("invalid input");
break;
}
}while(menu_option !='e');
}
get initials function:
#include "mathprogram.h"
char get_intials(void){
char initails;
printf("Please Enter Initials: ");
scanf("%c",&initails);
return initails;
}
scanf("%c", &var);
leaves the newline in the buffer or stream.
Try to change it to
scanf(" %c", &var);
and it should work as expected (Note the space before the %; this consumes the whitespace so that the next scanf call should work).
However, the initials as defined in your program consist of only one character, not three. I'll leave that up to you to design, that is a different question.
There's a '\n' between option 'b' and the initials char, the "scanf" must handle it.
Complete code that works:
#include <stdio.h>
char get_intials(void){
char initails;
printf("Please Enter Initials: ");
scanf("\n%c",&initails) ;
return initails;
}
int main (void)
{
char menu_option,initials;
int difficulty;
printf(" EDUCATIONAL MATH PROGRAM!!!\n");
printf("------------------------------------------\n\n");
do{
printf("Main Menu\n");
printf("a. Learn about how to use program.\n");
printf("b. Enter your initials (3 individual letters).\n");
printf("c. Difficulty Selection.\n");
printf("d. Start a new sequence of problems.\n");
printf("e. Save and quit.\n");
printf(" Please enter an option from the main menu: ");
scanf("%c",&menu_option);
switch(menu_option){
case 'a':
//Learn_to_use();
break;
case 'b':
initials=get_intials();
printf( "input initials:%c\n", initials) ;
break;
case'c':
printf("case c");
//difficulty = get_difficulty();
break;
case'd':
break;
case'e':
break;
default:
printf("invalid input");
break;
}
}while(menu_option !='e');
}
output:
Please enter an option from the main menu: b
Please Enter Initials: c
input initials:c
/*
This code includes a stray \n from previous input due to which get_initials method was not working properly,it picked up the remanant \n in the input stream and concluded,
flushing the input stream is the solution to the problem, uncomment the code at line 8 to get proper functionality
*/
#include <stdio.h>
char get_intials(void){
char initails;
//fflush(stdin);
printf("Please Enter Initials: ");
scanf("%c",&initails);
return initails;
}
int main (void)
{
char menu_option,initials;
int difficulty;
printf(" EDUCATIONAL MATH PROGRAM!!!\n");
printf("------------------------------------------\n\n");
do{
printf("Main Menu\n");
printf("a. Learn about how to use program.\n");
printf("b. Enter your initials (3 individual letters).\n");
printf("c. Difficulty Selection.\n");
printf("d. Start a new sequence of problems.\n");
printf("e. Save and quit.\n");
printf(" Please enter an option from the main menu: ");
fflush(stdin);
scanf("%c",&menu_option);
switch(menu_option){
case 'a':
//Learn_to_use();
break;
case 'b':
initials=get_intials();
if(initials == '\n')
printf("\n%s\n","I was there in stream as newline");
//printf("\n%c",initials);
break;
case'c':
printf("case c");
//difficulty = get_difficulty();
break;
case'd':
break;
case'e':
break;
default:
printf("invalid input");
break;
}
}while(menu_option !='e');
return 0;
}
I've written this c program to insert, view, modify and delete the records in a file. File name is emp.dat. The code for displaying, adding and deleting is working fine but the modify part is not working. The program asks to input details to modify but nothing gets updated/modified.
The Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
FILE *outp,*inpt;
char another,choice;
struct emp
{
int emp_no,age;
char name[40];
float bs;
};
struct emp e;
char empname[40];
long int recsize;
outp=fopen("emp.dat","r+");
if(outp=='\0')
{
outp=fopen("emp.dat","w+");
if(outp=='\0')
{
puts("cannot open file\n");
exit(1);
}
}
recsize=sizeof(e);
while(1)
{
printf("1.Add records\n");
printf("2.List records\n");
printf("3.Modify records\n");
printf("4.Delete records\n");
printf("0. exit\n");
printf("Your choice\n");
fflush(stdin);
choice=getche();
switch(choice)
{
case '1': //code to add data
.
case '2': //code to display data
case '3': //code to modify data
another='Y';
while(another=='Y')
{
printf("\nEnter name of employee to modify");
scanf("%s",empname);
rewind(outp);
while(fread(&e,recsize,1,outp)==1)
{
if(strcmp(e.name,empname)==0)
{
printf("\nenter new name,age & gs");
scanf("%d %s %d %f",&e.emp_no,&e.name,&e.age,&e.bs);
fseek(outp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,outp);
break;
}
}
printf("\nModify another record(Y/N)");
fflush(stdin);
another=getche();
}
printf("\n\n");
break;
case '4': //code to delete data
case '0':
fclose(outp);
printf("\n\n");
exit(1);
}
}
}
As can be seen in output the name doesn't change from Zaid to Cow, so does the age and gs
You really should test the return values.
The prompt asks for name,age & gs, and you enter them as asked. However scanf is instructed to get an integer (emp_no) first ("%d %s %d %f"). Missing that it fails immediately and nothing gets updated.
The situation is easily detectable: scanf returns the number of successful conversions.
I have an assignment to write a program for supporting an art gallery in C. It has to be menu based program using lists. I wrote the first function of the menu and I need some help writing the other three. So I have a structure of an unique code of the painting, author's name, painting's name, price, year of the painting. I have to create a function deleting a painting using the unique code, print out all the info about every painting and modifying a painting again using said code. The data has to be in a dynamic type structure using a linked list. This is the program so far.
#include <stdio.h>
void addPainting(void);
void erasePainting(void);
void printData(void);
void modifyData(void);
int main()
{
struct paintings
{
char code[20];
char name[50];
char paintingName[50];
double price;
int year;
}painting[100];
int choice;
do
{
printf("Menu\n");
printf("To add a painting press 1.\n");
printf("To erase a painting press 2.\n");
printf("To print data for all paintings by authors alphabetically press 3.\n");
printf ("To modify data for a painting press 4.\n");
printf("To exit the program press 5.\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
addPainting();
break;
}
case 2:
{
erasePainting();
break;
}
case 3:
{
printData();
break;
}
case 4:
{
modifyData();
break;
}
default: printf ("Wrong choice. Try again\n");
break;
}
}while (choice !=5);
void addPainting()
{
FILE *fp;
struct paintings painting;
printf("Enter code:");
scanf("%s", &painting.code);
printf("Enter the name of the author:");
scanf("%s", &painting.name);
printf("Enter the name of the painting:");
scanf("%s", &painting.paintingName);
printf("Enter price:");
scanf("%lf", &painting.price);
printf("Enter the year of creation:");
scanf("%d", &painting.year);
if ((fp=fopen("paintings","wb"))==NULL)
exit(1);
if ((fwrite (&painting,sizeof(painting),1,fp)!=1))
exit(2);
fclose(fp);
}
}
First problem: You are missing the closing brace ( } ) for the main() function. (but I am sure you knew that)
The reason for the struct size error is that you are attempting to create an instance of struct painting in the void addPainting() function, when it was created with local scope in the main function, and therefore it is not visible to the function. Create struct painting with global scope if you want to use it this way:
This will build (and run) but only for the functions defined, the others are commented out. There are other problems you will have to work out, or ask about.
EDITED to fix scanf() statements, show use of fopen()/fclose(), create and write strings using sprintf()/fputs()...
void addPainting(void);
//void erasePainting(void);
//void printData(void);
//void modifyData(void);
typedef struct //created with global scope, visible to all functions
{
char code[20];
char name[50];
char paintingName[50];
double price;
int year;
}PAINTING;
PAINTING painting[100];//array of instance of PAINTING
#define PATH "C:\\play\\painting.txt" //edit to your need
int main()
{
int choice;
do
{
printf("Menu\n");
printf("To add a painting press 1.\n");
printf("To erase a painting press 2.\n");
printf("To print data for all paintings by authors alphabetically press 3.\n");
printf ("To modify data for a painting press 4.\n");
printf("To exit the program press 5.\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
addPainting();
break;
}
case 2:
{
//erasePainting();
break;
}
case 3:
{
//printData();
break;
}
case 4:
{
//modifyData();
break;
}
default: printf ("Wrong choice. Try again\n");
break;
}
}while (choice !=5);
}
void addPainting(void)
{
FILE *fp;
char stringToWrite[80];
//Note: this function could be prototyped with an int argument
// to be used as an index for the array arguments of your
// structure. Currently, the indexes are hard-coded to 0,
printf("Enter code:");
//scanf("%s", &painting[0].code);
scanf("%s", painting[0].code); //& not needed for char array (char *) et. al.
printf("Enter the name of the author:");
scanf("%s", painting[0].name);
printf("Enter the name of the painting:");
scanf("%s", painting[0].paintingName);
printf("Enter price:");
scanf("%lf", &painting[0].price);
printf("Enter the year of creation:");
scanf("%d", &painting[0].year);
fp = fopen (PATH, "a");//open for create/append text file (not write binary, "wb")
if(fp)
{
sprintf(stringToWrite, "Painting Code is: %s\n", painting[0].code);
fputs(stringToWrite, fp);
// do others same way...
//...
fclose(fp);
}
}
I am doing a program that is a customer order application. Basically it is a database (like e-bay) where a customer must be registered in the system to buy a product. The problem I am currently having is when I come to modify a customer.
Basically when I run the program I have a menu. I choose customer and I have a sub-menu and I choose to modify the customer data. then it asks me for an id and the program checks whether it is valid or not. If the id number is valid it goes on to let you choose which criteria you want to modify. After the modification of that criteria it prints out ~~~ITEM EDITED~~~ and then it just like freeze. Nothing comes afterwards and I have to terminate the program myself.
This happens as well when an invalid id is entered. When this happens it prints out the main menu and when I enter a number to choose from the main menu nothing happens.
I tried solving it myself but cannot find the problem. I tried to use exit(0) instead of break; after the cases but same problem happened. Here under is the code of the modify function. Just to let you know that I have a header file with all the necessary information such as structure declaration.
modify_customer()
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
char another;
int flag = 0;
int choice;
char x[8];
FILE *pCust;
int size;
printf("\nENTER CUSTOMER ID NUMBER: ");
//fflush(stdout);
scanf("%s",x);
flag = checkcustomer(x);
if(flag==0)
{
pCust=fopen("customer.txt","r+b");
rewind(pCust);
while(fread(&cust,sizeof(cust),1,pCust))
{
if(strcmp(cust.id,x)==0)
{
printf("\nid ==> %s",cust.id);
printf("\nname ==> %s",cust.name);
printf("\nsurname ==> %s",cust.surname);
printf("\naddress ==> %s",cust.address);
printf("\nDO YOU WANT TO EDIT THIS REDORD [Y/N]: ");
scanf("%s", &another);
fflush(pCust);
if(another=='Y'|| another=='y')
{
printf("\n(1) EDIT ID NUMBER");
printf("\n(2) EDIT NAME");
printf("\n(3) EDIT SURNAME");
printf("\n(4) EDIT ADDRESS");
printf("\nenter choice (1) - (4) to edit: ");
//fflush(stdout);
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nEDIT RECORD [ID]");
printf("\nenter new customerID:");
scanf("%s",cust.id);
size = sizeof(cust);
fseek(pCust,-size,SEEK_CUR);
fwrite(&cust,sizeof(cust),1,pCust);
break;
case 2 : printf("\nEDIT RECORD [NAME]");
printf("\nenter new name: ");
//fflush(stdout);
scanf("%s",cust.name);
size = sizeof(cust);
fseek(pCust,-size,SEEK_CUR);
fwrite(&cust,sizeof(cust),1,pCust);
break;
case 3 : printf("\nEDIT RECORD [SURNAME]");
printf("\nenter new surname: ");
//fflush(stdout);
scanf("%s",cust.surname);
size = sizeof(cust);
fseek(pCust,-size,SEEK_CUR);
fwrite(&cust,sizeof(cust),1,pCust);
break;
case 4 : printf("\nEDIT RECORD [ADDRESS]");
printf("\nenter new address: ");
//fflush(stdout);
scanf("%s",cust.address);
size = sizeof(cust);
fseek(pCust,-size,SEEK_CUR);
fwrite(&cust,sizeof(cust),1,pCust);
break;
}
printf("\n~~~ITEM EDITED~~~");
break;
}
else
{
printf("\nRECORDS ARE NOT MODIFIED");
customer_menu();
}
}
}
}
if(flag==1)
{
printf("\nITEM DOES NOT EXIST. TRY AGAIN");
main_menu();
}
fflush(stdout);
getch();
fclose(pCust);
return 0;
}
#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.