help please

I cannot get the item's names to show up on the same line, and I'm not sure what I'm doing wrong. Can someone please look at my code and help me get through this part of my assignment? Also, there are some parts on the code that I haven't defined yet but that's because I'm still working on them, and they're commented out right now since I'm trying to figure out how to put the item names on the same line and can't get past it. Thanks to anyone who helps me.

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

using namespace std;

const double taxRate = 0.09;

const int kStandardNameWidth = 14;

const char kStandardTypeWidth = 14;

const int kStandardPriceWidth = 14;

const int kTotalColumnWidth = 30;

const int kStandardPrecision = 2;

const int kLineLength = 45;

int itemCount = 0;



int main()

{
	cout << " Opening File to read items... ";
	cout << endl;

	ifstream fin;

	fin.open("Items.txt");
	string name = "";
	getline(fin, name);
	double price = 0.0;
	fin >> price;
	char type = ' ';
	fin >> type;
	itemCount = itemCount + 1;

	cin.clear();
	cin.ignore(1, '\n');

	cout << fixed << showpoint
		<< setw(kStandardPrecision);



	while (!fin.eof())
	{
		fin >> name;


		cout << name << endl;


	}
	//cout << fixed << showpoint
	//	<< setprecision(kStandardPrecision);

	fin.close();
	
	cout << endl;

//	double subtotal = 0.0;
//	subtotal = 
//
//	double tax = 0.0;
//	tax = subtotal * taxRate;
//
//	double totalDue = 0.0;
//	totalDue = subtotal + tax;
//	cout << setfill(' ') << left << setw(25)
//		<< "Total due is  ";  cout << " $" << totalDue
//		<< setfill(' ') << left << setw(25);
//	cout << endl;
//
//	cout << fixed << showpoint << setprecision(kStandardPrecision);
//	cout << setfill(' ') << left << setw(25)
//		<< "Enter amount paid :"; cout << " $";
//	double amountPaid = 0.0;
//	cin >> amountPaid;
//
//	cout << endl;
//	cout << fixed << showpoint << setprecision(kStandardPrecision);
//	cout << setfill(' ') << left << setw(25)
//		<< "Debit Tend : "; cout << " $";
//	cout << amountPaid;
//	cout << endl;
//
//	double changeDue = 0.0;
//	changeDue = amountPaid - totalDue;
//	cout << fixed << showpoint << setprecision(kStandardPrecision);
//	cout << setfill(' ') << left << setw(25)
//		<< "Change Due : ";
//	cout << " $";
//	cout << changeDue;
//	cout << endl;
//
//	ofstream fout;
//	fout.open("Receipt.txt");
//
//	fout << fixed << showpoint << setprecision(kStandardPrecision)
//		<< left << "My Store " << endl;
//	fout << endl;
//
//	fout << setw(kStandardNameWidth) << left << name1
//		<< setw(kStandardPriceWidth) << right << price1
//		<< '-' << type1
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << name2
//		<< setw(kStandardPriceWidth) << right << price2
//		<< '-' << type2
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << name3
//		<< setw(kStandardPriceWidth) << right << price3
//		<< '-' << type3
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << "Subtotal"
//		<< setw(kStandardPriceWidth) << right << subtotal
//		<< endl;
//
//	double taxPercent = 0.0;
//	taxPercent = taxRate * 100;
//	fout << setw(kStandardTypeWidth) << left << "Tax @ 9.00%"
//		<< setw(kStandardPriceWidth) << right << tax
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << "Total"
//		<< setw(kStandardPriceWidth) << right << totalDue
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << "Debit tend"
//		<< setw(kStandardPriceWidth) << right << amountPaid
//		<< endl;
//
//	fout << setw(kStandardNameWidth) << left << "Change Due"
//		<< setw(kStandardPriceWidth) << right << changeDue
//		<< endl;
//
//	fout << "# items sold " << itemCount << endl;
//
//	fout << "Thank you for shopping with us!";
//
//	fout.close();
//
//	cout << endl;
//
//	cout << "Printing receipt... ";
//	cout << endl;
//	cout << "Receipt printed.";
//	cout << endl;
//
	return 0;

}
Last edited on
What is the assignment description?
Don't use endl if you want the next output to be on the same line.
Homework 3 – PROG 1003 Shopping Cart 3
Building on the program from Homework 2 - Cart.cpp.
Following the examples, we wrote in class, read an unknown number of items from the provided text file – ItemList.txt. You will use the same data types - Item name should be a string, item type should be a char, and item price should be a double.
 All output to the user (on the console) should show a “$” and be rounded to two decimal places with a pleasing format – set column widths and spacing.
 Assume a tax rate of 9% for standard items with a category of ‘N’
 Assume a tax rate of 12.5% for special items with a category of ‘X’
 Assume a tax rate of 0% for food items with a category of ‘F’
 Calculate a subtotal, tax, and total.
 Display the total to the user.
 Allow the user to enter the amount to pay (do not assume the amount will be equal to or more than
what is due, or that the input will be a valid number). Display the change due to the user.
 Create a print friendly receipt that closely matches the one provided – Receipt.txt
