Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
#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')
		{
			/*printf("\nPlease Enter Food Code to Search : ");*/
			/*scanf("%s", &code);*/
		}
		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;
	/*int f=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++;
	}

	/*f = i;*/

	/*for (i = 0; i < f; i++)
	{
	printf("%s\t\t%s\t\t%.2f\t\t%s\n", m[i].code, m[i].name, m[i].price, m[i].description);
	}*/

	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");
}
Posted
Updated 2-Jan-18 5:40am
v2
Comments
CHill60 2-Jan-18 11:40am    
What is wrong with the code you already have?
Member 13603572 2-Jan-18 12:02pm    
my output got a lot of weird number and code
Member 13603572 2-Jan-18 12:03pm    
my description cant work well and i dont know how to add the delete menu function
ZurdoDev 2-Jan-18 11:58am    
Where are you stuck?
Member 13603572 2-Jan-18 12:05pm    
my output got a lot of weird number and code i think the description menu there cant work well and i dont know how to add a delete function

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:
C#
rewind(stdin);

and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Member 13603572 2-Jan-18 12:25pm    
can u help me to write a new program??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900