C++ Class Menu Driven Program

Hi, I get my code to run but I am trying to get the cost and the total cost and it's not working. Please help me with the cost and total cost.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  //main.cpp file

#include"Fruit.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

//Function prototypes
void showMenu(Fruit MenuList[],int s);
int getData(Fruit MenuList[], Fruit ShoppingList[], int Max);
void ShopList(Fruit ShoppingList[], int counter);
void TotalCost(Fruit ShoppingList[], int counter);

int main()
{
    int counter = 0;
    const int SIZE = 8;
    const int Max = 100;
    Fruit ShoppingList[Max];
    Fruit MenuList[SIZE]= {Fruit("Apple", 0.99), Fruit("Apricot", 0.45), Fruit("Avocado", 1.50), Fruit("Banana", 0.75),
                Fruit("Blueberry", 1.00), Fruit("Grape", 1.25), Fruit("Orange", 1.50), Fruit("Pear      ", 1.30)};


    showMenu(MenuList,SIZE);
    counter = getData(MenuList, ShoppingList, Max);
    ShopList(ShoppingList, counter);
    TotalCost(MenuList, counter);

    return 0;
}


void showMenu(Fruit MenuList[], int s)                                  //Show menu function definition
    {
    cout << "Welcome to Cooper's Fresh Fruit Store! \n\n";
    cout << "   Items " << setw(25) << "Unit Price \n";
    for(int count = 0; count < s; count++)
    {
        cout << "#" << count+1 <<" "
             << MenuList[count].name << "\t\t$"
             << MenuList[count].price << "/lb\n";
    }
        cout <<"#9 Exit \n";

    }

int getData(Fruit MenuList[], Fruit ShoppingList[], int Max)                      //Get data function definition
	{
        int choice;
        double quan;
        int count = 0;

    do{
        cout << "\nPlease enter the number of your selection(1--9). \n";
        cin >> choice;
        if(choice ==9){break;}
        if(choice < 1 || choice > 9)
		{
        cout << "\a\a Invalid Input \n";
        cout << "\nPlease enter the number of your selection(1--9). \n";
        cin >> choice;
		}
        cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
        cin >> quan;
        if(quan < 0)
            {
            cout <<"\a\a Invalid quantity. \n";
            cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
            cin >> quan;
            }

		if(choice !=9 && quan >= 0)
            {
            ShoppingList[count].setN(MenuList[choice-1].getN()); //enters item name in order
            ShoppingList[count].SetP(MenuList[choice-1].getP()); //enter price in order
            ShoppingList[count].setQ(quan);                      //enter the quantity in order
            }
            count++;
    }while(choice !=9 && count < Max);                          //repeats the whole thing until 9 is entered

        return count;

}

void ShopList(Fruit ShoppingList[],int counter)                  //ShopList function def. shows shopping list
  {
    cout << "\nWelcome to Cooper's Restaurant!"<< "\n";
    cout << "This is your order list. \n\n";
    cout << "\tItems " << setw(20) << "Unit Price" << setw(15) << "Quantity "<< setw(13)<<"Cost \n";
    for(int count = 0; count < counter; count++)
        {
            cout << "#" << count+1 <<"\t"
                 << ShoppingList[count];
        }
    }

void TotalCost(Fruit ShoppingList[], int counter)                             //Total Cost function def.
{
    double FinalTotal;

    for(int count = 0; count < counter; count++)
    {
        FinalTotal+= ShoppingList[count].price;
    }
    cout << "****************************************************************** \n";
    cout << setw(25) << left << "Total Cost" <<fixed << showpoint <<setprecision(2)<<"$"<<FinalTotal << "\n";
}


//Fruit.cpp file-- Function definitions

#include "Fruit.h"
#include<iostream>
using namespace std;

void Fruit::setAll(string n, double p, double q)
{
    name = n;
    price = p;
    quantity = q;
}

void Fruit::setN(string n)
{
    name = n;
}
void Fruit::SetP(double p)
{
    price = p;
}
void Fruit::setQ(double q)
{
    quantity = q;
}

Fruit::Fruit(string n, double p, double q)                   //Constructor with default parameters
{
    setAll(n, p, q);
}

ostream& operator<<(ostream & os , const Fruit  & ob)           //Overloading (<<) definition
{
    os <<ob.name<<"\t\t$"
       <<ob.price<<"\t\t "
       <<ob.quantity<<" lb\t\t "
       << ob.price * ob.quantity <<"\n";
    return os;
}


//Fruit.h file

#ifndef FRUIT_H
#define FRUIT_H
#include<iostream>
#include<string>
using namespace std;

class Fruit                                         //Class Declaration
{
public:
    string name;                                     //Class variables
    double price;
    double quantity;

    void setAll(string n, double p, double q);          //Class functions
    void setN(string n);
    void SetP(double p);
    void setQ(double q);

    string getN(){return name;}
    double getP(){return price;}
    double getQ(){return quantity;}
    double getC(){int cost; cost = price * quantity; return cost;}

    Fruit(string = " ", double = 0.0, double = 0.0);        //Constructor with default parameters
    friend ostream& operator<<(ostream & os ,const Fruit  & ob);
};

#endif // FRUIT_H
Look at this snippet:
1
2
    ShopList(ShoppingList, counter);
    TotalCost(MenuList, counter);

Shouldn't TotalCost() be using the ShoppingList, not the MenuList?

Also in TotalCost() you need to insure all of your variables are initialized before use, and double check your formulas.

Thanks, that helped a little.
Now I am doing
"FinalTotal+= ShoppingList[count].price * ShoppingList[count].quantity;"
and it works.

