Structures

closed account (zAf1qMoL)
So, my display suppose to look like this... And I don't know what I'm doing.

Enter pizza size 1: Junior
Enter Number of Junior dough used today: 4

Enter pizza size 2: Regular
Enter Number of Regular dough used today: 7

Enter pizza size 3: Large
Enter Number of Large dough used today: 17
Please enter a number less than or equal to 15: 15.

Enter pizza size 4: Party
Enter Number of Party dough used today: 9

Junior:
4 Junior pizzas were sold.
$6.00 was made on Junior pizzas today.
Regular:
7 Regular pizzas were sold.
$21.00 was made on Regular pizzas today.
Large:
15 Large pizzas were sold.
$75.00 was made on Large pizzas today.
Party:
9 Party pizzas were sold.
$90.00 was made on Party pizzas today.

The total profit for the day is: $192.00

This is what I have right now.
|
v

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// Declare the constants
const double JUNIOR_PIZZA_COST = 1.50;
const double REGULAR_PIZZA_COST = 3.00;
const double LARGE_PIZZA_COST = 5.00;
const double PARTY_PIZZA_COST = 10.00;
const int FOUR = 4;
const int FIFTEEN = 15;

// Prototype for function
void pizzasize();

int main()
{

    // Prototype will be displayed
    pizzasize();

    // End of program.
    system("pause");
    return 0;

}

void pizzasize()
{

    // Declare the variables
    const string PIZZA_SIZES[] = { "Junior", "Regular", "Large", "Party" };
    const int DOUGH_MAX[] = { 15,15,15,15 };
    string size;
    int numSold = 0;
    double nightsProfit = 0;

    for (int i = 0; i < FOUR; i++)
    {
        do
        {
            cout << "Enter pizza size " << (i + 1) << ": ";
            cin >> size;
            cout << "Enter Number of Junior dough used today: ";
            cin >> numSold;
            cout << endl;

            if ((numSold + i) < 0)
            {
                cout << "No negative number can be entered. Try again." << endl;
            }
            else if ((numSold + i) > FIFTEEN)
            {
                cout << "Please enter a number less than or equal to 15." << endl;
            }
        } while ((numSold + i) < 0 || (numSold + i) > FIFTEEN);
    }

    for (int i = 0; i < FOUR; i++)
    {
        cout << "Pizza name/size: " << PIZZA_SIZES << endl;
        cout << "Number sold: " << numSold << endl;
        cout << "Total profit: $" << nightsProfit << endl;


        nightsProfit += (numSold + i);

        cout << "Total profit for the day is: $" << nightsProfit << endl;

        // Goodbye message.
        cout << "Thank you for your business!";
    }
}


Thank you in advance.
Last edited on
1
2
const int FOUR = 4;
const int FIFTEEN = 15;
I think.. you misunderstand the "don't have magic numbers in your code" guideline. xD
The name of the number shouldn't be what the number is, because that adds 0 value. The name of the constant should be what the 15 actually represents, which is the "number of dough".

You didn't actually explain what's wrong with your code. It looks like a good start.
That first issue is that on line 46, you're hardcoding the word "Junior". You should print whatever size the user entered.
Second, when the user enters a number > 15, you loop back to the top of your loop, making them re-enter the size as well.
Don't do that. Just have the "Enter number of ..." logic be within the loop.

cout << "Pizza name/size: " << PIZZA_SIZES << endl;
You can't just print an array like this, you have to loop through each element.
e.g.
1
2
3
4
for (int j = 0; j < 4; j++)
{
    cout << PIZZA_SIZES[j] << '\n'
}


At the end when you print everything, you need to remember the number of sold PER type.
So you need some array like,
int numSoldPerType[4] { 0, 0, 0, 0 }
And increment numSoldPerType[i] when an order comes in.
Last edited on
closed account (zAf1qMoL)
I redone the coding. How can I do this without typedef struct? And I need to add no negative numbers and maximum number you can type is less than or equal to 15.

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
#include <iostream>
#include <iomanip>

using namespace std;

const double JUNIOR_PIZZA_COST = 1.50;
const double REGULAR_PIZZA_COST = 3.00;
const double LARGE_PIZZA_COST = 5.00;
const double PARTY_PIZZA_COST = 10.00;

typedef struct pizza
{
    string size;
    int numSold;
    double profit;
}pizza;

int main()
{

    pizza p1 = { "Junior", 0, 0 };
    pizza p2 = { "Regular", 0, 0 };
    pizza p3 = {"Large", 0, 0};
    pizza p4 = { "Party", 0, 0 };

    pizza pizzas[] = { p1, p2, p3, p4 };

    string size;
    int numSold = 0;

    for (int i = 0; i < 4; i++)
    {
        cout << "\nEnter pizza size " << (i + 1) << ": ";
        cin >> size;
        cout << "Enter Number of " << size << " dough used today: ";
        cin >> numSold;

        if (pizzas[0].size.compare(size) == 0)
        {
            pizzas[0].numSold += numSold;
            pizzas[0].profit = pizzas[0].numSold * JUNIOR_PIZZA_COST;
        }
        else if (pizzas[1].size.compare(size) == 0)
        {
            pizzas[1].numSold += numSold;
            pizzas[1].profit = pizzas[1].numSold * REGULAR_PIZZA_COST;
        }
        else if (pizzas[2].size.compare(size) == 0)
        {
            pizzas[2].numSold += numSold;
            pizzas[2].profit = pizzas[2].numSold * LARGE_PIZZA_COST;
        }
        else if (pizzas[3].size.compare(size) == 0)
        {
            pizzas[3].numSold += numSold;
            pizzas[3].profit = pizzas[3].numSold * PARTY_PIZZA_COST;
        }
    }
    
    double dayProfit = 0;

    cout << setprecision(2) << fixed << showpoint;

    cout << endl;
    cout << "\t\tEND OF DAY REPORT" << endl;

    for (int i = 0; i < 4; i++)
    {
        cout << endl;
        cout << pizzas[i].size << ":" << endl;
        cout << pizzas[i].numSold << " " << pizzas[i].size << " pizzas were sold." << endl;
        cout << "$" << pizzas[i].profit << " was made on " << pizzas[i].size << " pizzas today." << endl;  
        dayProfit += pizzas[i].profit;
    }

    cout << "\nThe total profit for the day is: $" << dayProfit << endl;
    cout << "\nThank you for your business!" << endl;

    system("pause");
    return 0;
}

