c++ - 编辑文本文件中的项目c ++
问题描述
我为餐厅创建了一个菜单系统。使用数组,我创建了一个系统,用于向菜单添加新项目并将它们打印在文本文件中。这一切都很好。
当我需要编辑一个项目时会出现问题,例如,如果我添加了 3 个菜肴并使用编辑选项,则仅编辑文本文件的第一行,并且当最小值应始终为 1 时,项目编号返回 0 .
我怎样才能使它工作,所以如果我编辑第 3 项,编辑将显示在第 3 行的文本文件中,而不是覆盖第 1 行?
我不确定问题出在哪里,所以我附上了所有项目文件,因为它不是太长。
先感谢您
.h 文件:
#ifndef RESTAURANTMENU_ITEMS_H
#define RESTAURANTMENU_ITEMS_H
#define MAX_ITEMS 20
#include <iostream>
#include <string>
#include <limits>
#include <fstream>
class Item{
private:
int itemNumber;
std::string itemCategory;
std::string itemDescription;
double itemPrice;
public:
Item();
//setter function
void setItemDetails();
void editItemDetails();
//getter function
void printItemDetails();
//save to file
void save(std::ofstream &outfile);
};
#endif //RESTAURANTMENU_ITEMS_H
实现文件:
#include "item.h"
//constructor
Item::Item(){
itemNumber = 0;
itemCategory = "Item not categorised.";
itemDescription = "No description written.";
itemPrice = 0.0;
}
//setter functions
void Item::setItemDetails() {
int choice;
static int counter = 1;
itemNumber = counter++;
std::string copyCategory;
std::cout << "What category is this item?" << std::endl;
std::cout << "1. Meat Dish\n2. Fish Dish\n"
"3. Vegetarian Dish\n4. Drink\n";
std::cin >> choice;
switch (choice) {
case 1:
itemCategory = "Meat";
break;
case 2:
itemCategory = "Fish";
break;
case 3:
itemCategory = "Vegetarian";
break;
case 4:
itemCategory = "Drink";
break;
default:
std::cout << "Error. Please enter a number between 1-4: " << std::endl;
std::cin >> choice;
}
std::cout << "Please enter a short description: " << std::endl;
std::cin.ignore(100, '\n');
std::getline(std::cin, itemDescription);
std::cout << "Please set the price: " << std::endl;
std::cout << "£";
if (!(std::cin >> itemPrice)) {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Error. Please enter a number: ";
}
}
void Item::editItemDetails() {
int choice;
std::string copyCategory;
std::cout << "What category is this item?" << std::endl;
std::cout << "1. Meat Dish\n2. Fish Dish\n"
"3. Vegetarian Dish\n4. Drink\n";
std::cin >> choice;
switch (choice) {
case 1:
itemCategory = "Meat";
break;
case 2:
itemCategory = "Fish";
break;
case 3:
itemCategory = "Vegetarian";
break;
case 4:
itemCategory = "Drink";
break;
default:
std::cout << "Error. Please enter a number between 1-4: " << std::endl;
std::cin >> choice;
}
std::cout << "Please enter a short description: " << std::endl;
std::cin >> itemDescription;
//std::cin.ignore(100, '\n');
//std::getline(std::cin, itemDescription);
std::cout << "Please set the price: " << std::endl;
std::cout << "£";
//if (!(std::cin >> itemPrice)) {
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//std::cout << "Error. Please enter a number: ";
std::cin >> itemPrice;
//}
}
//getter functions
void Item::printItemDetails(){
std::cout << "\nItem Number: " << itemNumber << "| Category: "
<< itemCategory << "| Description: " << itemDescription << "| Price: £" << itemPrice;
}
//save to file
void Item::save(std::ofstream &outfile) {
outfile << "\nItem Number: " << itemNumber << "| Category: "
<< itemCategory << "| Description: " << itemDescription << "| Price: £" << itemPrice;
}
主文件
#include <iostream>
#include "item.h"
int main() {
Item newDish[MAX_ITEMS];
bool exit = false;
int choice;
int count = 0;
std::ofstream saveFile;
saveFile.open("menu.txt", std::ios::in | std::ios::out);
while (!exit) {
std::cout << "Menu Creation Terminal\n\n" << std::endl;
std::cout << "\tWelcome to Wrapid™ Restaurants\n\n\t\tMenu Creation Terminal\n\n" << std::endl;
std::cout << "1. Add a new dish\n2. Edit Current Menu\n3. Quit\n" << std::endl;
std::cout << "Please select an option: ";
std::cin >> choice;
switch (choice) {
case 1: {
int option = true;
int i;
//create items
std::cout << "Item Creation Menu";
for (i = 0; i < MAX_ITEMS; i++) {
count += 1;
std::cout << "\n\nItem number: " << i+1 << "\n\n";
newDish[i].setItemDetails();
newDish[i].save(saveFile);
std::cout << "Would you like to add another item?" << std::endl;
std::cout << "1. Yes\n2. No" << std::endl;
std::cin >> option;
if (option == 2) {
break;
}
std::cout << "You have added the following items: " << std::endl;
newDish[i].printItemDetails();
}
}
break;
case 2: {
int editOpt;
int i;
//edit items
std::cout << "Edit Current Menu\n\n" << std::endl;
for (i = 0; i < count; i++) {
newDish[i].printItemDetails();
}
std::cout << "\n\nPlease enter the item number of the item you would like to edit: " << std::endl;
std::cin >> editOpt;
while(editOpt > 20) { std::cout << "Error. Limited to 20 items.\n"
"Please try again: "; std::cin >> editOpt; }
i = editOpt-1;
newDish[i].editItemDetails();
newDish[i].save(saveFile);
}
break;
case 3: {
std::cout << "Thanks for using this terminal. Have a nice day." << std::endl;
exit = true;
}
break;
default: {
std::cout << "Error. Invalid selection. Please select a valid option: " << std::endl;
std::cin >> choice;
}
}
}
saveFile.close();
return 0;
}
解决方案
在 main 中更改此行,因为每次您覆盖文件时,请始终以附加模式打开文件
std::ofstream saveFile;
saveFile.open("menu.txt", std::ios::in | std::ios::out | std::ios_base::app);
希望这能解决你的问题
推荐阅读
- android - 理论:如何在后台观察和发布状态/位置?
- c# - 如何在 Unity 中随机生成一个不与我的游戏地形发生冲突的预制件
- hybris - Hybris 在 Hana 中创建的 Double 和 BigDecimal 属性的问题
- r - 如何按时间而不是日期对数据集进行子集化?
- mysql - Mysql 中的连接或函数,用于包含一列的所有唯一项和第二列的所有唯一项
- java - 如何使用 api 调用在 Sprinboot 后端传递枚举?
- reactjs - 如何更改输入
当人们打字的时候? - android - 用户为 React-native App 上传 FontFamily
- swiftui - 检测到长按手势并停止时如何运行功能?
- android - 调试和发布 apk 在 React Native 0.63.3 中不起作用