Explain Arrays Please

Pages: 1... 456789
Heres my output
As far as i can tell its reading every line from the Candy.txt file instead of just adding one.

C:\C++\Lab10>a


Candy Inventory Management Program
1. Add a piece of candy to inventory
2. Calculate totals for hard candy
3. Calculate totals for jelly beans
4. Calculate totals for chocolates
5. Calculate totals for all candy
6. Quit the program
Choose 1 - 6: 2

-----------------------------------------------------------------

Total Pieces Of Hard Candy:

TYPE MATERIALS COST LABOR HOURS ASKING PRICE
Hard Candy 7.46 2.00 27.46

Hard Candy 3.45 2.70 30.45

Hard Candy 2.31 0.50 7.31

Hard Candy 8.32 2.30 31.32

Hard Candy 2.11 0.75 9.61

Hard Candy 2.23 1.25 14.73

Hard Candy 14.89 2.30 37.89

Hard Candy 1.20 3.00 31.20

Hard Candy 2.00 2.00 22.00

Hard Candy 1.00 1.00 11.00

Hard Candy 2.00 2.00 22.00


TOTALS: $ 46.97 19.80 $244.97

-----------------------------------------------------------------



Candy Inventory Management Program
1. Add a piece of candy to inventory
2. Calculate totals for hard candy
3. Calculate totals for jelly beans
4. Calculate totals for chocolates
5. Calculate totals for all candy
6. Quit the program
Choose 1 - 6:
Last edited on
check that you dont have a data file with those items in because when the program first loads it opens it.
I have a file named Candy.txt that its reads this from
1 7.46 2

2 3.45 2.7

3 2.31 0.5

1 8.32 2.3

2 2.11 0.75

1 2.23 1.25

2 14.89 2.3

3 1.2 3

1 2 2

1 1 1

1 2 2

as you can see its just directly reading form this every line when its only supposed to take one.
Last edited on

I spotted another bug in your program inside calculateTotals. Change the for loop to this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

	if (mtype >= 2 && mtype <= 5) {
		for (int itemno = 0; itemno < numItems; itemno++) 
		{
			if ((mtype-1) == type[itemno] || mtype == 5) {

				price[itemno] = calculatePrice(cost[itemno], hours[itemno]);
				totalCost += cost[itemno];
				totalHour += hours[itemno];
				totalPrice += price[itemno];

				cout << setw(13) << left << canType;
				cout << setw(13) << right << cost[itemno]
					<< setw(16) << setprecision(2) << fixed << hours[itemno]
					<< setw(16) << price[itemno] << "\n\n";
			}

		}
	}


I now understand the reasons for the if statement test on mtype, but it was wrong and also in the wrong place. let me try to explain why: you are using the menu option to check against the data item.. yes? - well when selecting candy type its 1 - 3 yet on the main menu 1 is taken up by Add Candy so this would fail.

A way around that would be to subtract one from the mtype during testing... hope that made sense :)
Last edited on
Subtrack 1 in the if state like you have above? did that still giving me bonker output.
still give all the information in the candy file.

Doesn't here, have you done everything as discussed? - repost the whole project again (including your data file) so I can check it over.

Working fine here with your latest file and data file, I press 1 it shows me 6 Hard Candy's

Total Pieces Of Hard Candy:

TYPE MATERIALS COST LABOR HOURS ASKING PRICE
Hard Candy 7.46 2.00 27.46
Hard Candy 8.32 2.30 31.32
Hard Candy 2.23 1.25 14.73
Hard Candy 2.00 2.00 22.00
Hard Candy 1.00 1.00 11.00
Hard Candy 2.00 2.00 22.00

TOTALS: $ 23.01 10.55 $128.51

-----------------------------------------------------------------


If I select 3 I get 4 Jelly Beans


Total Pieces Of Jelly Beans:

TYPE MATERIALS COST LABOR HOURS ASKING PRICE
Jelly Beans 3.45 2.70 30.45
Jelly Beans 2.11 0.75 9.61
Jelly Beans 14.89 2.30 37.89
Jelly Beans 2.00 2.00 22.00

TOTALS: $ 22.45 7.75 $99.95

-----------------------------------------------------------------


This matches up with your data file contents, not sure what you are doing that end.

If I press 5 it shows all:


Total Pieces Of Candy in Inventory:

