变量不会存储数据
Variable won't store data
由于某种原因,我的小计变量不存储用户输入的商品的价格,有什么建议吗?我不知道我是否设置了错误的循环,或者小计是否必须放在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;
相关文章:
- 将从格式化文本文件读取的文本数据存储到链表
- 将线程中的数据存储到全局容器的最佳方法?
- 将数据存储在内存中以供以后访问
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 如何将函数的输出数据存储在文本文件中
- 将结构数据存储在循环缓冲区中
- 安卓中的Qt和应用程序数据存储
- C++ 将数据存储在对齐数组的中间
- 如何删除字符串中的所有空格并将数据存储在两者之间
- 实现双链接列表,其中数据存储在堆上
- 是否有一种方法可以从文本文件中读取并将单个数据存储为不同的变量
- 了解字符数组初始化和数据存储
- 如何在C++中将字符串数据存储在字符串 2D 数组中
- 将数据存储在具有可互换 x/y 坐标的 2D 矩阵中
- 将文件中的数据存储到数组中
- 将数据存储到地图
- 保存和加载类数据存储在C 中的二进制文件中
- 将由结构构建的数组传递到将数据存储到文件中的函数
- 在这种情况下,将数据存储在诸如SQLite之类的数据库中,还是存储在平面文本文件中更好
- 将数据存储在char数组中导致变量周围损坏