lines and data

So for my coin sorter program, I have to read lines of data from a file. Most lines have four values (the first two are ints and the last two are strings), however, not all of them have four, some have one or two. The information I have is as follows

10. In the data file Line 0 contains the number of lines of data (first line containing a number in the file), line 1 is the first line containing two sensor measurements and two strings. Line N is the Nth line containing two sensor measurements and two strings. Blank lines are not counted. It is possible that the data file contains more lines of data or less lines of data than is indicated by the number of lines given as the first value in the file.
11. Each time a line of data is read it must be checked for each of the following possible problems. In all cases listed below, NNN, should be replaced with the number of the line containing the problem pointed out in the message. Only one message should be printed for a given line of data even if the line contains more than one error. If two errors occur in the same line print the message for the error that is earliest in the list below.
For certain conditions (below) i have to write certain things

a) if the first int value was not a value

b)if data was not read because there was no more data

c)If only one int reading was present on the present line of data (present line can be any line of data including the last line)

d)if the second int value was not an integer

e)if only the two int readings are present on the line of data

f)if the third value on the line (string - which determines if the coin is bent or not)

g)if only two int and one string values are present (missing one)

h)if the fourth string value does not match one of the three values it is supposed to be.

i)if there is extra data at the end of the line ( ignore it)

j)if the value of the first int value is out of range.


All help would be appreciated.
Hello ineedhelp2,

The first thing you can do is help yourself by posting the input file so everyone will know what you have to work with and will be using the same information. Next if you have written any code post it.

From the information given it is hard to tell if you need to store what is read for later or just process each line.

The use of a string stream could be useful if you know about string streams.

I would start with writing the code to open and check if the file was opened then add the code to read the file. At first I would worry about just reading the first two lines and later add a loop to read the whole file.

Do not worry about how bad your code might be just do something and post it. It can always be fixed.

Hope that helps for now,

Andy
hi,
the input file contains a lot of lines but the problem im facing is this

107 117 usable NoMatch

171 131 usable NoMatch

180 160 usable NoMatch

44

44 68

44 68 usable

103 256 usable BothMatch extra stuff

101 235 somthingElse BothMatch

104 233 usable somethingElse (more lines similar to the first three after this)

As you can see, some lines have the correct values, some dont, and some dont even have enough.

my code so far is pretty simple but only prints until the single 44 (shown above) and prints it as:
44 44 68 44 (as one line) it doesnt print the whole file

my code is as follows

1
2
3
4
5
6
 
		while (inFile >> weightSensor >> diameter >> usableOrBent >> typeMatch)
		{
			
			cout << " " << weightSensor << " " << diameter << " " << usableOrBent << " " << typeMatch << endl;
		}


thanks
The istringstream. Read a line from file. Then read from that line via istringstream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>     // std::cout
#include <sstream>      // std::istringstream
#include <string>       // std::string

int main () {
  // dummy file
  std::istringstream file(
    "7 foo\n"
    "42 bar gaz\n"
    "3.14\n");
  
  std::string line;
  while ( std::getline( file, line ) )
  {
    std::cout << line << '\n';
    std::istringstream iss( line );
    int x {}; std::string y;
    iss >> x >> y; // you need validation here
    std::cout << x << " # " << y << " #\n";
  }
}
Topic archived. No new replies allowed.