ifstream in_gradebook;
        ofstream out_gradebook;
        in_gradebook.open("gradeBook.txt");
        out_gradebook.open("gradeBook_edit.txt");        

        while (???) //while int is being read
        {
            in_gradebook >> score;
            out_gradebook << score << " ";
            sum = sum + score;
        }
        average = sum / 10.0;
        out_gradebook << average << endl;

Here is a small snippet of the code I have trouble with.

My professor told me there's a way to end the loop when the input from in_gradebook is no longer type int, which I have no idea how to do. Basically, my loop goes on forever because the variable score gets the string FIRST_NAME_2 of the second line. What is a good way to end the loop when there are no more numbers to input?

The input file goes something like this:
FIRST_NAME LAST_NAME 1 2 3 4 5 6 7 8 9
FIRST_NAME_2 LAST_NAME_2 1 2 3 4 5 6 7

As you can see I am only a beginner :$, I apologize beforehand if I do not understand replies that may be beyond my level.

Recommended Answers

All 5 Replies

use while(1) loop and inside the loop read one character at a time.
Break from the loop on getting something other that 0-9.
Handle the space explicitly.
If the numbers are multi-digit numbers you will have to use temp buffer to store the number till u get a space.

commented: Terrible suggestion -4

Thank you for replying.

I understand what you are trying to say, but how do I start the loop at the numbers?
If I do in_gradebook.get(input_character), then I would have to get all the characters of the first and last name too right? Or does .get start where the last >> left off? Will check now, thank you.

I have found the solution:twisted:! I didn't follow your advice completely (couldn't figure out what temporary buffer is), but it definitely works now. I used...

stringstream score_list;

        while (true)
        {
            in_gradebook.get(input_char);
            if (input_char == '\n')
            {
                while (score_list >> score)
                {
                out_gradebook << score << " ";
                sum = sum + score;
                }
                break;
            }
            score_list.put(input_char);
        }
        average = sum / 10.0;
        out_gradebook << average << endl;

Instead of checking if the char is a 0-9 I thought it's just simpler to put all the char in a string and stop when it finds '\n'. Then treat the string as an object and grab inputs from it using >>. It took me the whole day, but thanks for putting me on the right path good sir. Also, I'm sorry, I just realized I should have been posting in the C++ section.


*EDIT*
Just a final little question to be clear, after I use '>>' to grab the FIRST_NAME and LAST_NAME from the input file, does the '.get' start getting input characters after LAST_NAME has been grabbed? This has always puzzled me, whether '.get' keeps count of its position with relation to previous uses of '>>'. It would seem to make sense that it does, since the characters from FIRST_NAME and LAST_NAME would mess stuff up, but I would like to make sure of this.

while(true)
        {
        in_gradebook >> firstName >> lastName;
        if ( in_gradebook.eof() )
            break;   
        out_gradebook << firstName << " "<< lastName << " ";
        
        /* Code from above snippet */
        }

Realistic input sample:
Price Betty 40 50 60 70 60 50 40 30 60 90 55
Good John 60 70 80 90 50 60 90 90 100 90 78

Sorry for the downvote. I'll get it cleaned up. It was the double post that threw me.

The solution you came up with is far superior to the suggestion.

*EDIT*
Just a final little question to be clear, after I use '>>' to grab the FIRST_NAME and LAST_NAME from the input file, does the '.get' start getting input characters after LAST_NAME has been grabbed? This has always puzzled me, whether '.get' keeps count of its position with relation to previous uses of '>>'. It would seem to make sense that it does, since the characters from FIRST_NAME and LAST_NAME would mess stuff up, but I would like to make sure of this.

Output the value from the .get and see what you have. If you ever have a question like this, your best and fastest solution is test it yourself with a small test program.

I see, thank you and I will keep your advice in mind good sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.