program not working if statements

My first statement works fine, but the second gathering type does not, and I can't figure out what is wrong

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
  #include <iostream>
#include <string>
using namespace std;

int main()
{
	int numberOfAnticipatedGuests, numberOfPanGriddles, numberOfBaconGriddles,
		numberOfCheeseGriddles, potatoChipBoxes, numberOfPickleJars, gatheringType, realPancake, realBacon;

	double lengthOfEventInHours, avgNumOfGuestsPerHr;

	char  picklesNeeded, chipsNeeded, baconNeeded;

	const char	YES_TO_PICKLES = 'Y', NO_TO_PICKLES = 'N', YES_TO_BACON = 'Y', NO_TO_BACON = 'N', YES_TO_CHIPS = 'N', NO_TO_CHIPS = 'N';

	const int pancakesPerHour = 240, baconPerHour = 550, grilledCheesePerHour = 200, picklesPerJar = 22, servingsPerBox = 25;

	const string ERROR = "ERROR";




	cout << "What kind of event do you want to plan?";
	cout << "\n1. Pancake Breakfast";
	cout << "\n2. Grilled Cheese Luncheon";
	cout << "\n Please please 1 or 2 ";
	cin >> gatheringType;

	if (gatheringType == 1)
	{

		cout << "\nplease input your anticipated number of guests: ";
		cin >> numberOfAnticipatedGuests;
		cout << "\nPlease Input your event in Hours";
		cin >> lengthOfEventInHours;
		cout << "\nwould you like bacon?";
		cin >> baconNeeded;
		baconNeeded = toupper(baconNeeded);


		if (baconNeeded == YES_TO_BACON)
		{
			realPancake = numberOfAnticipatedGuests * 3;
			realBacon = numberOfAnticipatedGuests * 3;
			numberOfPanGriddles = realPancake / pancakesPerHour;
			numberOfBaconGriddles = realBacon / baconPerHour;
			avgNumOfGuestsPerHr = numberOfAnticipatedGuests / lengthOfEventInHours;
			cout << "Bacon Chosen";
		}
			else if (baconNeeded == NO_TO_BACON)
			{
				realPancake = numberOfAnticipatedGuests * 3;
				realBacon = numberOfAnticipatedGuests * 3;
				numberOfPanGriddles = realPancake / pancakesPerHour;
				numberOfBaconGriddles = 0;
				avgNumOfGuestsPerHr = numberOfAnticipatedGuests / lengthOfEventInHours;

			}

		cout << "\nyour number of anticipated guests is: ";
		cout << numberOfAnticipatedGuests;
		cout << "\nthe length of your event is: ";
		cout << lengthOfEventInHours;
		cout << "\nthe average number of guests per hour is";
		cout << avgNumOfGuestsPerHr;
		cout << "\nthe number of pancake griddles";
		cout << numberOfPanGriddles;
		cout << "\nthe number of bacon griddles\n";
		cout << numberOfBaconGriddles;

	}
	else if (gatheringType == 2)
		{


			cout << "\nplease input your anticipated number of guests: ";
			cin >> numberOfAnticipatedGuests;
			cout << "\nPlease Input your event in Hours";
			cin >> lengthOfEventInHours;
			cout << "\nwould you like pickles?";
			cin >> picklesNeeded;
			picklesNeeded = toupper(picklesNeeded);
			cout << "\nwould you like chips? ";
			cin >> chipsNeeded;
			chipsNeeded = toupper(chipsNeeded);




			if (picklesNeeded == YES_TO_PICKLES && chipsNeeded == NO_TO_CHIPS)
			{

				
				numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour;
				numberOfPickleJars = numberOfAnticipatedGuests / picklesPerJar;
				potatoChipBoxes = 0;

			}
				else if (picklesNeeded == NO_TO_PICKLES && chipsNeeded == YES_TO_CHIPS)
				{

					
					numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour;
					numberOfPickleJars = 0;
					potatoChipBoxes = numberOfAnticipatedGuests / servingsPerBox;

				}
					else if (picklesNeeded == YES_TO_PICKLES && chipsNeeded == YES_TO_CHIPS)
					{

						
						numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour;
						numberOfPickleJars = numberOfAnticipatedGuests / picklesPerJar;
						potatoChipBoxes = numberOfAnticipatedGuests / servingsPerBox;

					}
						else if (picklesNeeded == NO_TO_PICKLES && chipsNeeded == NO_TO_CHIPS)
						{

						
							numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour;
							numberOfPickleJars = 0;
							potatoChipBoxes = 0;

						}

					avgNumOfGuestsPerHr = numberOfAnticipatedGuests / lengthOfEventInHours;


			cout << "your number of anticipated guests is: ";
			cout << numberOfAnticipatedGuests;
			cout << "the length of your event is: ";
			cout << lengthOfEventInHours;
			cout << "the average number of guests per hour is";
			cout << avgNumOfGuestsPerHr;
			cout << "the number of griddles needed for grilled cheese is";
			cout << numberOfCheeseGriddles;
			cout << "the number of pickle jars needed is";
			cout << numberOfPickleJars;
			cout << "the number of chip boxes needed is";
			cout << potatoChipBoxes;

		
		}
		else
		{
			cout << ERROR << endl;
			return 0;
		}

		return 0;
	}
