Program to create a count based on users input

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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
using namespace std;

struct emp{
  char name[20];
  char choice[20];
}obj[8];

int main(){
  int i;
  for(i=0;i<8;i++){
    cout<<"Enter guest "<<i+1<< " Enter choice vegeterian/non-vegeterian:  \n";
    cin>>obj[i].name>>obj[i].choice;
    

  }
  cout<<"Table of Guests: \nNAME\tCHOICE\t\n";
  for(i=0;i<8;i++){
    cout<<obj[i].name<<"\t"<<obj[i].choice<<"\t\t"<<"\n";  
  }       
  return 0;
}
Are you required to use C string arrays as well as an array to hold the names and choices?

Can you use std::string and a C++ container such as a std::vector?

BTW, the idea of containing the multiple bits of data in a struct IS a good idea. :)
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.
 
  constexpr int 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.
1
2
3
4
5
6
7
8
9
10
    constexpr double  COST_VEG {20};
    constexpr double COST_NON_VEG {30};
    double total_cost {0};

    for (i=0; i<NUM_GUESTS; i++)
      if (strcmp(obj[i].choice, "Veg") == 0)
          total_cost += COST_VEG;
      else
         total_cost += COST_NON_VEG;
    cout << "Total cost is: " << total_cost << endl;


Edit:
Should have the following #include:
 
#include <cstring> 
Last edited on
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.
strcmp is part of the <cstring> library. I didn't mention the #include for that.
Did you have that #include when you tried the online compiler?


No , I didn't, but I also didn't include it when I ran the code on Visual Studio and it worked.
I am just trying to make it better

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++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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


that whole if/else block is:
 
party[i].choice  =  (meal_choice == 'Y' || meal_choice == 'y');

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.
Last edited on
@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.
And you did it well. I hope what I said helps as well, building off it... its easy to steal someone else's work and add a tiny bit too it :)
Topic archived. No new replies allowed.