Last edited on
how can I do this without typedef struct?

C++ only has a single name space. It makes no* distinction between tag names and type names:

1
2
3
4
5
6
struct pizza
{
    string size;
    int numSold;
    double profit;
};

(*C++ only distinguishes the two in a special case for the purposes of C compatibility.)
Last edited on
fun fun

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
#include <iostream>
#include <iomanip>

using namespace std;

const double JUNIOR_PIZZA_COST = 1.50;
const double REGULAR_PIZZA_COST = 3.00;
const double LARGE_PIZZA_COST = 5.00;
const double PARTY_PIZZA_COST = 10.00;

struct pizza
{
    string size;
    int numSold;
    double profit; 
    double cost;
};

int main()
{

    pizza p1 = { "Junior", 0, 0, JUNIOR_PIZZA_COST};
    pizza p2 = { "Regular", 0, 0,REGULAR_PIZZA_COST};
    pizza p3 = { "Large", 0, 0,LARGE_PIZZA_COST };
    pizza p4 = { "Party", 0, 0,PARTY_PIZZA_COST };

    pizza pizzas[] = { p1, p2, p3, p4 };

    string size;
    int numSold = 0;

   
    for (int y = 0; y < 4; y++) {
        while (1) {
                cout << "\nEnter pizza size " << (y + 1) << ": ";
                cin >> size;

            for (int i = 0; i < 4; i++) {
                
                if (pizzas[i].size.compare(size) == 0) {
                    goto Label1;
                }
                else if (i == 3) {
                    cout << "Invalid Pizza Size Entered Try Again" << "\n";
                }
            }
        }
Label1:
        while (1) {
            cout << "Enter Number of " << size << " dough used today: ";
            cin >> numSold;
            if (numSold < 0 || numSold>15) {
                cout << "Invalid Number Entered Try Again" << "\n";
            }
            else {
                break;
            }
                
        }
            
        for (int i = 0; i < 4; i++) {
            if (pizzas[i].size.compare(size) == 0)
            {
                pizzas[i].numSold = numSold;
                pizzas[i].profit = pizzas[i].numSold * pizzas[i].cost;
                break;
            }
        }
    }

    double dayProfit = 0;

    cout << setprecision(2) << fixed << showpoint;

    cout << endl;
    cout << "\t\tEND OF DAY REPORT" << endl;

    for (int i = 0; i < 4; i++)
    {
        cout << endl;
        cout << pizzas[i].size << ":" << endl;
        cout << pizzas[i].numSold << " " << pizzas[i].size << " pizzas were sold." << endl;
        cout << "$" << pizzas[i].profit << " was made on " << pizzas[i].size << " pizzas today." << endl;
        dayProfit += pizzas[i].profit;
    }

    cout << "\nThe total profit for the day is: $" << dayProfit << endl;
    cout << "\nThank you for your business!" << endl;

    system("pause");
    return 0;
}
Hello ericmcha,

Just so you have a different idea to consider:

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
#include <cctype>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

constexpr int MAXSIZE{ 4 };
constexpr double PIZZA_COST[MAXSIZE]{ 1.5, 3.0, 5.0, 10.0 };

struct Pizza
{
	string size;
	int numSold;
	double profit;
};

int main()
{
	Pizza pizzas[MAXSIZE]
	{
		{"Junior", 0, 0},
		{"Regular", 0, 0},
		{"Large", 0, 0},
		{"Party", 0, 0}
	};

	std::string pizzaTypes[MAXSIZE]{ "Junior","Regular","Large","Party" };
	int numSold = 0;
	double dayProfit = 0;

	for (int i = 0; i < MAXSIZE; i++)
	{
		cout << "\n Enter Number of " << pizzaTypes[i] << " dough used today: ";
		cin >> numSold;

		pizzas[i].numSold = numSold;
		pizzas[i].profit = numSold * PIZZA_COST[i];

		dayProfit += pizzas[i].profit;
	}


	cout << setprecision(2) << fixed/* << showpoint*/;

	cout << "\n\n" << std::string(10, ' ') << "END OF DAY REPORT\n";

	for (int i = 0; i < MAXSIZE; i++)
	{
		cout << '\n';
		cout << pizzas[i].size << ":\n";  // <--- You could also use "pizzaType[i]".
		cout << pizzas[i].numSold << " " << pizzas[i].size << " pizzas were sold.\n";
		cout << "$" << pizzas[i].profit << " was made on " << pizzas[i].size << " pizzas today.\n";
	}

	cout << "\nThe total profit for the day is: $" << dayProfit << '\n';
	cout << "\n\nThank you for your business!" << "\n\n" << endl;

	// A fair C++ replacement for "system("pause")". Or a way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n\n\n Press Enter to continue: ";  // <--- Normally use 2 "\n"s. Added the other 2 to change the appearance in my window. Adjust the "\n"s as needed
	std::cin.get();

	return 0;
}

Much shorter and makes better use of what you have.

Andy
Topic archived. No new replies allowed.