-1

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

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

注意:我正在修复我的代码,使其无法运行。但是当它这样做时,代码在 2 个用户输入后中断,我不知道为什么。

这是我尝试编码的示例:

售货机:

- - 项目 - -

(以下列出 5 个)

你想买什么东西?输入“c”结帐,输入“a”添加商品,输入“r”删除商品。

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

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

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

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

如果是“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;
}
4

1 回答 1

1

要回答你的问题,你可以。

但是,无论如何,您都做得不对,您不需要所有这些转换,您可以item直接与 a进行比较,char或者int取决于您要求的输入类型。

我们还可以继续std::stoi将输入转换为其 int 值,但是,您必须注意 aphabetic 输入,当然,这些不会被转换,并且应该防止由于转换失败而导致的错误。

带有注释的半更正代码:

运行示例

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

您仍然有问题,例如输入验证和函数中的一些算术问题,我只是更正了与您提出的问题相关的问题,您应该尝试自己解决其他问题,这是最好的学习方式。

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

于 2020-08-13T21:00:25.793 回答