Hello snappleisawesome23,

When you say second gathering type does not what do you mean?

I could not find any problem with the if/else or if/else if statements. Now inside the if statements I did find some problems like a variable, "realPancake", not having a value and in choice 2 there is one or more variables being used that were not initialized which caused the program to crash for me. I need to do some more checking.

My first suggestion is to initialize all variables when they are defined.

I noticed in line 14 YES_TO_CHIPS = 'N' see anything wrong here?

Some of your variables used to calculate an answer have no usable value or no value at all.

I will have to work up a better list of what I find that is not working.

Hope that helps,

Andy
I changed that to a Y and it seemed to fix it. I have one more issue, I need to round up on the equation numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour, but I cant use ceil or any function, I need to use int double and truncation.
Hello snappleisawesome23,

I need to round up on the equation numberOfCheeseGriddles = numberOfAnticipatedGuests / grilledCheesePerHour

You can not round up because it already is rounded up to zero. Read on for a suggestion.

line 45: numberOfPanGriddles = realPancake / pancakesPerHour
If "realPancake" is less than "pancakesPerHour" than "numberOfPanGriddles" is zero based on integer division. Seems a bit difficult to cook pancakes with no griddle.

Same for next line.

line 50: This just needs to be an else. Since the possible choices are only two if it is not "Y" then the only other choice is "N". Of course the variable "baconNeeded" could contain something other than "Y/N" which could cause a problem.

Line 53: Is not needed because you are not dealing with bacon.

Line 54: same as lines 45 and 46.

The cout statements starting at line 60 show the problem with the variables " numberOfPanGriddles" and "numberOfBaconGriddles".

The same problem concepts are in the if statement for option 2.

For "numberOfGriddles" vriable you should follow these with a check for zero and set the "numberOfGriddles" variables to at least 1. At least it would show that you have something to cook with.
1
2
if (numberOfPanGriddles == 0)
	numberOfPanGriddles = 1;  // <--- Or any number tht is approperate. 


It is very refreshing to see variable names the actualy say what ther are for. Good job.

A few blank here and there would make some of the code easier to read.

The output for option 2 needs some work to make what is printed on the screen not be one line and the line for hours could use the word "hour(s)" after the number. same for option 1.

Hope that helps,

Andy
Hello snappleisawesome23,

Just an idea for how I changes the output:

would you like bacon? Y/N: y

Bacon Chosen

your number of anticipated guests is: 300
the length of your event is: 4 hour(s)
the average number of guests per hour is: 75
the number of pancake griddles: 3
the number of bacon griddles: 1


After doing this for awhile what goes to the screen bugs me when things run together.

Andy
Last edited on
I understand that, I just need it to round up if say the result is 1.4, I need that to be 2 as I need 2 whole griddles then.

Thank you for the help!
Last edited on
Hello snappleisawesome23,

You are using integer division. There is not answer that will ever be 1.4 because integer division will drop the .4 and leave the whole number.

If you change the variables used in the division to doubles you will have an answer like 1.4 that could be rounded up or down.

Hope that helps,

Andy
Topic archived. No new replies allowed.