Question

I am struggling with a program in C++. it involves using a struct and a class...

I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors.

Instructions

A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per serving. You must write a program that helps users choose which meal they will order based on calorie content.

Each item in a meal should be represented by a structure called MealItem that contains the following information:

The name of the item (a string).


The amount of calories per gram (a double).


The serving size in grams (a double).


Each meal should be represented by an instance of a class called Meal containing the following members:

Two private member variables:


A dynamically allocated array of MealItem structures.


An integer representing the number of meal items.


Two private member functions:


void allocate() dynamically allocates the array of MealItem structures using the integer private member variable to determine the size of the array.


void deallocate() deallocates the array of MealItem structures.


A default constructor that does the following:


Repeatedly prompts the user for the number of meal items until they enter a value between 1 and 10.


Initializes the number of meal items.


Calls allocate to allocate the array.


A one-argument constructor does the following:


Initializes the number of meal items to the argument provided.


Calls allocate to allocate the array.


A destructor that calls deallocate to deallocate the array.


Five public member functions:


int getNumItems() const returns the number of items in the meal. This should be implemented as an inline member function.


void readMeal() prompts the user for the name, grams per serving, and calories per gram for each MealItem in the array.


void displayMeal() displays names, grams per serving, and calories per gram for each MealItem in the array, aligned in columns with column headers.


double getTotalCalories() const calculates and returns the total number of calories in the meal by multiplying the grams per serving and calories per gram of each MealItem and adding the results together.


int getNumItemsCaloriesAbove(double calories) const calculates and returns the number of items in the meal whose calories per gram are higher than calories.


You must also implement the following functions in another file that contains the main function:

void displayMenu() displays a menu with five options:


Enter the meals.


Display the highest calorie meal.


Display the number of high calorie items in a meal.


Display the meals.


Exit the program.


Meal *readMeals(int &numMeals)


Repeatedly prompt the user to enter the number of meals until they enter a number between 1 and 5.


Store this value in numMeals.


Use this value to dynamically allocate an array of Meal objects.


This will result in the Meal constructor being called once for each item in the array, prompting for the number of items in each meal and allocating the MealItem arrays.


Call the readMeal member function on each meal in the array to prompt the user for the information about each meal item.


Return the array of Meal objects.


void displayMealInfo(Meal *meals, int numMeals) calls the displayMeal member function on each Meal object in the meals array.

void displayHighestCalorieMeal(Meal *meals, int numMeals)


Calls the getTotalCalories function on each Meal in meals and displays the results for each meal.


Displays the meal with the most calories.


void displayHighCalorieItemCount(Meal *meals, int numMeals)


Repeatedly prompts the user for the meal number until they enter a value between 1 and numMeals.


Prompts the user for a calorie value.


Calls getNumItemsCaloriesAbove to get the number of meal items in the selected meal that have more calories per gram than the provided calorie value and displays the results.


Your main function should display a welcome message, then repeatedly do the following until the user chooses to exit the program:

Call the displayMenu function and prompt for the user to enter their choice.


If the user chooses option 1, call readMeals to initialize an array of Meal objects and a variable representing the number of meals.


If the user chooses option 2, call displayHighestCalorieMeal.


If the user chooses option 3, call displayHighCalorieItemCount.


If the user chooses option 4, call displayMealInfo.


If the user chooses option 5, display a message then exit the program.


If the user chooses any other value display an error message and go back to displaying the menu and prompting the user for a choice.


Make sure to deallocate your array of Meal objects.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to above problem in c++. Please read the code comments for more information.

MEAL.CPP CONTAINS THE CLASS MEAL FILE AND DRIVER.CPP CONTAIN THE MAIN FUNCTION WITH THE MENU

PLEASE COPY CODE OF BOTH FILES IN DIFFERENT FILES BUT IN A SAME DIRECTORY/FOLDER

C++ CODE

MEAL.CPP

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;


//meal item structure
struct MealItem
{
   public:
       string item;
       int cal;
       int serving;
      
};

class Meal
{
   private:
       MealItem * meal;
       int items;
  
       //to allocate memory
       void allocate()
       {
           meal = new MealItem[items];
       }
       //to deallocate memory
       void deallocate()
       {
          
               delete(meal);
          
       }
       public:
           //default constructor
           Meal()
           {
               int temp=-1;
               //taking input till user enters value between 1-10
               while(temp<1||temp>10)
               {
               cout<<"Enter the number of meal Items (1-10): ";
               cin>>temp;
               }
  
               items=temp;
               allocate();
          
           }
          
           //parameterized constructor
           Meal(int I)
           {
               items=I;
      
               allocate();
           }
           //destructor
           ~Meal()
           {
               deallocate();
           }
           //get number of items
           int getNumItems()
           {
               return items;
           }
           //reading a meal from user
           void readMeal()
           {
               for(int i=0;i<items;++i)
               {
               cout<<"Item No: "<<i+1<<endl;
               cout<<"Enter Item name: ";
               cin>>meal[i].item;
               cout<<"Enter Grams per serving: ";
               cin>>meal[i].serving;
               cout<<"Enter calories per gram: ";
               cin>>meal[i].cal;
               }
           }
          
           //display meal
           void displayMeal()
           {
               cout<<setw(10)<<"SNo."<<setw(10)<<"Name"<<setw(10)<<"Grams "<<setw(10)<<"Calorie"<<endl;
              
               for(int i=0;i<items;++i)
               {
                   cout<<setw(10)<<i+1<<setw(10)<<meal[i].item<<setw(10)<<meal[i].serving<<setw(10)<<meal[i].cal<<endl;
               }
           }
          
