为什么不'我的自动售货机程序工作正常吗



程序应该从询问是否重新进货或继续使用当前库存开始。情况1(重新进货(工作得很好,但第二种情况是,如果任何产品为零,则始终返回零。

在我的文本文件中:

  1. 牛奶:10
  2. 鸡蛋:2个
  3. 水:7
  4. Burrito:10
  5. 面包:12
  6. 出口

如何修复?

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
string productName[5] = { "Milk", "Eggs", "Water", "Burrito", "Bread" };
//int productAmount[5] = { 5,12,10,4,7};
int productAmount[5];
int productPick;
int defaultPick;
int productBuy;
fstream productFile; //we create file 
void loadFromFile()
{
productFile.open("productsfile.txt", ios::in);
if (productFile.good() == false)
{
cout << "Unable to load the file. Try again later." << endl;
productFile.close();
exit(0);
}
else
{
ifstream productFile("productsfile.txt");
if (productFile.is_open())
{
cout << "How may I help you?" << endl;
string line;
while (getline(productFile, line))
{
// using printf() in all tests for consistency
cout << line.c_str() << endl;
}
productFile.close();
}
}
}
void saveToFile() //this function saves in the text file the data we've globally declared. It is used only if you want to declare new variables.
{
productFile.open("productsfile.txt", ios::out);
for (int i = 0; i < 5; i++)
{
productFile << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
}
productFile << "6. Exit" << endl;
productFile.close();
}
void askIfDefault()
{
cout << "Do you want to come back to default stock?" << endl;
cout << "1. Yes " << "2. No " << endl;
cin >> defaultPick;
switch (defaultPick)
{
case 1:
for (int i = 0;i < 5;i++)
{
productAmount[i] = 10;
}
saveToFile();
loadFromFile();
break;
case 2:
loadFromFile();
break;
default:
cout << "I don't understand." << endl;
exit(0);
break;
}
}
void productCheck()
{
if (productAmount[productPick - 1] <= 0 || productAmount[productPick - 1] < productBuy)
{
cout << "Unfortunately we have no more " << productName[productPick - 1] << " in stock. Please choose other product from the list below: " << endl;
productAmount[productPick - 1] = 0;
}
else
{
productAmount[productPick - 1] -= productBuy;
}
}

void listOfProducts()
{
cout << "How may I help you?" << endl;
for (int i = 0; i < 5; i++)
{
cout << i + 1 << ". " << productName[i] << ": " << productAmount[i] << endl;
}
cout << "6. Exit" << endl;
}
void order()
{
cin >> productPick;
switch (productPick)
{
case 1:
cout << "How many bottles?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 2:
cout << "How many cartons?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 3:
cout << "How many multi-packs?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 4:
cout << "How many portions?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 5:
cout << "How many batches?" << endl;
cin >> productBuy;
{
productCheck();
saveToFile();
}
break;
case 6:
cout << "See you soon!" << endl;
saveToFile();
system("pause");
break;
case 666:
cout << "You cannot use the secret magic spells here." << endl;
saveToFile();
exit(0);
break;
default:
cout << "Please pick the existing product: " << endl;
saveToFile();
order();
break;
}
}

int main()
{
askIfDefault();
order();
cout << endl;
while (true && productPick != 6)
{
listOfProducts();
order();
saveToFile();
cout << endl;
}
return 0;

}

除非声明一个全局fsteam productFile,否则请尝试在使用它的两个函数中的每一个函数中声明it:分别为'loadFromFile(('和'saveToFile((]。在他们开始的时候。那应该没事了。

让我对您的代码提出一些额外的建议,因为它有点难以理解:

  • 选择反映函数功能的函数名称,不要在函数中做任何超出其名称指示的事情。例如,如果您编写了一个名为ask_whether_to_restock()的函数——该函数应该提出问题,甚至可能得到答案,但即使答案是"是",也不会实际重新进货——也不会向文件中写入任何内容。甚至从文件中读取信息也有点过分
  • 如果你需要在一个函数中做比它名字所暗示的更多的事情,那么就为额外的工作写另一个函数,再写一个函数来调用前两个函数中的每一个,并将它们的作用组合在一起。例如,determine_whether_to_restock()可以调用从文件中读取的read_current_stock_state(),也可以调用print_stock_state()get_user_restocking_choice()
  • 尽量避免全局变量。更倾向于向每个函数传递它需要使用的变量(或者在必要时引用/指针(
  • 不要重复自己(DRI(:不要重复switch(produtPick)语句,试着用以下方法写一些东西:

    cout << "How many " << unit_name_plural[productPick] << "?" << endl;
    

    带有附加字符串阵列的"瓶"、"罐"、"部分"等

最新更新