Hello everyone,
I am working on a programming assignment for a beginner C++ course I am taking.
Input:
name and preference of Vegetarian/Non-vegetarian
price for Veg meal = 20$
price for Non-veg= 30$
output :
List of names and their preference Veg/Non-Veg
calculate the total price for dinner.
So far I have been able to create a list of all guests and their choices. However, I can not figure out how to access the array variable for the choice to make the count and the calculation.
Thanks, everyone and stay safe!
Hi Furry Guy, the assignment does not require anything specific. Actually, it is supposed to be very basic with already declared variables for each name and choice and then just make the final calculation. I am just trying to make it better and more complicated out of curiosity. Thanks
Suggestion: You've hard coded the number of guests in several places in your program.
A better idea is to use a named constant in place of hard coded values.
constexprint NUM_GUESTS {8};
That way if the number of guests changes, you only have to change it in one place and don't have to worry about all the places you used a hard-coded value.
Calculating the total cost is a simple matter of iterating through the list and checking the contents of choice.
Hello AbstractionAnon ,
this is exactly what I couldn't figure out how to iterate through the list . Thank you very much for the solution. I am wondering though why when I used "strcmp" on an online compiler it didn't work and gave me the error: "use of undeclared identifier 'strcmp'. However, when I run the code on Visual Studio it works perfectly.
Well, better is a rather vague concept that means different things to different people. What I might consider better might not be better to others.
and more complicated out of curiosity.
Pushing what you know with new ideas is never a bad thing. Complicated just for the sake of complication shouldn't be a design goal. Knowing more of what C++ has to offer can actually make program design less verbose and complicated.
I originally asked what you could use, vectors and C++ strings. I've written some sloppy code that retains much of your original program ideas and translates them to less C and more C++.
#include <iostream>
#include <string> // C++ std::string
#include <vector> // C++ variable size container, std::vector
struct emp
{
std::string name { }; // name is no longer restricted to 20 characters
bool choice { }; // two menu choices, let's use a boolean. true or false.
};
int main()
{
// let's have a variable number of guests
std::cout << "Number of guests: ";
int num_guests;
std::cin >> num_guests;
// size a vector to the requested number of guests
std::vector<emp> party(num_guests);
// "walk" through your container, asking the name and meal choice.
for (unsigned i { }; i < party.size(); ++i)
{
std::cout << "\nGuest #" << i + 1 << " name: ";
// std::getline retrieves an entire line of input, including spaces.
// https://en.cppreference.com/w/cpp/string/basic_string/getline
std::getline(std::cin >> std::ws, party[i].name);
// with 2 meal choices simply ask if the choice is one or the other
std::cout << "\nIs the meal Vegetarian? ";
char meal_choice;
std::cin >> meal_choice;
// since characters can be upper or lower case check for either
if (meal_choice == 'Y' || meal_choice == 'y')
{
party[i].choice = true;
}
else
{
party[i].choice = false;
}
}
// time to present the bill....
int bill { };
for (unsigned i { }; i < party.size(); ++i)
{
if (party[i].choice) // true, veg
{
bill += 20;
}
else // false, non-veg
{
bill += 30;
}
}
std::cout << "\nYour bill is $" << bill << '\n';
}
Number of guests: 2
Guest #1 name: Joe Blow
Is the meal Vegetarian? y
Guest #2 name: George
Is the meal Vegetarian? n
Your bill is $50
you see this a lot .. extra wordy conditions. It does not hurt anything, and it probably even compiles the same way on smart optimizing compilers. Its just... wordy.
and...
if you counted the above into a variable vegcount you can get rid of a loop AND the condition block.
bill = party.size()*30 - vegcount*10;
this one you also see a lot, places where if you retained something you knew but discarded (here, the count of veggies) you could just spew the answer rather than have to burn cycles to reclaim the lost info. This one, the compiler can't fix.
which cuts it to about 30 total lines, most of it I/O
every person who sees a chunk of code can always find something to poke at. Don't expect to see all this your first pass... you go over stuff and over it again and again if you need perfection.
@jonnin, if'n I'd been writing the code from a set of written instructions like the OP I would have probably gone for something similar to what you wrote, something a lot less complicatedly verbose.
I debated over a number of coding choices. I think I threw enough new stuff out for a beginner. Maybe it helped the OP, maybe it didn't.
Good design even before writing a single line of code is a must-do.