Setting conditions to a line count (input data file)

Hi everyone,
this is my first time posting here so i apologize in advance if i am posting something wrong. I am creating a simple program for class that takes the information in from a data file and then outputs it on the screen. i have figured this part out relatively easily, but one of the requirements of the project is to have condition for the number of lines (<=9,>=11). i am completely stuck and cannot figure out where to go from here. posted below is the assignment and what i have come up with so far. thank you for your time.

ASSIGNMENT:
You will be developing C++ program to report the Truck Tire readings which was collected manually. 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.

DATA FROM TRUCKTIRETEMP.TXT: 65,77,68,80,59,78,79,89,58,69

ASSIGNMENT (part i'm struggling with):

WARNING MESSAGE:
-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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	int x = 1;
	string line; 
	ifstream myfile("TruckTireTemp.txt");
	if (myfile.is_open())
	{
		while (!myfile.eof()) 
		{
		
			cout << "Tire #  " << x << "     Pressure=" << line << "   Psi" << endl;
			x++;
		}
		myfile.close();
	}
	else cout << "Ooops!!Unable to open file";
	system("pause");
	return 0;




Thank you for taking the time to help, i know its probably something extremely simple, but it has me tearing my hair out at this point.
since you are already using a counter "x", couldn't you just check x's value prior to your pause statement.

1
2
3
4
5
	if(x < 10)
	  cout << "File has less than 10 wheels. Check your input data file." << endl;

	if(x > 10)
	  cout << "File has more than 10 wheels. Check your input data file." << endl;
Last edited on
Line 16: You never actually read from your file into line. Therefore line remains an empty string. Tire pressures are integers, so you're going to want to read into an int, not a string. You're going to have to deal with the interspersed commas.

You're also not checking if the file has too many or too few tires.

Line 13: Do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin or istream operation
  }
closed account (37oyvCM9)
forgive me for the bad code!
Last edited on
closed account (37oyvCM9)
this code doesnt work lol
@VRGaming

Line 12: You've ignored my advice regarding looping on !eof()

Line 15: That's going to read the entire line (all 10 tires) into line. The cout at line 16 is going to display all 10 pressures at once.

Line 17: numberOfTyres is not going to be incremented correctly because only two iterations of the while loop will occur.

Lines 35-36: Unnecessary.
closed account (37oyvCM9)
yeah i posted before I read mate...you learn something new every day...as they say :)
sorry oracoin...thanks abstraction :)

oh yeah and I broke the file into dif lines thought he had just posted that way to save space(double oops).
Last edited on
Topic archived. No new replies allowed.