变量不会存储数据

Variable won't store data

本文关键字:数据 存储 变量      更新时间:2023-10-16

由于某种原因,我的小计变量不存储用户输入的商品的价格,有什么建议吗?我不知道我是否设置了错误的循环,或者小计是否必须放在if-else语句之外,但我不知道如何从用户输入的中存储什么

#include <iostream>
#include <iomanip>
#include <string> 
#include <fstream>
#include <cstdlib>
using namespace std; 
const double tax_rate = 0.05;
struct menuItemType
{
string ItemName;
double ItemPrice;
};
void getData(ifstream &in, menuItemType menuList[]);
void showMenu(menuItemType menuList[]);
void printCheck(menuItemType menuList[]);

int main()
{
menuItemType menuList[10];
menuList[10].ItemName; 
menuList[10].ItemPrice;
ifstream in;
in.open("Menu.txt");
cout << "Welcome to Johnny's Breakfast Diner!" << endl;
getData(in,menuList);
showMenu(menuList);
    return 0;
}
void getData(ifstream &in, menuItemType menuList[])
{
int i = 0;
while (!in.eof())
{
    in >> menuList[i].ItemName >> menuList[i].ItemPrice;
    i++;
}
}
void showMenu(menuItemType menuList[])
{
int j = 0;
char answ;
cout << fixed << setprecision(2);
cout << "Would you like to take a look at our menu? (Y/N)";
cin >> answ;
if (answ == 'Y' || answ == 'y')
{
    cout << left << setw(10) << "Item#" << left << setw(15) << "Item" << left     << setw(18) <<  "Price" << endl;
    do {
    {
        cout << left << setw(8) << j << " " << left << setw(15) << menuList[j].ItemName << " " << "$" <<  menuList[j].ItemPrice << endl;
        j++;
    }
} while (j < 8);
    printCheck(menuList);
}
else if (answ == 'N' || answ == 'n')
{
    system("cls");
    cout << "Have a good day!" << endl;
}
}
void printCheck(menuItemType menuList[])
{
char answ;
int choice;
bool menu = true;
double subtotal = 0;
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
cout << "Would like to place your order (Y/N)"; 
cin >> answ;
if (answ == 'Y' || answ == 'y') 
{
    cout << "Please enter the number of the item, 8 to finish order:";
    do {
        cin >> choice; 
        if (choice == 0)
        {
        cout << menuList[0].ItemName << " " << "$" << menuList[0].ItemPrice << endl;
        subtotal = subtotal + menuList[0].ItemPrice; \ for some reason here it doesn't store the prices have no idea why 
        }
        else if (choice == 1)
        {
            cout << menuList[1].ItemName << " " << "$" << menuList[1].ItemPrice << endl;
            subtotal = subtotal + menuList[1].ItemPrice;
        }
        else if (choice == 2)
        {
            cout << menuList[2].ItemName << " " << "$" << menuList[2].ItemPrice << endl;
            subtotal = subtotal + menuList[2].ItemPrice;
        }
        else if (choice == 3)
        {
            cout << menuList[3].ItemName << " " << "$" << menuList[3].ItemPrice << endl;
            subtotal = subtotal + menuList[3].ItemPrice;
        }
        else if (choice == 4)
        {
            cout << menuList[4].ItemName << " " << "$" << menuList[4].ItemPrice << endl;
            subtotal = subtotal + menuList[4].ItemPrice;
        }
        else if (choice == 5)
        {
            cout << menuList[5].ItemName << " " << "$" << menuList[5].ItemPrice << endl;
            subtotal = subtotal + menuList[5].ItemPrice;
        }
        else if (choice == 6)
        {
            cout << menuList[6].ItemName << " " << "$" << menuList[6].ItemPrice << endl;
            subtotal = subtotal + menuList[6].ItemPrice;
        }
        else if (choice == 7)
        {
            cout << menuList[7].ItemName << " " << "$" << menuList[7].ItemPrice << endl;
            subtotal = subtotal + menuList[7].ItemPrice;
        }
        else if (choice == 8)
        {
            break;
        }
    }while(menu = true);
cout << "Taxes" << "$" << tax << endl;
cout << "Amount Due" << "$" << total << endl;
}

else if (answ == 'N' || answ == 'n') 
{
    system("cls");
    cout << "Ok, maybe I can help you at a later time." << endl;
}

}

在实际将数据放入subtotal之前,您似乎正在尝试使用它。

问题是这些线路:

double tax = (subtotal * tax_rate);
double total = (tax + subtotal);

在程序的这一点上,subtotal仍然包含初始值,即0,因此这些计算的结果也是0。您需要将这些行放在循环之后,以便它们使用subtotal的最终值。

结果是什么。小计将具有
menuList[choice].ItemPrice value

如果您想更改

小计+=menuList[选项].ItemPrice;