如何解决我可以询问并存储可以是数字或字符的输入,并将其用于条件表达式吗?
我正在尝试在下面的“自动售货机”代码中使用stroi
。在尝试添加允许用户输入字符(“ c”表示结帐,“ a”表示添加和“ r”表示删除)的功能之前,我有一台可以使用的自动售货机。由于我的用户输入既可以是int
也可以是char
,所以我意识到必须将字符串转换为数字。我已经检查了参考文献,但没有一个给出如何将其与向量一起使用的示例。有人可以帮忙吗?
当前,在主体int myint1 = stoi(item);
语句的if
行中有一个错误。它说“使用未声明的标识符'i'。”
注意:我正在修复我的代码,因此它无法运行。但是这样做的时候,代码在两次用户输入后就中断了,我不确定为什么。
以下是我要编写代码的示例:
自动售货机:
----项目----
(下面列出了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,or r to remove item: " << flush;
}
解决方法
可以回答您的问题。
无论您是否需要进行所有这些转换,无论您做得如何,都可以直接将item
与取决于您输入类型的char
或int
进行比较要求。
我们还可以继续使用std::stoi
将输入转换为其int值,但是,您必须保护无脂肪的输入,这些将不会被转换,当然,从失败的转换中得出的错误应被预防。
带有注释的半正确代码:
string input; //string input
int item = 0;
float total;
void enterSelection()
{
total = 0;
int flag = 0; //flag for input errors
cout << "Enter your selection: ";
cin >> input; //input as string
try
{
item = stoi(input); //convert to int
}
catch (exception &e) //if string cannot be converted
{
//std::cout << e.what();
flag = -1; //if not set flag
}
if (flag != -1 && item < 5 && item > 0) //only execute if no exeception cached
{
item--;
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
flag = 0; //reset flag
}
}
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;
}
exit(EXIT_SUCCESS); //exit after checkout
}
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();
while(1) // infinite cycle will exit in checkout
if (!input.compare("c")) //string has a compare method similar to C strcmp
{
checkout();
}
else if (!input.compare("a"))
{
add();
}
else if (!input.compare("r"))
{
remove();
}
else
{
cout << "Please enter a number or press c to checkout,a to add item,or r to remove item: ";
enterSelection(); //execute enter selection again
}
return 0;
}
您仍然遇到问题,例如输入验证和函数中的某些算术问题,我刚刚纠正了与您所要求的问题有关的问题,您应该尝试自己解决其他问题,这是学习的最佳方法。
一点一点地构建您的代码,了解它的功能并对其进行测试,然后继续进行下去,如果您必须提出问题,使其专注于此,则这种调试工作在这里并不常见,问题必须是具体的,并完整地发布通常,对于我们检查您的错误的代码通常不被接受,因为您可以看到问题和我的答案中票数不足。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。