Help to my first program

Forgive me as I am currently in the process of learning programming. This is my 3rd day studying and I designed a simple program. It prompts a individual to grocery shop. I am pretty proud of the work Ive done. I wanted to get a better understanding on the fundamentals of if statements, and functions. How ever in the process Ive seem to not properly account for the scope of a variable. So my question is , after reviewing the code, where have I got wrong in reguards to the totalCustomerPurchase variable located in main? I think possibly it may be due to the while loop , and each time it loops it resets the variable. This being the case the totalCustomerPurchase would only contain the information from the most recent loop. Am I on the right track in that thinking???

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
  
/* This program will mimic your shopping experience at a store. It will ask you for how much money you may have on you at the beginning. It will than show you a list of aisles ,
ask which one you would like to go down. After being on a aisle you wil be able to select from the list of items on it. After making your selections , you can check out
and pay for your items. */
#include <iostream>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


float howMuchMoney(float x, float y, float z)
{
	cout << "How much cash do you have?" << endl;
	cin >> x;
	cout << "How much is on your debit card?" << endl;
	cin >> y;
	cout << "How much is on your credit card?" << endl;
	cin >> z;
	return x + y + z;
}

float storeAisles()

{
float totalCustomerPurchase;
float bread = 1;
float hotDogBuns = 1;
float hamburgerBuns = 1;
float cannedBeans = .50;
float cannedCorn = .60;
float cannedPeas = .40;
float cereal = 3.98;
float poptarts = 3.00;
float oatmeal = 1.98;
float flour = 1.98;
float sugar = 2.48;
float spices = 1.48;


	cout << "These are our aisles in our store, with a great selection of items." <<endl;
	
	cout <<"Aisle 1          Aisle 2          Aisle 3          Aisle 4" << endl 
	<<"Bread            Canned Beans     Cereal	           Flour" << endl
	<<"Hot Dog Buns     Canned Corn      Pop Tarts         Sugar" << endl
	<<"Hamburger Buns   Canned Peas      Oatmeal           Spices"<< endl;
	
	cout << endl << endl;
	
	int aisle = 0;
	do{
	
		cout << "Which aisle would you like to go down? 1 , 2 , 3 , or 4?";
		cin >> aisle;
		if (aisle==1)
		{
			float item;
			cout << "Here on aisle 1 we have the following:" << endl;
			cout << "1. Bread - $1.00" << endl;
		 	cout << "2. Hot Dog Buns - $1.00" << endl;
			cout << "3. Hamburger Buns - $1.00" << endl;
			
			cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
			cin >> item;
			if(item == 1)			
						{
							totalCustomerPurchase = totalCustomerPurchase + bread;
						}
			if (item == 2){
							totalCustomerPurchase = totalCustomerPurchase + hotDogBuns;
			}
			if(item ==3)
			{
							totalCustomerPurchase = totalCustomerPurchase + hamburgerBuns;
			}
		}
			 		
		if (aisle == 2)
		{
			float item;
			cout << "Here on aisle 2 we have the following:" << endl;
			cout << "1.Canned Beans - .50" << endl;
			cout << "2.Canned Corn - .60" << endl;
			cout << "3.Canned Peas - .40" << endl;
			
						cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
			cin >> item;
			if(item == 1)			
						{
							totalCustomerPurchase = totalCustomerPurchase + cannedBeans;
						}
			if (item == 2){
							totalCustomerPurchase = totalCustomerPurchase + cannedCorn;
			}
			if(item ==3)
			{
							totalCustomerPurchase = totalCustomerPurchase + cannedPeas;
			}
		}
		if(aisle == 3)
		{
			float item;
			cout << "Here on aisle 3 we have the following:" << endl;
			cout << "1.Cereal - $3.98" << endl;
			cout << "2.Poptarts - $3.00" << endl;
			cout << "3.Oatmeal - $1.98" << endl;
		
					cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
			cin >> item;
			if(item == 1)			
						{
							totalCustomerPurchase = totalCustomerPurchase + cereal;
						}
			if (item == 2){
							totalCustomerPurchase = totalCustomerPurchase + poptarts;
			}
			if(item ==3)
			{
							totalCustomerPurchase = totalCustomerPurchase + oatmeal;
			}
		}
		
		if(aisle == 4)
		{
			float item;
			cout << "Here on aisle 4 we have the following:" << endl;
			cout << "1.Flour - $1.98" << endl;
			cout << "2.Sugar - $2.48" <<endl;
			cout << "3.Spices $1.48" << endl;
			
						cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
			cin >> item;
			if(item == 1)			
						{
							totalCustomerPurchase = totalCustomerPurchase + flour;
						}
			if (item == 2){
							totalCustomerPurchase = totalCustomerPurchase + sugar;
			}
			if(item ==3)
			{
							totalCustomerPurchase = totalCustomerPurchase + spices;
			}
		}
		if (aisle > 4 || aisle < 1)
		{
			cout << "You did not pick a valid aisle with in Eric Mart";
			return 0;
		}
	}while(aisle = 0);
	return totalCustomerPurchase;
}
int main(int argc, char** argv) {
	float totalCustomerPurchase;
	char custChoice = 'Y';
	
	
	cout << "Welcome to Eric's Mart. " << endl;
	
	float customerMoneyTotal = howMuchMoney(0,0,0);
	while(custChoice == 'Y')
	{
	
	totalCustomerPurchase = storeAisles();
	cout << "Woud you like to continue shopping? Y or N." << endl;
	cin >> custChoice;
}

	cout << "The total for your order was " << totalCustomerPurchase << "." <<endl;
	if (totalCustomerPurchase > customerMoneyTotal)
	{
		cout << "I am sorry, but you are not able to afford these items";
	}
	else
	{
		cout << "Thank you for shopping with us, have a great day!";
	}
	return 0;
}
just as advice, for your float variables to which you assigned values, add the f prefix, as in float a = 0.5f; to tell the compiler it is a float, else it will get promoted to a double.
Thank you for that tip.
I think your error is writing float values like .67 it should be 0.67
just as advice, for your float variables to which you assigned values, add the f prefix, as in float a = 0.5f; to tell the compiler it is a float, else it will get promoted to a double.


