从文本文件输入数据时可能遇到问题

Possibly having trouble with inputting data from a text file

本文关键字:遇到 问题 数据 文本 文件 输入      更新时间:2023-10-16

我在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;
    }
}

main 中所有函数调用之前的 void s 在你有的地方是不必要的

//Execute functions