如何使用stoi区分char输入和整数输入?

问题描述

我正在尝试编写一个代码,允许用户从“自动售货机”中添加或删除菜单项。我有自动售货机代码,但是如何在菜单中添加或删除项目?我为结帐设置了“ c”,为添加项设置了“ a”,为删除项设置了“ r”。 我已经编辑了我的代码(请在底部检查已编辑的代码),以便所有内容都在一个函数中,但是我在将integer输入和char输入分隔为主if statement时遇到麻烦。我知道存在语法错误,但是我不确定如何使用stoi。有什么建议?谢谢!

我想要的输出示例:

自动售货机:

----项目----

(下面列出了5个)

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

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

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

  1. 如果为“ c”,则运行checkout函数

  2. 如果为“ a”,您想添加什么项目以及价格是多少?然后添加到menuItems

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

然后打印:“用户输入”已从菜单中添加/删除。

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

以下代码:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
**input item;** //here is my error

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;

}


int main() {

    cout.precision(2);
    cout << std::fixed;

    cout << "Vending Machine" << endl;
    cout << "----Items------" << endl;

    vendingMachine();
    cout << "Enter 0 to checkout" << endl;
    cout << "Enter a to add items" << endl;
    cout << "Enter r to remove items" << endl;
    
    
    float total;
    total = 0;

    

    do {
        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];

    } while (item != -1); {

    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;
    }
    } while (item != "a") {
        if (item == "a") {
        cout << "What item would you like to add: " << flush;
        string add;
        cin >> add;
        menuItems.push_back(add);
      }
    } while (item != "r") {
        if (item == "r") {
      cout << "What item would you like to remove: " << flush;
      string remove;
      cin >> remove;
      menuItems.erase(remove);
      }
    }

    
    return 0;
}

编辑后的代码:(更有条理,但无法运行)

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

stringstream item;
float total;

std::vector<string> menuItems = {"Popcorn",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);
    
}

void remove() {
    cout << "What item would you like to remove (enter a number): " << flush;
    int remove;
    cin >> remove;
    menuItems.erase(remove);
}


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 == int) {
    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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...