Dump truck/tire pressure

I am having trouble with the error part of my program.

You will be developing C++ program to report the Truck Tire readings which was collected manually.The first thing you have to do 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.

I did this part but im stuck on this next part....

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.

Data file:
65
77
68
80
59
78
79
89
58
69


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

using namespace std;
int main() //Starting the body of the code
{

	

		int I = 1, pressure;
		ifstream myfile("TruckTireTemp.txt");

			while (myfile >> pressure) {
				cout << "Tire # " << I << "\tTire pressure = " << pressure << "\tPsi" << endl;
				I++;
				
			}
			{
				if (I <= 9)
				{					
					cout << "\nError, file has less than 10 wheels. Check your input data file and re-run the code\n";	
			
				}
				else if (I >= 11)
				{
					
					cout << "\nError, file has more than 10 wheels. Check your input data file and re-run the code\n";
				}

				else
				{
					
				}	
			}		
		cout << endl << endl;
		system("pause");
		return 0;
}


When there are 9 numbers in the data file it does not print an error
And when there is 10 numbers it does print in error

I am confused. Please help.
Initialise I to 0 not 1.

int I = 0, pressure;
Hello w0rd,

The variable I should be initialized to 1 to work with output at line 15. The problem is at lines 20 and 25 with the if statements. Line 20 works better as I < 10 and line 25 should be I > 11. Check the value of I after the while loop has ended and you will fine that it equals 11.

Hope that helps,

Andy
Thank you both for your responses. Handy Andy, when I put I <10, it prints out no error for 9 and 10. I don't get why it doesn't print the error if the data file contains 9 numbers. However it works when I put I <= 10. Which to me doesn't make any sense.

Again thank you

-w0rd
Hello w0rd,

My fault. It is called off by one. When the file has 10 entries the variable 'I' has a value of 11 when all input is read and processed. So if the file has 9 entries the value if 'I' would be 10. Therefor the check would be I < 11 or I <= 10 either will work.

Sorry, changing the size of the input file is the one part I did not test for earlier.

Hope that helps,

Andy
Andy,

Thank you for explaining that to me. Makes sense now.

Thank you,

-w0rd
Topic archived. No new replies allowed.