           double getTotalCalories()
           {
               double total=0;
               for(int i=0;i<items;++i)
               {
                   total+= (meal[i].serving*meal[i].cal);
               }
               return total;
           }
           int getNumItemsCaloriesAbove(double calories)
           {
               int num=0;
               for(int i=0;i<items;++i)
               {
                   if(meal[i].cal>calories)
                       num++;
               }
               return num;
           }
          
      
};

DRIVER.CPP (THIS FILE CONTAINS THE MAIN)

#include<iostream>
#include"meal.cpp"

using namespace std;

Meal *readMeals(int &numMeals)
{
   Meal *meal = new Meal[numMeals];
   for(int i=0;i<numMeals;++i)
   {
   cout<<"Enter data for meal no: "<<i+1<<endl;
   meal[i].readMeal();
   cout<<endl;
   }
   return meal;
}

void displayMealInfo(Meal *meal,int numMeals)
{
  
   for(int i=0;i<numMeals;++i)
   {
       cout<<"-----------------MEAL ("<<i+1<<")------------------\n";
       meal[i].displayMeal();
       cout<<endl;
   }
  
}
void displayHighestCalorieMeal(Meal *meal,int numMeals)
{
  
   int loc=0;
   int cal=-999;
   for(int i=0;i<numMeals;++i)
   {
       if(meal[i].getTotalCalories()>cal)
       {
           loc=i;
           cal=meal[i].getTotalCalories();
       }
       cout<<endl;
       cout<<"TOTAL CALORIE IN MEAL "<<i+1<<" IS: "<<meal[i].getTotalCalories()<<endl;
   }
   cout<<"MEAL WITH HIGHEST CALORIE IS MEAL NO: "<<loc+1<<endl;
   meal[loc].displayMeal();
   cout<<endl;
}

void displayHighCalorieItemCount(Meal *meal,int numMeals)
{
   int temp=-1;
   while(temp<1||temp>numMeals)
   {
       cout<<"PLEASE ENTER A MEAL NUMBER: ";
       cin>>temp;
   }
   int cal;
   cout<<"PLEASE ENTER A CALORIE VALUE: ";
   cin>>cal;
   cout<<"THE NUMBER OF ITEMS WITH CALORIE COUNT MORE THAN "<<cal<<" ARE: "<<meal[temp-1].getNumItemsCaloriesAbove(cal)<<endl;
}
int main()
{
   int option=0;
   Meal *meal;
   int numMeals=0;
   while(option!=5)
   {
       cout<<"-----------MENU-------------\n";
       cout<<"1)Enter the meals\n";
       cout<<"2)Display the highest calorie meal\n";
       cout<<"3)Display the number of high calorie items in the meal\n";
       cout<<"4)Display the meals\n";
       cout<<"5)Exit the program\n";
       cout<<"Enter your choice: ";
       cin>>option;
       switch(option)
       {
           case 1:
           {
               int temp=-1;
               while(temp<1||temp>5)
               {
                   cout<<"Enter the number of meals\n";
                   cin>>temp;
               }
               numMeals=temp;
               meal=readMeals(numMeals);  
           }
           break;
           case 2:
               {
                   displayHighestCalorieMeal(meal,numMeals);  
               }
               break;
           case 3:
               {
                   displayHighCalorieItemCount(meal,numMeals);
               }
               break;
           case 4:
               {
                   displayMealInfo(meal,numMeals);
               }  
               break;
           case 5:
               {
                   cout<<"Exiting the program\n";  
               }
               break;
           default: cout<<"Enter a Valid Choice\n";
          
       }
   }
   return 0;
}

SCREENSHOT OF OUTPUT

Add a comment
Know the answer?
Add Answer to:
I am struggling with a program in C++. it involves using a struct and a class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • write it in c++ Question 6 Consider a case of single inheritance where Landline phone is a base class and Mobile phone is the derived class. Both the classes are as follow: (a) Landline: It has su...

    write it in c++ Question 6 Consider a case of single inheritance where Landline phone is a base class and Mobile phone is the derived class. Both the classes are as follow: (a) Landline: It has subscriber name and number as data members. The member functions are to provide the features of calling on a subscriber's number and receiving a call Void call (int sub_number) Void receivel (b) Mobile: Apart from inheriting the features of a Landline phone, it provides...

  • /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use...

    /* * Purpose: Test an implementation of an array-based list ADT. * To demonstrate the use of "ListArrayBased" the project driver * class will populate the list, then invoke various operations * of "ListArrayBased". */ import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based...

  • Write a java program that uses a loop to input, from the user not using gui,...

    Write a java program that uses a loop to input, from the user not using gui, a collection of values that are the number of steps taken each day for a sequence of days and stores those amounts in an array. the array length should be 31, The user is not required to enter 31 values. the program determines and displays the largest number of steps for a single day, the smallest number of steps for a single day and...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • PROGRAMMING LANGUAGE OOP'S WITH C++ Functional Requirements: A jewelry designer friend of mine requires a program...

    PROGRAMMING LANGUAGE OOP'S WITH C++ Functional Requirements: A jewelry designer friend of mine requires a program to hold information about the gemstones he has in his safe. Offer the jewelry designer the following menu that loops until he chooses option 4. 1. Input a gemstone 2. Search for a gemstone by ID number 3. Display all gemstone information with total value 4. Exit ------------------------------------- Gemstone data: ID number (int > 0, must be unique) Gem Name (string, length < 15)...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT