I am trying to use stroi
in my "vending machine" code below. I had a working vending machine before I tried to add a feature that allows users to enter characters ("c" for checkout, "a" for add, and "r" for remove) as well. Since my user input can be both int
and char
I realized I had to convert strings to numbers. I have checked references but none give an example of how to use it with vectors. Can someone please help?
Currently, there is an error located at the int myint1 = stoi(item);
line in the if
statement in the main. It says "use of undeclared identifier 'i'."
Note: I am fixing my code so it does not run. But when it did, the code breaks after 2 user inputs and I am not sure why.
Here is an example of what I am trying to code:
Vending machine:
----Items----
(5 listed below)
what item would you like to buy? Enter "c" to checkout, "a" for add item and "r" for remove item.
-If user input is an integer, then run the enterSelection
function
-If user input is a character (c, a, or r) , then:
if "c" , then run checkout
function
if "a", What Item would you like to add and for what price? Then append to menuItems
and cost
accordingly.
if "r", what item would you like to remove (enter a number)? Then erase
the item from menuItems
.
Then print: "User input" has been added/removed from the menu.
When the menu is displayed again, the user edit will show.
And yes, I know there are many more problems with my code that I do not know how to fix.
Full Code:
#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;
}
The major problem segment of code:
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;
}