如何在 c++ 中使我的"subscripted value"成为数组



>我正在我的"自动售货机"代码上取得进展,用户可以将无限数量的商品添加到购物车中,直到他们按 0 结帐。我在"do-while"循环中遇到了数组cost[item]的问题,它说"下标值不是数组、指针或向量"。 有人可以帮我解决这个问题吗? 如果有人愿意帮助,我也有一些小问题。以下是我遇到的主要问题:

  1. 按 0 签出部分代码 - 我认为这个问题与我上面描述的cost[item]数组问题有关。

  2. 如何让我的菜单价格显示 2 位小数 - 如果这是正确的方法,我不确定在我的代码中cout << fixed << setprecision(2) << total;放在哪里。

  3. 如何在"结帐"时打印报表以显示具有总成本的项目。

我的完整代码:

#include <iostream>
#include <iomanip>
using namespace std;

string menuItems[5] = {"Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate"};
float cost[5] = {2, 3, 2.50, 1.50, 1};

void vendingMachine() {
for(int i = 0; i < 5; i++)
cout << i+1 << ". " << menuItems[i] << ": $" << cost[i] << endl;

}

int main() {
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;

float cost;
int total;
total = 0;


do {
cout << "Enter your selection: " << flush;
int item;
cin >> item;
item = item -1;
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];


} while (item != 0);
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;

cout << "Amount due: " << total << endl;

cout << "Insert money here: $" << flush;
float money;
cin >> money;
if (money > cost) {
float change = money-cost;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == cost) {
cout << "Thank you! Have a nice day!." << endl;
}
if (money < cost) {
float amountOwed = cost-money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: " << flush;
float payment;
cin >> payment;
if (payment > amountOwed) {
float change2 = payment-cost;
cout << "Thank you! You have $" << change2 << " change." << endl;
}
if (payment == amountOwed) {
cout << "Thank you! Have a nice day!." << endl;
}
if (payment < amountOwed) {
cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
}
}
return 0;
}

我会尽力帮助你理清你犯错的地方:

  1. 关于成本[项目]: 也许您已经听说过"范围"。这是关于变量、指针等的位置。在您的程序中,您在全局范围内声明了float cost[5] = {2, 3, 2.50, 1.50, 1};。此作用域是您在 main 函数之外声明某些内容的地方。此范围适用于此文件中的任何位置。但是,您还在本地范围内声明了具有相同名称的float cost;。本地范围仅适用于您声明此函数和更深层次的地方(在此函数的循环中,if 语句等);编译器优先级适用于局部变量(请注意,编译器允许您编写相同名称但作用域不同的变量)。 我已经纠正了您的错误,并随时添加评论:
#include <iostream>
#include <iomanip>
using namespace std;

string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };
float cost[5] = { 2, 3, 2.50, 1.50, 1 };
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;//you can place this options righе here if you want that it works on all numbers of your program
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;

//float costs;you dont need this local variable
//int total;//if your products have number after point (2.50, 1.50)
//it could be moment when total will be 2.5 and integer variable
//will convert it to 2 and you will lose 0.5 so choose float total
float total;
total = 0;
int item;
do {
cout << "Enter your selection: " << flush;
//int item was here. again it's local scope of do{}, "while" ouside this scope;
cin >> item;
//item = item - 1; you did it because you display products from 1
//but if client want to escape and press '0' it will be -1 and this loop never ends;
item = item - 1;
//here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
//use if(item!=-1) {
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];//}statement to avoid this bug
} while (item != -1);//right number  for your case to exit
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: " << total << endl;

cout << "Insert money here: $" << flush;
float money;
cin >> money;
if (money > total) {//Here and below you should to compare money with total
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
if (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: " << flush;
float payment;
cin >> payment;
if (payment > amountOwed) {
float change2 = payment - total;
cout << "Thank you! You have $" << change2 << " change." << endl;
}
if (payment == amountOwed) {
cout << "Thank you! Have a nice day!." << endl;
}
if (payment < amountOwed) {
cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
}
}
return 0;
}

我试图尽量减少这些更改,这样您就不会浪费时间试图弄清楚这些更改。我认为你刚刚开始学习这种语言,所以我们不应该用术语让你超负荷。但是要知道,这段代码远非完美,随着经验的积累,你会明白这一点,祝你好运。

最新更新