我在 C++ 中有一个班级作业,基本上希望我使用一个结构以及一个或两个数组,以允许客户查看和选择早餐菜单中的项目。每当我去运行程序时,它只会说它以返回值 0 退出。我尝试过抽查,但我无法弄清楚出了什么问题。我怀疑我可能没有正确加载文本文件中的数据,因此由于没有加载数据而导致没有结果。
这是我的代码:
//Student Name: Jacob Gillespie
//Date: 10/18/13
//Program: Breakfast Billing System
//Summary: Program allows customer to select different items from a menu and sums up
their total
//Headers
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Define structs
struct menuItemType
{
string menuItem;
double menuPrice;
};
//Declare variables and arrays
ifstream inData;
const double tax = 0.05;
int itemSelected[8];
menuItemType menuList[8];
//Provide function prototypes
void getData(ifstream& inFile);
void showMenu();
void printCheck();
void customerSelection();
//Main Program Execution
int main()
{
//Initialize itemSelected to 0
for (int counter = 0; counter < 8; counter++)
itemSelected[counter] = 0;
//Open input file
inData.open("menu.txt");
//Execute functions
void getData(ifstream& inData);
void showMenu();
void customerSelection();
void printCheck();
inData.close();
return 0;
}
//Function Definitions
//getData
void getData(ifstream& inFile)
{
for (int counter = 0; counter < 8; counter++)
{
inData >> menuList[counter].menuItem
>> menuList[counter].menuPrice;
}
}
//showMenu
void showMenu()
{
for (int counter = 0; counter < 8; counter++)
cout << menuList[counter].menuItem << " " << menuList[counter].menuPrice << endl;
}
//printCheck
void printCheck()
{
double total = 0;
double addedTax = 0;
for (int counter = 0; counter < 8; counter++)
if (itemSelected[counter] = 1)
{
cout << menuList[counter].menuItem << " " << menuList[counter].menuPrice << endl;
total = total + menuList[counter].menuPrice;
}
addedTax = total * tax;
cout << "Tax " << addedTax << endl;
cout << "Amount Due " << total << endl;
}
//customerSelection
void customerSelection()
{
string choice;
for (int counter = 0; counter < 8; counter++)
{
cout << "If you would like to order the item, " << menuList[counter].menuItem << ", please enter 'yes'. "
<< endl << "If not, please enter 'no'." << endl;
if (choice == "yes")
itemSelected[counter] = 1;
}
}