reading from a 4 column .txt file

i have a .txt file that im reading from that looks like this:::

H 80 6 S
M 15 0 D
E 30 2 S
M 23 7 D
S 54 3 S
O 72 2 D
H 85 0 S
O 54 9 S

disregarding the first column....

the second and third column represent an amount of people (Adults, Children)
(NOTE: adults and children have different multiply values)
adult S value = 5.00
adult D value = 7.00
children S value = 2.00
children D value = 4.00


how do i tell the program to multiply by the "S" or "D" values for the correct column ?

im very... very new to C++, any help to get me on my way would help
Files are read line by line.
You code will be like this:
while you can read things from the file,
   cin >> something
   cin >> adults
   cin >> children
   cin >> SorD
   if SorD == "S" then adults *= 5; children *= 2;
   else adults *= 7; children *= 4;
   do stuff ...
i get that part, the whole line by line thing....
...i guess where i get confused at is when you have to use functions outside the main....
.. i draw a complete blank and get all confused :/
heres my code. its a little.. maybe A WHOLE LOT of a mess... im kinda lost.. kinda not.. i dont know.. let me know what you think.

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
// ROOM CHARGES
const double huronCharge = 250.00;
const double ontarioCharge = 250.00;
const double michiganCharge = 100.00;
const double erieCharge = 125.00;
const double superiorCharge = 200.00;

// MEAL CHARGES
const double adultDeluxemeal = 15.80;
const double adultStandardmeal = 11.75;
const double childDulxemeal = 9.48;
const double childStandardmeal = 7.05;

// DISCOUNT PERCENTAGES 
const double under500 = .01;
const double between500_1000 = .02;
const double between1000_1500 = .04;
const double over1500 = .05;

// FUCTION CALLS
void getroomname_price (string &name, string &price);
void getAdult_child (int &input1, int &input2);
void getmealcost (int &mealcost); 
void getdiscount (string &discount);
void rowtotal (string &rowtotals);
int getTOTAL();


int main ()
{
	ifstream infile;

	string name;
	string price;
	int input1;
	int input2;
	double discount;
	double fee;
	

    infile.open("reservations.txt");
    infile << fixed << showpoint << setprecision(2);

	while (!infile.eof())
	{
		////////////////// do i do this???
		cout << "enter room selection: ";
		cin >> roomselection;
		cout << "enter how many adults: ";
		cin >> numadults;
		cout << "enter how many children: ";
		cin >> numchildren;
		cout << "enter meal type: ";
		cin >> mealtype;
		//////////////////////////

	}
	return 0;
}

// GET ROOM NAME AND PRICE
void getroomname_price (string &name, string &price)
{
	if (name == "H" || name == "h")
		name = "Huron";
		price = huroncharge;
	if (name == "O" || name == "o")
		name = "Ontario";
		price = ontariocharge;
	if (name == "M" || name == "m")
		name = "Michigan";
		price = michigancharge;
	if (name == "E" || name == "e")
		name = "Erie";
		price = eriecharge;
	else if (name == "S" || name == "s")
		name = "Superior";
		price = superiorcharge;
}

// GET ADULT NUMBER and CHILD NUMBER
void getAdult_child (int &input1, int &input2)
{
	int input1, input2;

	cout << "Enter how many adults: ";
	cin  >> input1;
	cout << "Enter how many children: ";
	cin  >> input2;
}

// GET MEAL TYPE
void mealtype (string &mealname)
{
	if (mealname == "S" || mealname == "s")
		mealname = "Standard";
	else if (mealname == "D" || mealname == "d")
		mealname = "Deluxe";
}

// GET MEAL COST
void getmealcost (int &mealcost)
{
	// for adults
	if (mealname == "S" || mealname == "s")
		adult = input1 * adultStandardmeal;
		child = input2 * childStandardmeal;
		mealcost = adult + child;
	else if (mealname == "D" || mealname == "d")
		adult = input1 * adultDeluxemeal;
		child = input2 * childDeulxemeal;
		mealcost = adult + child;
}


// get discount
void getdiscount (string &discount)
{
	amount = mealcost + price;

	if (amount < 500)
		discount = under500;
		discountoff = amount * under500;
	if (amount >= 500 || amount < 1000)
		discount = between500_1000;
		discountoff = amount * between500_1000;
	if (amount >= 1000 || amount < 1500)
		discount = between1000_1500;
		discountoff = amount * between1000_1500;
	else 
		discount = over1500;
		discountoff = amount * over1500;
}

// GET ROW TOTAL WITH DISCOUNT
void rowtotal (string &rowtotals)
{
	rowtotals = amount - discountoff;
}



// get total of all column by column 
Last edited on
Well, that's not right. In my example I wrote "cin", where I meant "infile". Sorry about that. Apart from that your problem is that you never call any functions. How do you expect them to process data if you never tell them to do so? Also, you use variables without declaring them. There are other problems, but lets fix there first.

When writing code it is a good idea to start with a subset of it. That way you can test it more often and it is easier to fix a shorter program.

For now, write a program with two functions. main() and getroom_price().
main should open and read from a file. At the end of each line call getroom_price and print it's results. Things to fix in main: roomselection, numchildren, numadults and mealtype are all undeclared. Declare them and make sure you give them correct types. Also, why is price a string?
getroom_preice is almost good. Again, why did you decide to make price a string?
Another thing is that you're overusing passing by reference. This isn't really bad, but it might be more comfortable to return values. Do you know how to do that? Try changing getroom_price to "double getroom_price(string& str)".
Topic archived. No new replies allowed.