The point of this exercise is to learn how to use loops to handle an unknown amount of information and how to manipulation file I/O as the same time. Remember no magic numbers, follow the coding conventions, that all output should be properly formatted.


Here is the assignment description.
I'm using the ignore command and it's separating the text in the description field when it encounters the first space. The items I'm trying to read are these :

keyboard
22.62
N
blouse
18.17
N
plastic fork
20.16
X
credit card
14.61
F
perfume
2.76
N
playing card
2.08
N
table
5.56
N
toothbrush
16.71
F
thermometer
12.38
N
canvas
22.43
X
bottle
7.29
X
cup
14.27
X
nail file
23.07
X
slipper
5.66
N
spring
7.57
F
apple
4.28
X
food
4.66
F
chapter book
16.89
F
rusty nail
20.25
F
rubber duck
23.96
F
sharpie
20.60
X
bottle cap
13.00
X
cookie jar
7.54
X
needle
24.96
X
sailboat
23.47
F
nail clippers
11.72
X
puddle
17.89
N
white out
13.95
F
bow
15.51
F
deodorant
21.45
N
candle
14.72
F
button
7.13
X
teddies
4.75
F
bananas
15.50
N
ring
23.14
N
thread
14.42
X
rug
8.02
F
beef
10.60
X
camera
5.23
F
CD
17.86
F
door
7.61
N
twezzers
2.39
F
flag
14.26
F
house
16.84
X
stop sign
3.89
X
book
2.70
N
ipod
18.05
N
soda can
14.36
F
cat
20.93
F
coasters
2.25
X
shoe lace
22.54
N
controller
19.90
X
balloon
16.15
N
wallet
3.06
N
toilet
2.66
F
clay pot
22.15
N
scotch tape
2.33
X
soy sauce packet
2.11
X
tire swing
19.40
F
toothpaste
21.49
F
newspaper
2.15
N
seat belt
13.46
F
shirt
19.60
N
bowl
6.70
X
magnet
15.14
F
charger
15.52
N
sticky note
7.79
N
lamp shade
9.77
N
hair tie
16.88
F
blanket
9.91
X
towel
11.76
N
fridge
21.05
N
fake flowers
16.56
F
greeting card
15.56
F
knife
21.24
N
boom box
7.40
F
floor
21.74
X
pillow
15.70
X
sand paper
23.93
F
zipper
12.45
F
pool stick
21.06
X
tomato
20.03
F
fork
8.91
F
clamp
23.35
N
paint brush
21.84
N
speakers
5.53
F
glass
9.77
F
window
20.01
X
thermostat
13.17
X
truck
15.74
F
paper
1.54
F
watch
4.24
F
couch
16.46
F
bag
10.56
X
shampoo
19.98
F
air freshener
24.83
X
leg warmers
13.34
X
rubber band
19.17
X
pencil
14.22
X
street lights
19.45
F

There's about 100 of them, I can get the single words to show up just fine but the ones like "street lights" and "rubber band" I cannot get to show up on the same line. I think it has something to do with my ignore command but I'm not quite sure how to get it to show up properly on the same line, I've tried moving the ignore command and taking things out but nothing seems to get it to work.
I can get the single words to show up just fine but the ones like "street lights" and "rubber band" I cannot get to show up on the same line. I think it has something to do with my ignore command but I'm not quite sure how to get it to show up properly on the same line, I've tried moving the ignore command and taking things out but nothing seems to get it to work.


The problem is not the ignore() function call, the problem is that the extraction operator stops processing the string when it encounters a whitespace character. If you want to retrieve a string that contains spaces you should be using getline(), not the extraction operator. Looking at your input file I suggest you use getline() to read every line then use a stringstream to parse the line to convert the string to the double and the character.

Topic archived. No new replies allowed.