我可以请求和存储既可以是数字也可以是字符的输入并在条件表达式中使用它吗?

Can I ask and store input that can be both numbers or characters and use it in conditional expressions?

我正在尝试在下面的“自动售货机”代码中使用 stroi。在我尝试添加允许用户输入字符(“c”表示结帐,“a”表示添加,“r”表示删除)的功能之前,我有一台可用的自动售货机。由于我的用户输入可以是 intchar 我意识到我必须将字符串转换为数字。我已经检查了参考资料,但 none 给出了一个如何将它与矢量一起使用的示例。有人可以帮忙吗?

目前,在 main 中的 if 语句中的 int myint1 = stoi(item); 行有一个错误。它说“使用未声明的标识符 'i'”。

注意:我正在修复我的代码,所以它不会 运行。但是当它这样做时,代码在 2 次用户输入后中断,我不确定为什么。

这是我尝试编码的示例:

自动售货机:

----项目数----

(下面列出 5 个)

您想购买什么商品?输入“c”结帐,输入“a”添加项目,输入“r”删除项目。

-如果用户输入的是整数,则运行 enterSelection 函数

-如果用户输入的是字符(c、a 或 r),则:

如果 "c" ,则 运行 checkout 函数

如果是“a”,您想添加什么项目,价格是多少?然后相应地附加到 menuItemscost

如果是“r”,您想删除什么项目(输入一个数字)?然后 erase 来自 menuItems 的项目。

然后打印:“用户输入”已从菜单中 added/removed。

再次显示菜单时,将显示用户编辑。

是的,我知道我的代码还有很多我不知道如何解决的问题。

完整代码:

#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;
}

回答你的问题,你可以。

但是你做的不正确,无论如何,你不需要所有这些转换,你可以将 item 直接与 charint 进行比较,具体取决于类型输入您要求的内容。

我们还可以继续std::stoi将输入转换为它的int值,但是你必须注意非字母输入,这些将不会被转换,当然,由此产生的错误失败应该阻止转换。

Semi-corrected 带有注释的代码:

Running sample

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;
}

你还有问题,比如输入验证和函数中的一些算术问题,我只是纠正了与你问的相关的问题,你应该尝试自己解决其他问题,这是最好的学习方法。

一点一点地构建你的代码,了解它的作用并测试它,然后继续,如果你必须提出问题,让它们集中注意力,这种调试工作在这里并不常见,问题必须具体,发布您用于我们检查您的错误的整个代码通常没有被很好地接受,因为您可以在您的问题和我的回答中看到缺少投票。