conditions applied on while statement - Read Data Files

Hello everybody,
this is my first time posting a question here, so forgive me if it's not in a way it should be. I try to ask my question in simplest and easiest way to understand.
my problem is about one of my school's assignments which I thought it would be so easy to code it, but apparently it's not!
I'm almost done with its coding, but I'm running into a problem at the end of the execution.

here's the question:

"You will be developing C++ program to report the Truck Tire readings which was collected manually ( as seen in the picture below). The first thing you have to is to prepare an input data file. Name of the file must be TruckTireTemp.txt . Make a notepad file ( .txt) in your computer for the Truck Tire temperature readings and enter (simply type in) the following tire temp readings from Dump Truck #1 ( 10 Wheels). And, Save this file where you have the source code ( the default location on your computer is under MS Visual Studio / Projects..) . Then close this input data file. And then, develop to program code to produce following table on the screen."
It's too easy to code this program till this point, however, once two conditions are applied, things get a bit tricky.

conditions are as follows:
"If the input data file contains 9 or less tire data then “ Error, file has less than 10 wheels. Check your input data file and re-run the code” . Then, the program stops here.
If the input data file contains 11 or more tire data then “ Error, file has more than 10 wheels. Check your input data file and re-run the code” . Then, the program stops here.
You do not need to establish a loop here. This is a one shot deal. "

I did apply these conditions through my loop, but when I did so, the program was no longer able to list all the data from the "txt" text file in order; it would keep displaying same number over and over. (However, it was still in the loop; it would show "tire #" from 1 - 10, but the pressure stays the same number)
but when I changed the number of data in the data file (add or take a data out) it worked perfectly. It would break the loop and stop the execution, so it doesn't display any data from data file and would exit the program.
So, I got that part right, but I'm stuck figuring out how to fix the first error.

Thank you for taking time to read my question,
Any help would be greatly appreciated,

here's my code:

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

using namespace std;

int main()
{

	int i = 1;
	const double iMAX = 10;
	ifstream myfile("TruckTireTemp.txt");      // Declare the input data file

	if (myfile.fail())
	{
		cerr << " Your File Didn't Work !" << endl;
		exit(EXIT_FAILURE);
	}

	int pressure;
	int count = 0;

	while (myfile >> pressure)
	{
		count++;
	}
	for (i = 1; i <= iMAX; i++)
	{
		if (count < 10) {
			cout << " ** Error, file has less than 10 wheels. Check your input data file and re-run the code ** " << endl;
			break;
		}
		else if (count > 10) {
			cout << " ** Error, file has more than 10 wheels. Check your input data file and re-run the code **" << endl;
			break;
		}
		if (count = 10)
			cout << "   Tire # " << i << "     Pressure = " << pressure << "    Psi" << endl;

	}


	cout << "==================================================================" << endl;
	system("pause");
	return 0;

}
Since you read all of the pressures in the file you will need to clear() the stream error flags and reset the file pointer back to the end of the file.

1
2
3
4
5
6
7
8
9
10
11
...
   while (myfile >> pressure)
   {
      count++;
   }
   // Check your count here, return to the operating system if not correct.
...
   myfile.clear(); // Clear the eof() error flag caused by the loop above.
   myfile.seekg(0);  // Reset the file pointer to the beginning of the file.
...


By the way your if() statements should probably be outside the for loop. And remember that the operator= is the assignment operator not the comparison operator==. You will also need to re-read the file to get your tire pressures for display.


Thank you Jlb for your help,

I figured it out;
Here it is:
(code)
while (myfile >> pressure)
{
count++;
}


{

if (count < 10) {
cout << " ** Error, file has less than 10 wheels. Check your input data file and re-run the code ** " << endl;

}
else if (count > 10) {
cout << " ** Error, file has more than 10 wheels. Check your input data file and re-run the code **" << endl;

}

else {
myfile.clear();
myfile.seekg(0);
while (myfile >> pressure)
{
if (count = 10)
cout << " Tire # " << i << " Pressure = " << pressure << " Psi" << endl;
i++;
}
}
}
(/code)
This: if (count = 10) is probably wrong since you're using the assignment operator= instead of the comparison operator== (actually you probably don't even need this if() statement).

When you use the assignment operator= the "comparison" is always true.

jlb,
I see what you're saying,
I fixed it,
thank you for help.
Topic archived. No new replies allowed.