Better, use double all the time.

I think your error is writing float values like .67 it should be 0.67


That's not an error, but it is good advice. Always put digits before and after the decimal place like 1.0

@kidd2100

Well done so far!! :+)

Always compile with the warning levels set to their maximum, here is a warning from cpp.sh with all 3 warning options on:

In function 'float storeAisles()': 149:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]


= is assignment (always true)

== is a comparison, and is what you want here in the conditional.

Is comparison to zero what you really want here?

Another way of doing this is with a switch You can put it inside a while loop, and use a bool variable to help decide when to Quit. Provide a quit option in your menus, and in the switch

http://www.cplusplus.com/doc/tutorial/control/


152:14: warning: unused parameter 'argc' [-Wunused-parameter]
152:27: warning: unused parameter 'argv' [-Wunused-parameter]


If you don't use the argc and argv, then don't have them, or at least comment them out.

Some other advice:

You need more functions. Functions should only do 1 conceptual thing. Some candidates are your menus, have a function for each one. Each option on a menu should also call a function.

Functions should be short, aim for 40 LOC or even less.

You have type problems on lines 101 and 109 and similar, item should be an unsigned int Equality comparisons with float or double is a totally different and evil animal.

Don't have using namespace std;, just put std:: before each std thing, believe me it's the best way, and is what all the experts do. So if you start dong it now, you will be ahead of the game a bit. Google to see why it's bad.

This:
totalCustomerPurchase = totalCustomerPurchase + cannedBeans;

can be written like this:
totalCustomerPurchase += cannedBeans;

It's also good to put function declarations before main, then functions definitions after main, so main is always at the top.

There you go some stuff to work on :+)
Thank you for the tips. Will research more on switch to see where I can utilize. Thanks
I took the input and I believe I have implemented the majority of it. I have not changed over std as I'm not comfortable with where and when to use with out using namespace. Better?
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This program will mimic your shopping experience at a store. It will ask you for how much money you may have on you at the beginning. It will than show you a list of aisles ,
ask which one you would like to go down. After being on a aisle you wil be able to select from the list of items on it. After making your selections , you can check out
and pay for your items. */
#include <iostream>

using namespace std;
// The methods for this program. Methods are defined here, and listed below main.

int aisleOne();
int aisleTwo();
int aisleThree();
int aisleFour();
float storeAisles();
float howMuchMoney(float x, float y, float z);

