0
我在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;
}
}
更重要的一點。那些'void'-s導致所有這些函數成爲*聲明*而不是*調用*。作爲聲明,他們完全沒有*。 – WhozCraig
非常感謝你們!它現在有效!這很愚蠢,應該是多麼明顯,但是這表明如何從外部看東西可以讓你發現甚至是小事情。 – TheGuy101