I'm training myself via a programming problem about a taqueria (burrito) sale.
My issue is that the code is perfectly running but it does not print me the total profit for each burrito.
Here are 2 crucial steps that I had to include and follow in the problem :
STEP 1 : Write a function which takes a salesRecord as a parameter (by value), and returns the total profit from selling all the burritos of this type (unitProfit = retail - wholesale; totalProfit = unitProfit * numSold).
float getProfit(salesRecord record);
STEP 2 : Write another function which steps through your array of salesRecords and prints out the total profit for all burritos sold. You can do this by passing your function a pointer to the first element of your array and the number of elements in the array. The function can have a loop which will call getProfit() for each salesRecord.
void printReport(salesRecord* records, int size);
Did i missdo one of those step? How to fix it ?
Here is my code :
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
|
#include <iostream> // preprocessor directive
#include <string>
using namespace std;
// struct who store the wholesale and retail price of the burrito
struct cost
{
float wholesale;
float retail;
};
//struct who store the names and number of burritos
struct salesRecord
{
string burritoNames;
int burritoNumber;
cost pricing;
};
// step 1
float getProfit(salesRecord record)
{
float unitProfit = 0.00;
float totalProfit = 0.00;
unitProfit = record.pricing.retail - record.pricing.wholesale;
totalProfit = unitProfit * record.burritoNumber;
return totalProfit;
};
//step 2
void printReport(salesRecord *records, int size)
{
for (int i = 0; i < size; i++)
{
// cout << "index is " << i << "\n";
cout << "Total profit for " << records->burritoNames << " is :\n";
getProfit(*records);
}
};
int main()
{
int numType = 0;
// salesRecord taqueriaRecord[numType];
salesRecord *taqueriaRecord = new salesRecord[numType];
//ask user number of type of burrito
cout << "Hello Sir. How many type of Burrito sold today ? \n";
cin >> numType;
// get the name of each type and number sold
int totalBurrito = 0;
for (int i = 0; i < numType; i++)
{
cout << "Please enter the name of type " << i + 1 << " : \n";
cin >> taqueriaRecord[i].burritoNames;
// cout << "Burrito name is " << taqueriaRecord[i].burritoNames << "\n";
cout << "How many burrito sold for type " << i + 1 << " ? \n";
cin >> taqueriaRecord[i].burritoNumber;
// cout << "Burrito number is " << taqueriaRecord[i].burritoNumber << "\n";
cout << "What is the wholesale cost of " << taqueriaRecord[i].burritoNames << " ?\n";
cin >> taqueriaRecord[i].pricing.wholesale;
// cout << "Pricing choosed is " << taqueriaRecord[i].pricing.wholesale << "\n";
cout << "What is the retail cost of " << taqueriaRecord[i].burritoNames << " ?\n";
cin >> taqueriaRecord[i].pricing.retail;
// cout << "Pricing choosed is " << taqueriaRecord[i].pricing.wholesale << "\n";
cout << "Burrito : " << taqueriaRecord[i].burritoNames << " has sold " << taqueriaRecord[i].burritoNumber << " today.\n";
totalBurrito = totalBurrito + taqueriaRecord[i].burritoNumber;
}
cout << "Total of Burrito sold for today is : " << totalBurrito << "\n";
printReport(taqueriaRecord, numType);
delete[] taqueriaRecord;
return 0;
}
|
I wonder if i misdo the step 1 since via step 2 it does not print me what i what i want. I got in output :
Hello Sir. How many type of Burrito sold today ?
2
Please enter the name of type 1 :
Carnitas
How many burrito sold for type 1 ?
10
What is the wholesale cost of Carnitas ?
10
What is the retail cost of Carnitas ?
15
Burrito : Carnitas has sold 10 today.
Please enter the name of type 2 :
Beef
How many burrito sold for type 2 ?
5
What is the wholesale cost of Beef ?
5
What is the retail cost of Beef ?
20
Burrito : Beef has sold 5 today.
Total of Burrito sold for today is : 15
Total profit for Carnitas is : // step 2 does not print me the total profit
Total profit for Carnitas is : // and it does repeat the line a second.
|
I have been thinking if in step 1, i should do record[i], but the i will be undefined.