I am trying to use stringstream or sstream 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, hence the stringstream. However, after I edited the if statement in the main, the code had stopped working and I am not sure why. Can someone please help?
Note: I am not sure if this is the complete problem. Please tell me if I should modify my question title if needed.
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>
#include <sstream>
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};
stringstream ss;
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 (menuItems.begin()+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();
ss << item;
string info = ss.str();
if (info != "c" && info != "a" && info != "r") {
enterSelection();
} else if (info == "c") {
checkout();
} else if (info == "a") {
add();
} else if (info == "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: ss << item; string info = ss.str();
if (info != "c" && info != "a" && info != "r") {
enterSelection();
} else if (info == "c") {
checkout();
} else if (info == "a") {
add();
} else if (info == "r") {
remove();
}
else {
cout << "Please enter a number or press c to checkout, a to add item, or r to remove item: " << flush;
}
Updated code from suggestions:
#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);
}
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 != 'c' || item != 'a' || item != 'r') {
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;
}
Aucun commentaire:
Enregistrer un commentaire