TYPE MATERIALS COST LABOR HOURS ASKING PRICE
Candy in Inventory 7.46 2.00 27.46
Candy in Inventory 3.45 2.70 30.45
Candy in Inventory 2.31 0.50 7.31
Candy in Inventory 8.32 2.30 31.32
Candy in Inventory 2.11 0.75 9.61
Candy in Inventory 2.23 1.25 14.73
Candy in Inventory 14.89 2.30 37.89
Candy in Inventory 1.20 3.00 31.20
Candy in Inventory 2.00 2.00 22.00
Candy in Inventory 1.00 1.00 11.00
Candy in Inventory 2.00 2.00 22.00
Candy in Inventory 2.00 2.00 22.00

TOTALS: $ 48.97 21.80 $266.97

-----------------------------------------------------------------

Here I'm adding the actual assignment in to the drop box folder. I don't think its supposed to output like that because that is what my output says.
https://www.dropbox.com/s/wgylnriy6vgnkay/Lab10.zip

Well other than option 5 everything is displaying correctly as per the test output of your pdf. With regards to option 5 all you needed to do is test the type[itemno] to see what value it was and print out depending on that - easy stuff - a simple switch could take care of that like so...

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

		for (int itemno = 0; itemno < numItems; itemno++)
		{
			if ((mtype - 1) == type[itemno] || mtype == 5) {
				price[itemno] = calculatePrice(cost[itemno], hours[itemno]);
				totalCost += cost[itemno];
				totalHour += hours[itemno];
				totalPrice += price[itemno];

				switch (type[itemno])
				{
					case 1:
						cout << setw(13) << left << "Hard Candy";
					break;
					case 2:
						cout << setw(13) << left << "Jelly Beans";
					break;
					case 3:
						cout << setw(13) << left << "Chocolate";
					break;
					default:
						cout << setw(13) << left << "DEBUG: Invalid candy!!";

				}

				cout << setw(13) << right << cost[itemno]
					<< setw(16) << setprecision(2) << fixed << hours[itemno]
					<< setw(16) << price[itemno] << "\n\n";
			}
		}



which will now give an output for option 5 like...

n.b. alignment issues are this forum not the code.

-----------------------------------------------------------------

Total Pieces Of Candy in Inventory:



TYPE MATERIALS COST LABOR HOURS ASKING PRICE

Hard Candy 7.46 2.00 27.46
Jelly Beans 3.45 2.70 30.45
Chocolate 2.31 0.50 7.31
Hard Candy 8.32 2.30 31.32
Jelly Beans 2.11 0.75 9.61
Hard Candy 2.23 1.25 14.73
Jelly Beans 14.89 2.30 37.89
Chocolate 1.20 3.00 31.20
Hard Candy 2.00 2.00 22.00
Hard Candy 1.00 1.00 11.00
Hard Candy 2.00 2.00 22.00
Jelly Beans 2.00 2.00 22.00

TOTALS: $ 48.97 21.80 $266.97

-----------------------------------------------------------------



To be honest these kinda additions such as the one above you should be able to do now yourself after all this practice and previous discussions we've had... ;-)
Last edited on
ok i read over the entire program just a few minutes ago, i think i have been trying to solve an imaginary problem for the past 18 hours, i had fixed the program last night(to a working degree) i had already tried some of the corrections you suggested, as the == to just = and i found a few others before i sent it to you in the first place. It just occurred to me that the candy.txt file is supposed to display everything in it of each type. I feel so dumb right now.

Not sure if I am missing your point here but the data file layout is correct as per the pdf, i.e. it doesnt actually store the words "Hard Candy" which can be seen on page 4 of the pdf.

My point is i mis interpreted that part of the program, my reading comprehension is lousy, im a recovering dyslexic :p.

so then, on that note.. is the program working the way you need it to be ;-)
Haha yes sir thank you for all your help again! Ive only got a few more weeks of this class and one more program and im free!!!! But i guess i need to keep practicing afterward so ill be prepared for data structures.

Heres some handy links for c++ which may help you on your path...

http://www.3dbuzz.com/training/view/c-plus-plus-complete/introduction-to-c
http://www.3dbuzz.com/training/view/c-plus-plus-complete/intermediate-techniques

Not everything on that site is free but those are, and they are pretty good. The pointers one is nice for the stage your at and video is always better than trying to dip your head into a book.

Thank you kind sir i shall defiantly use those.
Hey, How would you make a program count the number of characters, and distinguish if those are upper or lower case then count how many of each there are in a text file? This part of one of my programs has got me stumped.
<cctype>
so isupper, islower, but how do i make it count it?
Pages: 1... 456789