I have written a code that reads and input file, does some silly little calculations with some functions. It's a schoolwork assignment, but its done :) With one small kink- I need to create an output file. Well, actually I can make the out file just fine. But I have 2 functions that cout data that I need to have in my outfile. I am a bit sketchy on the syntax and I could use some assistance on this last part, thank you so much!!!

  • Sphiros

This is the text needed for the inFile:

NO NAME POS CLASS HEIGHT WEIGHT Hometown/High School/Last College
60 Josh Mann OL SO 6-4 300 Virginia Beach, Va./Ocean Lakes
64 Ricky Segers K/P FR 5-11 185 Glen Allen, Va./Henrico
70 Brandon Carr OL RS_SR 6-2 305 Chesapeake, Va./Western Branch/Fork Union Military Academy
53 Calvert Cook LB FR 6-0 250 Norfolk, Va./Booker T. Washington
51 Michael Colbert DE RS_SR 6-1 230 Fayetteville, N.C./E.E. Smith
22 T.J. Cowart CB RS_JR 5-9 190 Virginia Beach, Va./Ocean Lakes
1 Jakwail Bailey WR SO 5-11 185 Haddonfield, N.J./Paul VI
25 Andre Simmons S JR 6-0 205 Lorton, Va./South County/Vanderbilt
34 Johnel Anderson RB FR 5-8 180 Sicklerville, N.J./Paul VI