//Program to determine customers money. Than allows the customer to see what store has. After a user picks his items, his total is cross checked with 
// his total worth. If the customer has enough money his purchase is completed, if not he is told he does not have enough money.
int main() {
	float totalCustomerPurchase;
	float customerPurchase;
	char custChoice = 'Y';
	
	
	cout << "Welcome to Eric's Mart. " << endl;
	
	float customerMoneyTotal = howMuchMoney(0.0,0.0,0.0);
	while(custChoice == 'Y' || custChoice == 'y')
	{
	
	totalCustomerPurchase = storeAisles();
	customerPurchase = customerPurchase + totalCustomerPurchase;
	cout << "Woud you like to continue shopping? Y or N." << endl;
	cin >> custChoice;
}

	cout << "The total for your order was " << customerPurchase << "." <<endl<<endl<<endl;
	if (totalCustomerPurchase > customerMoneyTotal)
	{
		cout << "I am sorry, but you are not able to afford these items";
	}
	else
	{
		cout << "Thank you for shopping with us, have a great day!";
	}
	return 0;
}

// The next three methods show what goes on each aisle and prints it out to the user.  

int aisleOne()
{
			cout << "Here on aisle 1 we have the following:" << endl;
			cout << "1. Bread - $1.00" << endl;
		 	cout << "2. Hot Dog Buns - $1.00" << endl;
			cout << "3. Hamburger Buns - $1.00" << endl;
			
			cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
}

int aisleTwo()
{
			cout << "Here on aisle 2 we have the following:" << endl;
			cout << "1.Canned Beans - .50" << endl;
			cout << "2.Canned Corn - .60" << endl;
			cout << "3.Canned Peas - .40" << endl;
			
			cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
}

int aisleThree()
{
			cout << "Here on aisle 3 we have the following:" << endl;
			cout << "1.Cereal - $3.98" << endl;
			cout << "2.Poptarts - $3.00" << endl;
			cout << "3.Oatmeal - $1.98" << endl;
		
			cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";
}

int aisleFour()
{
			cout << "Here on aisle 4 we have the following:" << endl;
			cout << "1.Flour - $1.98" << endl;
			cout << "2.Sugar - $2.48" <<endl;
			cout << "3.Spices $1.48" << endl;
			
			cout << "Type the number in for the item you would like to purchase. Which item would you like to purchase?";	
}

// Activates and shows user the aisles. Calculates and adds each customer choice under variable totalCustomerPurchase. This is returned to main

float storeAisles()

{
float totalCustomerPurchase;
float bread = 1.0;
float hotDogBuns = 1.0;
float hamburgerBuns = 1.0;
float cannedBeans = 0.50;
float cannedCorn = 0.60;
float cannedPeas = 0.40;
float cereal = 3.98;
float poptarts = 3.00;
float oatmeal = 1.98;
float flour = 1.98;
float sugar = 2.48;
float spices = 1.48;


	cout << "These are our aisles in our store, with a great selection of items." <<endl;
	
	cout <<"Aisle 1          Aisle 2          Aisle 3          Aisle 4" << endl 
	<<"Bread            Canned Beans     Cereal	           Flour" << endl
	<<"Hot Dog Buns     Canned Corn      Pop Tarts         Sugar" << endl
	<<"Hamburger Buns   Canned Peas      Oatmeal           Spices"<< endl;
	
	cout << endl << endl;
	
	int aisle = 0;
	
	
		cout << "Which aisle would you like to go down? 1 , 2 , 3 , or 4?";
		if (aisle != 1 || aisle != 2 || aisle !=3 || aisle !=4 )
		{
			
			cout << "Which aisle would you like to go down? 1 , 2 , 3 , or 4?";
			cin >> aisle;
		}
		switch(aisle)
		{
		
			case 1:
				
					int item;
					aisleOne();
					cin >> item;
					switch(item)
					{
						case 1:
							totalCustomerPurchase += bread;
							break;
						case 2:
							totalCustomerPurchase += hotDogBuns;
							break;
						case 3:
							totalCustomerPurchase += hamburgerBuns;
							break;
						default:
							break;
						}	
		 	break;
			case 2:
					 
					aisleTwo();
					cin >> item;
					switch(item)
					{
						case 1:
							totalCustomerPurchase += cannedBeans;
							break;
						case 2:
							totalCustomerPurchase += cannedCorn;
							break;
						case 3:
							totalCustomerPurchase += cannedPeas;
							 break;
						default:
							break;
					}
			break;
			case 3:
					
					aisleThree();
					cin >> item;
					switch(item)
					{
						case 1:
							totalCustomerPurchase += cereal;
							break;
						case 2:
							totalCustomerPurchase += poptarts;
							break;
						case 3:
							totalCustomerPurchase += oatmeal;
							break;
						default:
							break;
					}
					
			break;
			case 4:
					
				 	aisleFour();
					cin >> item;
					switch(item)
					{
						case 1:
							totalCustomerPurchase += flour;
							break;
						case 2:
							totalCustomerPurchase += sugar;
							break;
						case 3:
							totalCustomerPurchase += spices;
							break;
						default:
							break;
					}
			break;
			default:
			break;
}
		if (aisle > 4 || aisle < 1)
		{
			cout << "You did not pick a valid aisle with in Eric Mart";
			return 0;
		}
		
	
	return totalCustomerPurchase;
}

