1

I have a file that includes this information:

Bev Powers
3
76 81 73
Chris Buroughs
5
88 90 79 81 84
Brent Mylus
2
79 81

I have a count controlled loop that will do the first 3 lines and use the information correctly but I am struggling with a loop that will reuse the the loop until all the information is displayed from the file regardless of how many golfers with matches are on the file. I am asking for pointers in the right direction, any assistance would be appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

ifstream inScores;
string filename;

string name;

int loopCount, matchScore;

int count = 1;
float mean = 0;
float adder = 0;

int main()
{
    cout << endl << "Enter the golfer's filename: ";
    getline(cin,filename);
    cout << endl;

    inScores.open(filename.c_str());
      if(!inScores)
        {
            cout << "** " << filename << " does not exist. Please ";
            cout << "check the spelling and rerun ";
            cout << "the program with an existing golfer file. ** " << endl << endl;

            return 1;
        }


    getline(inScores,name);
    inScores >> loopCount;

    cout << name << " has " << loopCount << " matches with scores of" << endl << endl;

    inScores >> matchScore;
    while (count <= loopCount)
    {
            cout << "Match " << count << ": " << matchScore << endl;
            adder = adder + matchScore;
            adder = adder + matchScore;
            inScores >> matchScore;
            count++;
    }

    cout << endl;
    int(mean) = .5 + (adder / loopCount);
    cout << "The mean score is " << mean << endl << endl;

inScores.close();
return 0;
}
  • 3
    Your first step is to get rid of `>>`. Mixing `std::getline` with `>>` [produces unexpected results](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). Read three lines of text using `std::getline`. Construct a `std::istringstream` for the second and the third line, then parse them using `std::istringstream`. – Sam Varshavchik Dec 07 '16 at 03:26
  • 1
    Read the first two lines, use a for loop for the next N values, and wrap that all in your while loop. Probably best to put the reading of the N values in a function too, it'll be prettier. – Retired Ninja Dec 07 '16 at 03:26

1 Answers1

0

As stated using loops will be necessary to get what you want. Also since the getline and extraction return false if they fail you can use them for the test in the loop:

ifstream inScores;
string filename;

string name;

int loopCount , matchScore;

int count = 1;
float mean = 0;
float adder = 0;

int main()
{
    cout << endl << "Enter the golfer's filename: ";
    getline( cin , filename );
    cout << '\n';

    inScores.open( filename );
    if ( !inScores )
    {
        cout << "** " << filename << " does not exist. Please ";
        cout << "check the spelling and rerun ";
        cout << "the program with an existing golfer file. ** " << "\n\n";

        return 1;
    }
    while ( getline( inScores , name ) )
    {
        if ( inScores >> loopCount )
        {
            cout << name << " has " << loopCount << " matches with scores of" << "\n\n";
        }
        else
        {
            cout << "File read error";
            return 1;
        }

        for ( int count = 1; count <= loopCount; count++ )
        {
            if ( inScores >> matchScore )
            {
                cout << "Match " << count << ": " << matchScore << '\n';
                adder = adder + matchScore;

            }
            else
            {
                cout << "File read error";
                return 1;
            }
        }

    }
    cout << '\n';
    int( mean ) = .5 + ( adder / loopCount );
    cout << "The mean score is " << mean << "\n\n";

    inScores.close();
    return 0;
}
tinstaafl
  • 6,610
  • 2
  • 12
  • 22