And this is the code that I am using, the part about the outFile is at the end of main():

    /*
       "Kellen_B_Prj4.cpp
        "Programmer: Kellen Berry"
        "Date: 11/11/2012"
        "CRN 14086"
    */

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    #include <cstring>
    #include <string>


    using namespace std;

    struct players_rec
    {
        string   fname;
        int      player_num;
        string   lname;
        string   POS;
        string   year;
        string   where_from;
        char     dash, where_from_ltr;
        int      feet, inches, Weight;
    };

    void print ( players_rec  list[] , int size);
    void getData(ifstream& inFile, players_rec list[], int& howmany);
    void print_height (players_rec players[], int howmany, int when_ft, int when_inch);
    void print_year (players_rec list[], int howmany, string& year);
    void print_weight (players_rec list[], int howmany,int maxweight,int minweight);
    void remove(players_rec a[], int& n, int i);
    void selectionSort( players_rec list[], int length);
    void addOne(players_rec list[],  int& length, int where, players_rec what);
    int seqSearch(const players_rec list[], int listLength, int search_ID);
    void openOut(ostream& outFile, players_rec list[], int howmany);
    void program_info();

    const int SIZE = 50;

    int main()
    {
    string  fname, lname, POS, year, where_from;
    char    dash, where_from_ltr;
    int     feet, inches, Weight, player_num,when_ft, when_inch, maxweight, minweight;
    int     count = 0;//number of names in the file
    int     looper = 0;
    int     cycle = 0; //number of red shirts
    int     loc;   // first letter in the where_from variable


      struct players_rec players [SIZE];

      int howmany = 0;
      int sub;
      players_rec what;

    string filename; // name of the input file
    ifstream inFile; //input file stream variable
    ofstream outFile;

    cout << "Enter the name of the input file:  " ;
    cin >> filename;

    inFile.open(filename.c_str ()  );

    if (!inFile)
    {
         cout << "bummer file name \n\n";

         return 1;
    }
    else{
        cout << filename << " was opened" << endl<<endl;
    }
    outFile.clear();
    outFile.open("Kellen_B_Prj4.out");
    if (outFile.fail())
    {
        cout << "The output file could not be created" << endl;

        return(1);
    }

    // ignore the first line
    inFile.ignore ( 200,'\n');
    while ( !inFile.eof())
    {
          howmany++;
            // assigns all the values to the variables based on the file read i
    getData(inFile, players, howmany);
        print (players, howmany);
        cout << endl <<endl;


        // part c

        cout << "Enter how tall in feet. ";
        cin >> when_ft;
        cout << "\nEnter how many inches over the foot (i.e. if 5\'3\" you would type 3. ";
        cin >> when_inch;

        cout <<"Are any players taller than " << when_ft <<"-"<< when_inch << "?"<<endl;
        print_height (players, howmany, when_ft, when_inch);

        cout << endl <<endl;

        cout <<"What class year are you interested in?" << endl;
        cin >> year;

        cout <<"How many players are in the class of " << year << "?"<<endl;
        print_year (players, howmany, year);


        cout << endl <<endl;

        cout << "Enter the max weight of the players you wish to view. ";
        cin >> maxweight;
        cout << "Enter the minimum weight of the players you wish to view. ";
        cin >> minweight;

        cout <<"Are any players who are between " << maxweight <<"lbs and "<< minweight<< "lbs?"<<endl;
        print_weight (players, howmany, maxweight, minweight);

        cout << endl <<endl;

        //part d
        cout <<"Enter a location for the new player ";
        cin >> sub;

        if (sub>= 0 && sub < howmany   )
        {
                cout << "Enter the new player\'s number ";
                cin >> what.player_num;
                cout << "Enter the new player\'s first name ";
                cin >> what.fname;
                cout << "Enter the new player\'s last name ";
                cin >> what.lname;
                cout << "Enter the new player\'s POS ";
                cin >> what.POS;
                cout << "Enter the new player\'s class ";
                cin >> what.year;
                cout << "Enter the feet in the new player\'s height ";
                cin >> what.feet;
                cout << "Enter a dash ";
                cin >> what.dash;
                cout << "Enter the inches in the new player\'s height ";
                cin >> what.inches;
                cout << "Enter the new player\'s weight ";
                cin >> what.Weight;
                cout << "Enter the new player\'s homecity with no spaces ";
                cin >> what.where_from;

        addOne(players, howmany, sub, what);

        cout <<"\n\nAfter adding "<<endl;
        print (players, howmany);

        }
        else
            cout << "illegal subscript\n\n";
        // part e
        // Prompt the user for an players number.
        // remove that id
        // missing id - message


        cout << "What player should be removed? Enter their player number " ;
        cin >> player_num;

        sub = seqSearch(players, howmany, player_num);

        if ( sub == -1)
            cout << player_num << " is not found " <<endl;
        else
        {
            remove(players, howmany,sub );
            cout << "after removing " << player_num << endl;
            print (players, howmany);
            cout << endl <<endl;
        }
selectionSort( players, howmany);

        //this is the part of the program I am having trouble with- how do I make it print out the contents of the functions?

     outFile << fixed << setprecision(0) << "This is my out file XD" <<"\nAfter sorting:" << endl;

        print (players, howmany);// I want this output in the outfile
        program_info();// and I need this in the outfile as well
        cout << endl <<endl;
        outFile.close();

    }
    inFile.close();
    cout << "The data has been written to the file.\n\n";
    program_info();
    return 0;
    }
    void program_info()
        {
        cout << endl<<endl;
        cout<< "Kellen_B_Prj4.cpp"<<endl;
        cout << "Programmer: Kellen Berry" <<endl;
        cout << "Date: 11/11/2012" <<endl;
        cout << "CRN 14086" <<endl <<endl;
        }
    // searchs list for the animal whose player_num is search_player_num
    // if found, returns the subscript of that animal
    // if not found, returns -1
    // Input employees from the input file
        void getData(ifstream& inFile, players_rec list[], int& howmany)
       {
           players_rec players ;
           howmany = 0;

    inFile  >> players.player_num
                >> players.fname
                >> players.lname
                >> players.POS
                >> players.year
                >> players.feet
                >> players.dash
                >> players.inches
                >> players.Weight
                >> players.where_from_ltr;
        getline ( inFile, players.where_from ); // created the where_fromstring correctly
        players.where_from = players.where_from_ltr + players.where_from;
           while (inFile )
          {
            list [howmany] = players;
            howmany++;
            inFile  >> players.player_num
                >> players.fname
                >> players.lname
                >> players.POS
                >> players.year
                >> players.feet
                >> players.dash
                >> players.inches
                >> players.Weight
                >> players.where_from_ltr;
        getline ( inFile, players.where_from ); // created the where_fromstring correctly
        players.where_from = players.where_from_ltr + players.where_from;
          }

          inFile.close () ;
       }
    int seqSearch(const players_rec list[], int listLength, int search_player_num)
    {
        int loc;
        bool found = false;

        loc = 0;

        while (loc < listLength && !found)
            if (list[loc].player_num == search_player_num)
                found = true;
            else
                  loc++;
        if (found)
            return loc;
        else
            return -1;
    }
    // inserts item what into location where.
    //assumes that the suscript where is valid(where >= 0 && where <= length)
    //increments length
    void addOne(players_rec list[],  int& length, int where, players_rec what)
    {

        for (int i = length -1 ; i >= where; i--)
                list [i+1] = list [i];

         list [where] = what;

        length++;

    }
    // sorts a list of players_rec in ascending order by last name
    void selectionSort( players_rec list[], int length)
    {
        int index;
        int smallestIndex;
        int minIndex;
        players_rec temp;

        for (index = 0; index < length - 1; index++)
        {
                //Step a
            smallestIndex = index;

            for (minIndex = index + 1; minIndex < length; minIndex++)
                if (list[minIndex].lname < list[smallestIndex].lname)
                    smallestIndex = minIndex;

                //Step b
            temp = list[smallestIndex];
            list[smallestIndex] = list[index];
            list[index] = temp;
        }
    }
    // remove the item from position i
    // update n, the number of components in the array
    //assumes that i is a valid subscript
    void remove(players_rec a[], int& n, int i)
    {
         for (int j=i+1; j<n; j++)
             a[j-1] = a[j];

         --n;
    }
     // prints out players acquired prior to when
     void print_height (players_rec list[], int howmany, int when_ft, int when_inch)
     {
        int loc;
        bool found = false;
        for (loc = 0 ; loc < howmany;loc++)
        if (list[loc].feet >= when_ft && list[loc].inches >= when_inch  )
        {
           cout << setw(5) << left << "NO "<< list[loc].player_num<<  endl << "======== "<< endl
                << setw(15) << left << "First Name "<< list[loc].fname << endl << "====================== "<< endl
                << setw(15) << left << "Last Name"<< list [loc].lname << endl << "====================== "<< endl
                << setw(8) << left << "Field Position"<< list [loc].POS << endl << "======= " << endl
                << setw(3) << left << "Class"<< list [loc].year << endl << "======= " << endl
                << setw(1) << left  << list [loc].feet << "\'"
                << setw(1) << left << list [loc].inches << "\"" << endl << "======= " << endl
                << setw(8) << left << "Weight" << list [loc].Weight<< endl << "=========== " << endl
                << setw(12) << left << "Home Town"<< list [loc].where_from<< endl<< "==================================== " << endl<<endl;
           found = true;
        }
        if (!found)
            cout << "\n\tno players who match are taller than" << when_ft<<"-" << when_inch <<endl;
     }
     // Prints the players who are in a specific class then tells you how many players are in that class
     void print_year (players_rec list[], int howmany, string& year)
     {
        int loc;
        int looper =0;
        bool found = false;
        for (loc = 0 ; loc < howmany;loc++)
        if (list[loc].year == year)
        {
           cout << setw(5) << left << "NO "<< list[loc].player_num<<  endl << "==== "<< endl
                << setw(15) << left << "First Name "<< list[loc].fname << endl << "====================== "<< endl
                << setw(15) << left << "Last Name"<< list [loc].lname << endl << "====================== "<< endl
                << setw(8) << left << "Field Position"<< list [loc].POS << endl << "======= " << endl
                << setw(3) << left << "Class"<< list [loc].year << endl << "======= " << endl
                << setw(1) << left  << list [loc].feet << "\'"
                << setw(1) << left << list [loc].inches << "\"" << endl << "======= " << endl
                << setw(8) << left << "Weight" << list [loc].Weight<< endl << "======= " << endl
                << setw(12) << left << "Home Town"<< list [loc].where_from<< endl<< "==================================== " << endl<<endl;
                looper++;
            cout << "There are "<< looper << " players in the class "<< year <<endl;
           found = true;
        }
        if (!found)
            cout << "There are no players in the class "<< year <<endl;
     }
     //Prints a list of all players who fall between the weights input by the user.
     void print_weight (players_rec list[], int howmany,int maxweight,int minweight)
     {
        int loc;
        bool found = false;
        for (loc = 0 ; loc < howmany;loc++)
        if (list[loc].Weight <= maxweight && list[loc].Weight >= minweight )
        {
           cout << setw(5) << left << "NO "<< list[loc].player_num<<  endl << "==== "<< endl
                << setw(15) << left << "First Name "<< list[loc].fname << endl << "====================== "<< endl
                << setw(15) << left << "Last Name"<< list [loc].lname << endl << "====================== "<< endl
                << setw(8) << left << "Field Position"<< list [loc].POS << endl << "======= " << endl
                << setw(3) << left << "Class"<< list [loc].year << endl << "======= " << endl
                << setw(1) << left  << list [loc].feet << "\'"
                << setw(1) << left << list [loc].inches << "\"" << endl << "======= " << endl
                << setw(8) << left << "Weight" << list [loc].Weight<< endl << "======= " << endl
                << setw(12) << left << "Home Town"<< list [loc].where_from<< endl<< "==================================== " << endl<<endl;
           found = true;
        }
        if (!found)
            cout << "\n\tno players who are between " << maxweight << "lbs and " << minweight<< "lbs?"<<endl;
     }

    // prints out the list of players_rec
    void print ( players_rec  list[] , int size)
    {
        int loc;

        for (loc = 0 ; loc < size;loc++)
        {
           cout << setw(5) << left << "NO "<< list[loc].player_num<<  endl << "==== "<< endl
                << setw(15) << left << "First Name "<< list[loc].fname << endl << "====================== "<< endl
                << setw(15) << left << "Last Name (ID)"<< list [loc].lname << endl << "====================== "<< endl
                << setw(8) << left << "Field Position"<< list [loc].POS << endl << "======= " << endl
                << setw(3) << left << "Class"<< list [loc].year << endl << "======= " << endl
                << setw(1) << left  << list [loc].feet << "\'"
                << setw(2) << left << list [loc].inches << "\"" << endl << "======= " << endl
                << setw(8) << left << "Weight" << list [loc].Weight<< endl << "======= " << endl
                << setw(12) << left << "Home Town"<< list [loc].where_from<< endl<< "==================================== " << endl<<endl;
        }
    }

    }

You mean you want to put a function in the output file? Why? Can't be done. You don't put executable code in the output file. Instead, you write a function that reads the output file and do whatever you need to do with the data.

What you can do is add an additional parameter to the printing functions, one that takes an output stream by reference (ostream&). And you can also make cout the default stream, as so:

void print_height (players_rec players[], int howmany, int when_ft, int when_inch, ostream& dest = cout);
void print_year (players_rec list[], int howmany, string& year, ostream& dest = cout);
void print_weight (players_rec list[], int howmany,int maxweight,int minweight, ostream& dest = cout);
void program_info(ostream& dest = cout);

And then you implement them in terms of that given output stream, as so for example:

void program_info(ostream& dest)
{
    dest << endl << endl;
    dest << "Kellen_B_Prj4.cpp" << endl;
    dest << "Programmer: Kellen Berry" << endl;
    dest << "Date: 11/11/2012" << endl;
    dest << "CRN 14086" << endl << endl;
}

So, by default, all those print functions will do the same as they do now (print to cout), but if you pass them an output stream, they will output to that instead.

Thank you mike 2000 17! I was able to take that and get it to do just what I needed! On to the next code!

This article has been dead for over six months. Start a new discussion instead.