// Records input from user to store how much money customer has. Later uses in main to determine if customer can afford purchase
float howMuchMoney(float x, float y, float z)
{
	cout << "How much cash do you have?" << endl;
	cin >> x;
	cout << "How much is on your debit card?" << endl;
	cin >> y;
	cout << "How much is on your credit card?" << endl;
	cin >> z;
	return x + y + z;
}
Last edited on
I took the input and I believe I have implemented the majority of it.


Excellent, I am pleased you are having a go !! So many young guys seem to ignore advice these days.

I have not changed over std as I'm not comfortable with where and when to use with out using namespace.


The only ones you really need are std::cin and std::cout You can change them in bulk by using find/replace in your editor. For anything else, you should be looking at the documentation anyway, so you will discover whether or not it is in std.

Avoid using std::endl as it flushes the buffer every time, this could be a little inefficient. Just use '\n' instead:

std::cout << "How much cash do you have?\n";

There are times when std::endl is OK, as is writing to log files, where one wants to ensure the latest data is being written, but not needed here.

int aisleOne();
These functions can be void, they don't return anything.

Change all your float to double. double is the default, this is because the precision of float is easily exceeded.

With line 27, you can make use of the toupper() or tolower() function to make this easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <locale>
.
.
.
	while(custChoiceAsUpperCase == 'Y')
	{
	   // indentation makes code easier to read
	   totalCustomerPurchase = storeAisles();
	   customerPurchase +=   totalCustomerPurchase;
	   std::cout << "Woud you like to continue shopping? Y or N.\n";
	   std::cin >> custChoice;
           char custChoiceAsUpperCase = std::toupper(custChoice);
        }



Lines 95 to 107, these can all be const.

95
96
97
const float totalCustomerPurchase;
const float bread = 1.0;
const float hotDogBuns = 1.0;


const is a really good feature of C++, it means the compiler can enforce the idea that you are not going to change it's value.

Lines 110 to 117 can be a void function, like the other menus.

With the switch, consider changing aisle to char, then you can have a Quit case ('Q')

Lines 123 to 128:
By putting the switch into a while loop, we can handle errors with the default: case of the switch.

Each case of the outer switch should call it's own function: ProcessAisleOne(), ProcessAisleTwo() ... say. The inner switched are OK, they only have one-liners.


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
bool Quit = false;

while (!Quit) {
   char aisle = 'z';

   std::cout << "Which aisle would you like to go down? 1 , 2 , 3 ,  4? Q to quit\n";
   std::cin >> aisle;

   switch(aisle) {
      case: '1'
         ProcessAisleOne();
         break;
.
.
.
      case: 'Q' // "Fall though", no break on purpose
      case: 'q'
           Quit = true;
           break;

      default:  // catch errors with this
         std::cout << "Wrong Choice, try again";
         break;
   } // end switch

} // end while 


With howMuchMoney you don't need parameters / arguments here. Remember to change the function declaration on line 14.

1
2
float howMuchMoney()
{


There we go, some more stuff to work on - you are doing well :+)
Lots of tips there. I wrote this program to better understand functions. With your help I've learned the switch statement to. So that's really awesome. I was worried on using double variables due to working with money.
Also it seems using names pace eliminates alot of typing, why is it frowned on?
I was worried on using double variables due to working with money.


I wouldn't worry about it here, if you were doing a proper commercial program then yes. In any case float would be worse than double , float has 6 or 7 significant figures, while double has 15 or 16, so double is much more superior. When you print out a value, rounding still happens, so most of the time it won't make a lot of difference. A commercial program might have a currency struct, with the dollars and cents stored as ints. Then it would have a bunch of code to allow that type to be used in calculations.

Also it seems using names pace eliminates alot of typing, why is it frowned on?

Well you can Google that one, plenty has been written about it. It's a good idea to put your own stuff into it's own namespace. That is the purpose of them, to avoid having the same variable or function name in the same scope. Bringing in an entire namespace ruins that idea.

Good Luck!!
Topic archived. No new replies allowed.