Is there a way I can use the cost function in the header file to get the total cost instead of using price * quantity?
Last edited on
Please post your current code.

Now I am doing
"FinalTotal+= ShoppingList[count].price * ShoppingList[count].quantity;"
and it works.

Is there a way I can use the cost function in the header file to get the total cost instead of using price * quantity?

//Assignment I- Fresh Fruit Store. Nathaniel Cooper; February 2, 2016
//main.cpp file

#include"Fruit.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

//Function prototypes
void showMenu(Fruit MenuList[],int s);
int getData(Fruit MenuList[], Fruit ShoppingList[], int Max);
void ShopList(Fruit ShoppingList[], int counter);
void TotalCost(Fruit ShoppingList[], int counter);

int main()
{
int counter = 0;
const int SIZE = 8;
const int Max = 100;
Fruit ShoppingList[Max];
Fruit MenuList[SIZE]= {Fruit("Apple", 0.99), Fruit("Apricot", 0.45), Fruit("Avocado", 1.50), Fruit("Banana", 0.75),
Fruit("Blueberry", 1.00), Fruit("Grape", 1.25), Fruit("Orange", 1.50), Fruit("Pear ", 1.30)};


showMenu(MenuList,SIZE);
counter = getData(MenuList, ShoppingList, Max);
ShopList(ShoppingList, counter);
TotalCost(ShoppingList, counter);

return 0;
}


void showMenu(Fruit MenuList[], int s) //Show menu function definition
{
cout << "Welcome to Cooper's Fresh Fruit Store! \n\n";
cout << " Items " << setw(25) << "Unit Price \n";
for(int count = 0; count < s; count++)
{
cout << "#" << count+1 <<" "
<< MenuList[count].name << "\t\t$"
<< MenuList[count].price << "/lb\n";
}
cout <<"#9 Exit \n";

}

int getData(Fruit MenuList[], Fruit ShoppingList[], int Max) //Get data function definition
{
int choice;
double quan;
int count = 0;

do{
cout << "\nPlease enter the number of your selection(1--9). \n";
cin >> choice;
if(choice ==9){break;}
if(choice < 1 || choice > 9)
{
cout << "\a\a Invalid Input \n";
cout << "\nPlease enter the number of your selection(1--9). \n";
cin >> choice;
}
cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
cin >> quan;
if(quan < 0)
{
cout <<"\a\a Invalid quantity. \n";
cout << "\nPlease enter the quantity of " << MenuList[choice-1].getN() <<":\n";
cin >> quan;
}

if(choice !=9 && quan >= 0)
{
ShoppingList[count].setN(MenuList[choice-1].getN()); //enters item name in order
ShoppingList[count].SetP(MenuList[choice-1].getP()); //enter price in order
ShoppingList[count].setQ(quan); //enter the quantity in order
}
count++;
}while(choice !=9 && count < Max); //repeats the whole thing until 9 is entered

return count;

}

void ShopList(Fruit ShoppingList[],int counter) //ShopList function def. shows shopping list
{
cout << "\nWelcome to Cooper's Restaurant!"<< "\n";
cout << "This is your order list. \n\n";
cout << "\tItems " << setw(20) << "Unit Price" << setw(15) << "Quantity "<< setw(13)<<"Cost \n";
for(int count = 0; count < counter; count++)
{
cout << "#" << count+1 <<"\t"
<< ShoppingList[count];
}
}

void TotalCost(Fruit ShoppingList[], int counter) //Total Cost function def.
{
double FinalTotal;

for(int count = 0; count < counter; count++)
{
FinalTotal+= ShoppingList[count].price * ShoppingList[count].quantity;
}
cout << "****************************************************************** \n";
cout << setw(25) << left << "Total Cost" <<fixed << showpoint <<setprecision(2)<<"$"<<FinalTotal << "\n";
}

//Assignment I- Fresh Fruit Store. Nathaniel Cooper; February 2, 2016
//Fruit.cpp file-- Function definitions

#include "Fruit.h"
#include<iostream>
using namespace std;

void Fruit::setAll(string n, double p, double q)
{
name = n;
price = p;
quantity = q;
}

void Fruit::setN(string n)
{
name = n;
}
void Fruit::SetP(double p)
{
price = p;
}
void Fruit::setQ(double q)
{
quantity = q;
}

Fruit::Fruit(string n, double p, double q) //Constructor with default parameters
{
setAll(n, p, q);
}

ostream& operator<<(ostream & os , const Fruit & ob) //Overloading (<<) definition
{
os <<ob.name<<"\t\t$"
<<ob.price<<"\t\t "
<<ob.quantity<<" lb\t\t "
<< ob.price * ob.quantity <<"\n";
return os;
}

//Assignment I- Fresh Fruit Store. Nathaniel Cooper; February 2, 2016
//Fruit.h file

#ifndef FRUIT_H
#define FRUIT_H
#include<iostream>
#include<string>
using namespace std;

class Fruit //Class Declaration
{
public:
string name; //Class variables
double price;
double quantity;

void setAll(string n, double p, double q); //Class functions
void setN(string n);
void SetP(double p);
void setQ(double q);

string getN(){return name;}
double getP(){return price;}
double getQ(){return quantity;}
double getC(){int cost; cost = price * quantity; return cost;}

Fruit(string = " ", double = 0.0, double = 0.0); //Constructor with default parameters
friend ostream& operator<<(ostream & os ,const Fruit & ob);
};

#endif // FRUIT_H


Please edit your post and place the cod inside code tags.
Topic archived. No new replies allowed.