Help with file processing

Total newbie here trying my hand a t program that requires me to uses loops and file streaming. The exact is here below:
A high-end coffee shop has just hired you as their senior programmer. They have tasked you to design and implement two C++ applications allowing employees to take orders from its customers and to determine weekly sales.

Application #1: Create an application allowing employees to take orders and then tally up the total costs from its customers.

The coffee shop offers the following items on its menu:
• Coffee
• Tea
• Soda
• Juice
• Manager Special (store manager offers a drink of their choice to customers)

The application must be able to:
• Accept one or more drinks in a single order; use a sentinel value to terminate an order
• Once an order has been placed, tally up the total cost of the order

Application #2: Create another application to process an input file, weekly_sales.txt, and tally up its weekly sales total. The data file consists of 7 entries representing the sales from last week.

If the input file does not exist, prompt user to try again.


I've been running into problems left and right due to me trying to write all of this on my own and never really reaching out for help. My obstacle now is that when I tried to do the second part of my project my input file won't open. I thought I might see if there is anyone on here who could point out to what I'm doing wrong at this point, and help me figure how to do file processing correctly. Here's my code for part 1:

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




#include<iostream>
#include<iomanip>
#include<cmath>
#include <fstream>
using namespace std;

int main() {

	ofstream outputFile;
	char order;
	double cost = 0;
	double total = 0;
	int beverageTotal = 0;
	int coffeeAmount = 0;
	int juiceAmount = 0;
	int sodaAmount = 0;
	int teaAmount = 0;
	int specialAmount = 0;
	int coffeeTotal = 0;
	int juiceTotal = 0;
	int sodaTotal = 0;
	int teaTotal = 0;
	int specialTotal = 0;

	outputFile.open("weekly_sales.txt");

	if (outputFile.is_open())
	{


           do
	   {
		cout << "Welcome to my Coffe Shop! Our offer";
		cout << "\n C- Coffee ($1.29)";
		cout << "\n J- Juice ($1.50)";
		cout << "\n S- Soda ($1.00)";
		cout << "\n T- Tea ($1.39)";
		cout << "\n M- Manger Special ($2.99)";
		cout << "\n X- Done with placing order";
		cout << "What drink would you like to select. Select C, J, S, T, M, or X";
			

                        cout << "  " << endl;
			cin >> order;
			outputFile << order << endl;

			cout << fixed << showpoint << setprecision(2);



	switch (order)
	{


	case 'c':case 'C':
	cost += 1.29;
	beverageTotal++;
	coffeeAmount++;
	cout << "Thank you. You have order coffee as your drink.";
	outputFile << 'c' << endl;
	outputFile << 'C' << endl;
	cout << "  " << endl;
	break;


	case 'j': case 'J':
	cost += 1.50;
	beverageTotal++;
	juiceAmount++;
	cout << "Thank you. You have order juice as your drink.";
	outputFile << 'j' << endl;
	outputFile << 'J' << endl;
	cout << "  " << endl;
	break;


	case 's':case 'S':
	cost += 1.00;
	beverageTotal++;
	sodaAmount++;
	cout << "Thank you. You have order soda as your drink.";
	outputFile << 's' << endl;
	outputFile << 'S' << endl;
	cout << "  " << endl;
	break;

	case 't': case 'T':
	cost += 1.39;
	beverageTotal++;
	teaAmount++;
	cout << "Thank you. You have order tea as your drink.";
	outputFile << 't' << endl;
	outputFile << 'T' << endl;
	cout << "  " << endl;
	break;

	case 'm':case 'M':
	cost += 2.99;
	beverageTotal++;
	specialAmount++;
	cout << "Thank you. You have order the Manager Special as your drink.";
	outputFile << 'm' << endl;
	outputFile << 'M' << endl;
	cout << "  " << endl;
	break;





	case 'x': case 'X':
	cout << "You have ordered: " << beverageTotal << "  drinks";
        cout << "\nHere is your order: ";
	break;


	default: cout << "Invalid selection please place your order again.";
	}


	} while ((order != 'x') && (order != 'X'));



		cout << "  " << endl;
		cout << "  " << endl;
		cout << "  " << endl;


cout << left << setw(20) << "Beverage"; cout << right << setw(10) << "Quanity" << endl;
	{
  if (coffeeAmount != 0) cout << left << setw(20) << "Coffee" << right << setw(10) << (coffeeTotal += coffeeAmount);
  if (juiceAmount != 0) cout << left << setw(20) << "\nJuice" << right << setw(10) << (juiceTotal += juiceAmount);
  if (teaAmount != 0) cout << left << setw(20) << "\nTea" << right << setw(10) << (teaTotal += teaAmount);
  if (sodaAmount != 0) cout << left << setw(20) << "\nSoda" << right << setw(10) << (sodaTotal += sodaAmount);
  if (specialAmount != 0) cout << left << setw(20) << "\nManager Special" << right << setw(10) << (specialTotal += specialAmount);
		}


		cout << "" << "\nTotal: " << (total += cost);
		cout << "  " << endl;

		outputFile << total << endl;

		outputFile.close();
		cout << "\nData written to weekly_sales.txt\n";
	}

	else cout << "Data was not saved to weekly_sales.txt\n";
		
	

	outputFile.close();
	cout << "\nData written to weekly_sales.txt\n";

	system("pause");
	return 0;
}





And here is my code for part 2:




#include<iostream>
#include<cmath>
#include <fstream>
#include <string>
using namespace std;

int main()
{

ifstream inputFile;
double total;
double weeklySales;
char order;


inputFile.open("weekly_sales.txt");

if (inputFile.is_open())
{
	inputFile >> order;
	inputFile >> total;
	inputFile.close();

weeklySales = (total * 7);
cout << "About to process weekly sales\n";
cout << "There are "<<order<<" entries and the total was " << weeklySales;

	}
	else cout << "Error opneing file.\n";


	
inputFile.close();
system("pause");
return 0;



}


Appreciate the help guys!




Last edited on
http://www.cplusplus.com/doc/tutorial/files/

I don't see a problem with your code, maybe the file is not in the same folder or maybe it's not named what you think it is.

Open a dos prompt, go to the folder and type DIR.
Last edited on
Thanks for the advice SamuelAdams.

Do I do this when I open up my project in Visual Studio?
So I did what you told me to do, but it didn't seem to fix anything.

Any other suggestions?
It wasn't meant to fix anything, but you need to make sure the file is in the folder with the compiled program, else you have to tell the program the entire path, such as c:\folder location\myfiles\filename.txt.
Topic archived. No new replies allowed.