I'm creating a menu generator that takes in inputs from a .txt
file that has info, such as the type of dish ("appetizers", "entrees", etc), the price, and the name of the dish.
I made a class called Dish
and added some getters/setters I would use. I made a while
loop that takes in the info from the .txt
file. And I made a vector
that would store the price and name of the dish, depending on the type of dish it is.
I have what was stored in those vector
s be printed onto the output file. It does print out those dishes and prices, but they are all grouped up in one section (ie, all the dishes are under "Entrees"
). Here's some of my code below.
Part of my code in main()
that prints out the menu:
// Taking in the info
while (fin >> type >> price >> ws && getline(fin, name)) {
dishType = menu.get_dish_type();
menu.add_dish(type, price, name);
}
// Printing out the Menu
fout << "Menu\n\n";
fout << "Appetizers\n\n";
if (dishType == 0) {
for (const auto& dish : menu.appetizer) {
fout << dish.first << "($" << dish.second << ")" << endl;
}
}
fout << "\nEntrees\n\n";
if (dishType == 1) {
for (const auto& Dish : menu.entree) {
fout << Dish.first << "($" << Dish.second << ")" << endl;
}
}
fout << "\nDesserts\n";
if (dishType == 2) {
for (const auto& Dish : menu.dessert) {
fout << Dish.first << "($" << Dish.second << ")" << endl;
}
}
// Closing the Files
fin.close();
fout.close();
My implementation file:
#include "Dish.h"
Dish::Dish() : dish_type(0), dish_price(0), dish_name("") {}
// The Getters
unsigned int Dish::get_dish_type() const {return dish_type;}
unsigned int Dish::get_dish_price() const {return dish_price;}
string Dish::get_dish_name() const {return dish_name;}
// The Setters
void Dish::set_dish_type(string type) {
if (type == "appetizer") {dish_type = 0;}
if (type == "entree") {dish_type = 1;}
if (type == "dessert") {dish_type = 2;}
}
void Dish::set_dish_price(unsigned int new_price) {dish_price = new_price;}
void Dish::set_dish_name(string new_name) { dish_name = new_name; }
void Dish::add_dish(string type, unsigned int price, string name) {
if (dish_type == 0) {
appetizer.push_back(make_pair(name, price));
}
else if (dish_type == 1) {
entree.push_back(make_pair(name, price));
}
else if (dish_type == 2)
dessert.push_back(make_pair(name, price));
}
I used to have my vector
s be in the private
part of the class, but I moved them to the public
, which helped with an error I was getting in the printing part of main()
.
At first, nothing was printed, but I changed the if
conditions in the add_dish()
part of my header file, and it got the dishes to print. I'm thinking it's the way I get/use the "type of dish" but I can't think of a way to fix it.
What my code is printing out:
Menu
Appetizers
Pan-Seared Halibut with Sauteed Spinach($28)
Thai Green Curry Chicken Wings($9)
Char-Grilled Shrimp Skewers($10)
Angus Beef Burger with Aged Cheddar($10)
Gluten-Free Carrot Cake($5)
Linguine with White Clam Sauce($19)
12-oz Ribeye Steak with Cumin Pepper($23)
Chilled Cucumber Soup($4)
Jam and Peanut Butter Cookie($5)
Grilled Artichokes($10)
Entrees
Desserts
What my code is supposed to print out:
Menu
Appetizer
Thai Green Curry Chicken Wings ($9)
Char-Grilled Shrimp Skewers ($10)
Chilled Cucumber Soup ($4)
Grilled Artichokes ($10)
Entree
Pan-Seared Halibut with Sauteed Spinach ($28)
Angus Beef Burger with Aged Cheddar ($10)
Linguine with White Clam Sauce ($19)
12-oz Ribeye Steak with Cumin Pepper ($23)
Dessert
Gluten-Free Carrot Cake ($5)
Jam and Peanut Butter Cookie ($5)