Problem reading input file

so i want the program to read five value from a file and if a value doesn't exist it should display an error. I think the problem starts with line 36 where it says "if (inFS >> fileNum1 ==0)" as c++ wont read it.

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
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <iomanip>
using namespace std;

int main() {
	ifstream inFS;
	ofstream outFile;		// output file with invalid values
	string outFileName = "saleschart.txt";	//name fore output file


	long long int fileNum1 = 0;
	long long int fileNum2 = 0;
	long long int fileNum3 = 0;
	long long int fileNum4 = 0;
	long long int fileNum5 = 0;
	string filename = "";
	cin >> filename;

	outFile.open(outFileName.c_str()); //open file
	if (!outFile) { //if file cant open
		cout << "File \"" << outFileName << "\" could not be opened" << endl;
		return 1;
	}

	inFS.open(filename.c_str());
	if (!inFS.is_open())
	{
		cout << "File \"" << filename << "\" could not be opened\n";

		return -1;
	}

	if (inFS >> fileNum1 ==0)
	{
		cout << "Your input file must contain the sales values for five stores" << endl;

		return -1;
	}
	if (inFS >> fileNum2 == 0) {
		cout << "Your input file must contain the sales values for five stores" << endl;

		return -1;
	}
	if (inFS >> fileNum3 == 0) {
		cout << "Your input file must contain the sales values for five stores" << endl;


		return -1;
	}
	if (inFS >> fileNum4 == 0) {
		cout << "Your input file must contain the sales values for five stores" << endl;


		return -1;
	}
	if (inFS >> fileNum5 == 0) {
		cout << "Your input file must contain the sales values for five stores" << endl;


		return -1;
	}


	if (fileNum1 <= 0 || fileNum2 <= 0 || fileNum3 <= 0 || fileNum4 <= 0 || fileNum5 <= 0) {
		cout << "One or more of the sales values are negative" << endl;
	}

	inFS.close();

	outFile << "SALES BAR CHART" << endl;
	outFile << "(Each X equals 1,000 dollars)" << endl;


	outFile << "Store 1: ";
	for (int i = 0; i<fileNum1 / 1000 || i == 200; i++)
	{
		outFile << "X";
	}
	cout << endl;
	outFile << "\nStore 2: ";
	for (int i = 0; i<fileNum2 / 1000 || i == 200; i++)
	{
		outFile << "X";
	}
	cout << endl;
	outFile << "\nStore 3: ";
	for (int i = 0; i<fileNum3 / 1000 || i == 200; i++)
	{
		outFile << "X";
	}
	cout << endl;
	outFile << "\nStore 4: ";
	for (int i = 0; i<fileNum4 / 1000 || i == 200; i++)
	{
		outFile << "X";
	}
	cout << endl;
	outFile << "\nStore 5: ";
	for (int i = 0; i<fileNum5 / 1000 || i == 200; i++)
	{
		outFile << "X";
	}

	outFile.close();
	cout << endl;
	return 0;
}
> as c++ wont read it.
you've got an error message, paste the error message. (the 178 lines if you must)


the >> operator returns the istream object, that way you may chain the command
input >> a >> b >> c;
so when doing inFS >> fileNum1 ==0 you are doing inFS == 0 and that is not defined.

Not sure what you're trying to do. If you want to check if the reading was successful, then simply do inFS >> fileNum1
the error message is
"main.cpp:44:23: error: no match for 'operator==' (operand types are 'std::basic_istream::__istream_type {aka std::basic_istream}' and 'int')"

what im trying do is that when the the file is read and the first value is missing it should print out the cout text. it should do this for all five values.

1
2
3
4
if (not (inFS >> fileNum1 >> fileNum2 >> /*...*/ >> fileNum5)){
   cout << "Your input file must contain the sales values for five stores" << endl;
   return -1;
}


you may want to use arrays to avoid all that code duplication.
Last edited on
That worked. Thanks
Topic archived. No new replies allowed.