how to write a text file to do a food menu that include
1.Add Menu
2.Search Menu
3.Modify Menu
4.Display Menu
5.Delete Menu
just for a C++ beginner programmer
What I have tried:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<windows.h>
#include<string.h>
#pragma warning(disable:4996)
void mainMenu();
void addMenu();
void searchMenu();
void modifyMenu();
void displayMenu();
typedef struct
{
char code[10];
char name[31];
double price;
char description[100];
}Menu;
void addMenu()
{
Menu menu;
FILE*add;
char cont;
add = fopen("menu.txt", "a");
do
{
printf("Please enter the food code:");
rewind(stdin);
scanf("%[^\n]", menu.code);
printf("Please enter the name of the food:");
rewind(stdin);
scanf("%[^\n]", menu.name);
printf("Please enter the price:RM ");
rewind(stdin);
scanf("%.2f", &menu.price);
printf("Please enter the description of the food:");
rewind(stdin);
scanf("%[^\n]", &menu.description);
fprintf(add, "%s:%s:%.2f:%s\n", menu.code,menu.name,menu.price,menu.description);
printf("Added Successfully!!\n");
printf("Do you want to add more records (Y/N) ? :");
rewind(stdin);
scanf("%c", &cont);
system("cls");
} while (toupper(cont) == 'Y');
fclose(add);
return;
system("cls");
}
void searchMenu()
{
char code[10];
char name[31];
double price;
char description[100];
int i = 0, found;
char cont;
Menu m[100];
FILE *search;
search = fopen("menu.txt", "r");
while (fscanf(search, "%[^:]:%[^:]:%.2f:%[^:]\n", &m[i].code, &m[i].name, &m[i].price, &m[i].description) != EOF)
{
i++;
}
do
{
printf("\nPlease Enter food code to Search : ");
rewind(stdin);
scanf("%[^\n]", &code);
found = 0;
printf("Food Code\tName\t\t\tPrice\tDescription\n");
printf("=========\t====\t\t\t======\t===========\n");
for (i = 0; i < 100; i++)
{
if (strcmp(code, m[i].code) == 0)
{
found = 1;
printf("%s\t\t%s\t\tRM%.2f\t%s\n", m[i].code, m[i].name, m[i].price, m[i].description);
printf("\nMenu found !!~\n");
}
}
if (!found)
{
printf("\nInvalid food code!\n");
}
printf("\nWould you like to search again ? (Y/N)\n");
rewind(stdin);
scanf("%c", &cont);
system("cls");
if (toupper(cont) == 'Y')
{
}
else if (toupper(cont) == 'N')
{
printf("\nSEARCH COMPLETED !\n\n ");
}
else
{
printf("\nINVALID INPUT !\n\n ");
}
} while (toupper(cont) == 'Y');
fclose(search);
return;
}
void modifyMenu()
{
char ans, cont;
char code[6], updName[20], updDescription[100];
int i = 0, modiCount = 0, updPrice;
int menuCount, found;
Menu m[100];
FILE*fp, *modify;
fp = fopen("menu.txt", "r");
while (fscanf(fp, "%[^:]:%[^:]:%.2f:%[^:]\n", &m[i].code, &m[i].name, &m[i].price, &m[i].description) != EOF)
{
i++;
}
menuCount = i;
do
{
printf("Please enter food code to be modified:");
rewind(stdin);
scanf("%[^\n]", &code);
found = 0;
printf("Food Code\tName\tPrice\tDescription\n");
printf("=========\t====\t======\t==========\n");
for (i = 0; i < menuCount; i++)
{
if (strcmp(code, m[i].code) == 0)
{
found = 1;
printf("%s\t\t%s\t\tRM%.2f\t\t%s\n", m[i].code, m[i].name, m[i].price, m[i].description);
printf("Please enter new name of the food:");
rewind(stdin);
scanf("%[^\n]", updName);
printf("Please enter new price of the food:RM ");
rewind(stdin);
scanf("%c", &updPrice);
printf("Please enter new description:");
rewind(stdin);
scanf("%[^\n]", &updDescription);
printf("Confirm to modify (Y/N)?:");
rewind(stdin);
scanf("%c", &ans);
system("cls");
if (toupper(ans) == 'Y')
{
strcpy(m[i].name, updName);
m[i].description == updDescription;
m[i].price = updPrice;
modiCount++;
}
else
{
printf("Back to main menu.\n");
mainMenu();
system("cls");
}
printf("\nUpdated List:\n");
printf("Food Code\tName\tPrice\tDescription\n");
printf("=========\t====\t======\t==========\n");
printf("%s\t\t%s\t\tRM%.2f\t\t%s\n", m[i].code, m[i].name, m[i].price, m[i].description);
}
}
if (found == 0)
{
printf("\n%s Food Code not found!\n", code);
}
printf("More Food Code to modify (Y/N)?:");
rewind(stdin);
scanf("%c", &cont);
system("cls");
} while (toupper(cont) == 'Y');
modify = fopen("menu.txt", "w");
for (i = 0; i < menuCount; i++)
{
fprintf(modify, "%s:%s:%.2f:%s\n", &m[i].code, &m[i].name, &m[i].price, &m[i].description);
}
printf("\n%d of Menu Code modified.\n", modiCount);
system("cls");
fclose(fp);
fclose(modify);
return;
}
void displayMenu()
{
Menu m[100];
FILE *display;
int count = 0, i = 0;
display = fopen("menu.txt", "r");
if (display == NULL)
{
printf("Error!!...");
exit(-1);
}
printf("Food Code\tName\t\tPrice\tDescription\n");
printf("=========\t====\t\t=====\t===========\n");
while (fscanf(display, "%[^:]:%[^:]:%.2f:%[^:]\n", &m[i].code, &m[i].name, &m[i].price, &m[i].description) != EOF)
{
printf("%s\t\t%s\t\tRM%.2f\t%s\n", m[i].code, m[i].name, m[i].price, m[i].description);
i++;
}
printf("\n\n%d Menu listed\n\n", i);
fclose(display);
return;
}
void mainMenu()
{
int userChoice;
do
{
printf("============================\n");
printf(" Welcome To PAPARICH !!!\n");
printf("============================\n");
printf("Food Menu\n");
printf("---------------------\n");
printf("1. Add menu\n");
printf("2. Search menu\n");
printf("3. Modify menu\n");
printf("4. Display menu\n");
printf("5. Exit\n");
printf("\nEnter your option:");
scanf("%d", &userChoice);
rewind(stdin);
system("cls");
switch (userChoice)
{
case 1:
addMenu();
break;
case 2:
searchMenu();
break;
case 3:
modifyMenu();
break;
case 4:
displayMenu();
break;
case 5:
break;
default:
printf("Please enter available choices only!\n");
}
} while (userChoice != 5);
return;
}
void main()
{
mainMenu();
system("pause");
}