altering output based on newline check

I am doing an assignment that requires me to read lines of data, of various data types, from a text file which represents phone lines on an account. My text file, for example, reads:

111111 M 111-111-1111 100 200 0.5
222222 H 222-222-2222 300 300 1.0
...

I am using the following line of code to read this information from the text file:

inData >> accountNumber >> planType >> phoneNumber >> minutesUsed >> textsUsed >> dataUsed;

Then, to test that the program read the information correctly, simply outputting by:

cout << accountNumber << " " << planType << " " << phoneNumber << " " << minutesUsed << " " << textsUsed << " " << dataUsed << endl;

This works perfectly fine, the program outputs exactly as the information is shown in the text file. My problem is there are situations where there are multiple lines on the same account. For example:

333333 M 313-313-3131 100 200 1.0 323-323-3232 200 300 1.5

We have been instructed to use the peek function to check for the \n character to determine if the end of the line has been reached or if there is another line on that account. I am having trouble using the peek function and altering my read coding to, instead of just going to the next line and repeating, to output the first phone line, then read the second phone line on the same text line (minus the account number because its the same account) and outputting that as well. Basically, I am trying to get the program to output exactly as the information is shown on the text file. Can someone help steer me in the right direction? I apologize if all of this is unclear and will clarify as best as I could if needed.
Here's a quick overview of a strategy:

1) read account beginning (number, plan)
2) read one phone number data
3) peek at next char
4a) if it is '\n': go to 1, as we are to read a new account's data
4b) if it is not '\n': go to 2, as we are to read another number for the same account

Does this help?
Peeking is not a good idea, there may be whitespace after the data but before the newline. It is better to read in a whole line at a time and then process the line until it is empty.
Zhuge: That is essentially the idea but unfortunately I can't seem to figure out how to effectively perform 4a and 4b.

L B: Well the issue with that is we are to assume we do not know how much data is in the file. We are to make a test text file to use but my instructor will use his own test file when running the program, so it cannot be adapted to my text file exclusively. Therefore, I'm not sure I would process a whole line at a time, assigning values to variables when I do not know how much data there will be per line. My first line in the text file could only have 1 phone number but his may have 2 or 3. So it seems to me he wants us to constantly be checking to see if there is additional information per line before beginning the new line.
Zhuge: That is essentially the idea but unfortunately I can't seem to figure out how to effectively perform 4a and 4b.


1
2
3
4
5
6
7
8
9
    while ( inData >> accountNumber )    // 1
    {
        // output here.
        do {
            inData >> planType >> phoneNumber >> minutesUsed >> textsUsed >> dataUsed; // 2
            // output here.
            int ch = inData.peek() ; // 3
        } while ( ch != EOF && ch != '\n' ) ; // 4a/4b
    }



L B:Well the issue with that is we are to assume we do not know how much data is in the file.

That is not an issue at all with the approach LB recommended. By contrast the above code might look like the following if his was the approach you took:

1
2
3
4
5
6
7
8
9
10
    std::string line ;
    while ( std::getline(inData, line) )
    {
        std::istringstream is(line) ;
        is >> accountNumber ;
        // output here

        while ( is >> planType >> phoneNumber >> minutesUsed >> textsUsed >> dataUsed )
            ; // output here. 
    }


Not only does the code look simpler, it is more robust. But, you should definitely follow your instructor's requirements for your classwork.
Topic archived. No new replies allowed.