我正在尝试stroi
在下面的“自动售货机”代码中使用。在我尝试添加一个允许用户输入字符的功能(“c”用于结帐,“a”用于添加,“r”用于删除)之前,我有一台可以工作的自动售货机。由于我的用户输入可以是两者int
,char
我意识到我必须将字符串转换为数字。我检查了参考资料,但没有一个例子说明如何将它与向量一起使用。有人可以帮忙吗?
目前,主语句中的int myint1 = stoi(item);
行中存在错误。if
它说“使用未声明的标识符'i'”。
注意:我正在修复我的代码,使其无法运行。但是当它这样做时,代码在 2 个用户输入后中断,我不知道为什么。
这是我尝试编码的示例:
售货机:
- - 项目 - -
(以下列出 5 个)
你想买什么东西?输入“c”结帐,输入“a”添加商品,输入“r”删除商品。
- 如果用户输入是整数,则运行enterSelection
函数
- 如果用户输入是字符(c、a 或 r),则:
如果 "c" ,则运行checkout
函数
如果“a”,您想添加什么项目以及价格是多少?然后附加到menuItems
并cost
相应地。
如果是“r”,您想删除什么项目(输入一个数字)?然后erase
项目来自menuItems
。
然后打印:“用户输入”已从菜单中添加/删除。
再次显示菜单时,将显示用户编辑。
是的,我知道我的代码还有很多我不知道如何解决的问题。
完整代码:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
int item;
float total;
std::vector<string> menuItems = {"Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate"};
std::vector<float> cost = {2, 3, 2.50, 1.50, 1};
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
void enterSelection() {
total = 0;
cout << "Enter your selection: " << flush;
cin >> item;
item = item - 1;
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
}
void checkout() {
cout << " " << endl;
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: $" << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
money += payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
}
void add() {
cout << "What item would you like to add: " << flush;
string add;
cin >> add;
menuItems.push_back(add);
cout << "For what price: $" << flush;
int price;
cin >> price;
cost.push_back(price);
cout << add << " for $" << price << "has been added to the menu." << endl;
}
void remove() {
cout << "What item would you like to remove (enter a number): " << flush;
int remove;
cin >> remove;
menuItems.erase (menuItems.begin()+remove);
cout << remove << " has been removed from the menu." << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter c to checkout" << endl;
cout << "Enter a to add items" << endl;
cout << "Enter r to remove items" << endl;
enterSelection();
if (item != 'c' && item != 'a' && item != 'r') {
int myint1 = stoi(item);
enterSelection();
} else if (item == 'c') {
checkout();
} else if (item == 'a') {
add();
} else if (item == 'r') {
remove();
}
else {
cout << "Please enter a number or press c to checkout, a to add item, or r to remove item: " << flush;
}
return 0;
}
代码的主要问题段:
if (item != 'c' || item != 'a' || item != 'r') {
int myint1 = stoi(item);
enterSelection();
} else if (item == 'c') {
checkout();
} else if (item == 'a') {
add();
} else if (item == 'r') {
remove();
}
else {
cout << "Please enter a number or press c to checkout, a to add item, or r to remove item: " << flush;
}