由于某种原因,我的小计变量没有存储用户输入的项目的价格有什么建议吗?我不知道我是否设置了我的循环错误或者是否必须将小计放在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;
}
}
答案 0 :(得分:1)
在您实际将数据放入其中之前,您似乎正在尝试使用subtotal
。
问题在于以下几点:
double tax = (subtotal * tax_rate);
double total = (tax + subtotal);
在程序中的那一点,subtotal
仍包含初始值,即0,因此这些计算的结果也是0.您需要将这些行放在之后循环,以便它们使用最终值subtotal
。
答案 1 :(得分:0)
结果是什么。小计将有
menuList[choice].ItemPrice value
如果你想改变
小计+ = menuList [choice] .ItemPrice;