Hello all,

I am trying to create a program which keeps track of football stats for individual players. The program is setup to read a list of players names, numbers, and positions from a .txt file and then put them into a strctured array, print them to the screen, and then to a .txt file on my computer. I am getting the program to read in from the text file and print to the screen but i am getting an problem with the output stream when writing to the new file. I have worked through the code several times, and compared it to other similar programs writen to do the same thing and cannot find my error. The output file gets created, there just seems to be an error when sending information to the file in my function (printFile). the code compiles but when ran hangs up in the file stream. I am using a function to do this but i also tried it brute force with a for loop and still get the same problem. any offer of assistance would be much appreciated. Thanks! (Code Below)

// FootballDatabase.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int PLAYERS = 5;

struct player
{
    string name;
    string position;
    int number;
    int touchdowns;
    int catches;
    double rushing;
    double passing;
    double receiving;
};

void initialize(ifstream& infile, player list[], int listSize);
void printFile(ofstream& outfile, player list[], int listSize);
void printScreen( player list[], int listSize);
//void updateStats(string playerName);
//void searchPlayers(player list[], string playerName, int listSize);
//void menu();
//void getCommand();
//void completetask(char option, player list[], int index);

int main()
{
    ifstream infile;
    ofstream outfile;

    string inFilename;
    string outFilename;

    player playerList[PLAYERS];

    cout << "Please enter input filename: ";
    cin >> inFilename;
    cout << endl;

    infile.open(inFilename.c_str());

    if(!infile)
    {
        cout << "cannot open input file" << endl;

        return 1;
    }

    initialize(infile, playerList, PLAYERS);

    printScreen(playerList, PLAYERS);

    infile.close();
    infile.clear();

    cout << "Enter name of output file: ";
    cin >> outFilename;
    cout << endl;

    outfile.open(outFilename.c_str());

    printFile(outfile, playerList, PLAYERS);

    outfile.close();

    system("pause");

    return 0;
}

void initialize(ifstream& infile, player list[], int listSize)
{
    int i;
    for (i = 0; i <= listSize; i++)
    {
        // get names, number, and positions
        infile >> list[i].name >> list[i].number >> list[i].position; 

        // Initialize stats
        list[i].touchdowns = 0;
        list[i].catches = 0;
        list[i].passing = 0.0;
        list[i].receiving = 0.0;
        list[i].rushing = 0.0;
    }
} // end initialize

void printScreen(player list[], int listSize)
{
    int i;

    for(i = 0; i < listSize; i++)
    {
        cout << list[i].name << "  " ;
        cout << setw(5) << list[i].number;
        cout << setw(5) << list[i].position;
        cout << setw(5) << list[i].touchdowns;
        cout << setw(5) << list[i].catches;
        cout << setw(5) << list[i].passing;
        cout << setw(5) << list[i].receiving;
        cout << setw(5) << list[i].rushing << endl;
    }
} // end print to screen

void printFile(ofstream& outfile, player list[], int listSize)
{
    int i;

    for(i = 0; i < listSize; i++)
    {
        outfile << list[i].name << "  " ;
        outfile << setw(5) << list[i].number;
        outfile << setw(5) << list[i].position;
        outfile << setw(5) << list[i].touchdowns;
        outfile << setw(5) << list[i].catches;
        outfile << setw(5) << list[i].passing;
        outfile << setw(5) << list[i].receiving;
        outfile << setw(5) << list[i].rushing << endl;
    }
} // end print to File

Recommended Answers

All 4 Replies

How do you know the open file worked? You never checked for errors.

Walt,

Thanks that is a good point, I will add that to my code and check. I dohowever know that the file is being created because I can find it in the programs directory, it is just empty.

Extend my question to the next logical level...

commented: what next level would you look at, it is passing the error check. I dont understand why it would not work... +0

Don't downvote a post to ask your next question. Post it properly.

what next level would you look at, it is passing the error check. I dont understand why it would not work...

You opened the file and didn't check for an error. Logical extension:
What's the next thing you do that might not work? Did you check for an error there?

Try adding output statements to make sure your program gets where it should, and variable you need have the proper values.

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.