File Input and Output

Pages: 12
Maybe someone else can explain why line 35 compiled. AFAIK, that's not a valid statement.

I think it would be a lot clearer to you if you were to move the logic for adding a player out of main to a separate function. Likewise, move the logic for displaying players to a separate function. Now you're left with just the logic in main to prompt for a player and to ask if the user wants to add another player. And as mentioned before, if select is "y", continue your loop to prompt for more players.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool add_player (char * player, int score)
{  ofstream file ("golf.dat", ios::app);
    if (! file.is_open()) 
    {  cout << "failed to open golf.dat" << endl;
        return false;
    ]
    file << endl;
    file << "player name: " << player << endl;
    file << "player score: " << score << endl;
    cout << "Successfully added player!" << endl;
    file.close();
    return true;
}

bool display_players ()
{   ifstream readFile;
    string golfScore;
    readFile.open("golf.dat");
    if (! readFile.isOpen())
    {  cout << "Couldn't open golf.dat" << endl;
        return false;
    }
    cout << "Here are the records from the golf.dat file:" << endl;
    while (readFile >> golfScore) 
    {    cout << golfScore << endl;
    }  
    readFile.close();
    return true;
}


The code I've show above opens and closes golf.dat for each addition, but that is a minor inefficiency. It does however guarantee that you don't attempt multiple opens of golf.dat for every player added as was the case with your recursive calls to main.
Last edited on
the if(select=='y') should only run once, try using a while loop so that it runs until you hit anything but 'y', or you can specify a character to hit when you want the loop to stop like 'n'.
Last edited on
AbstractionAnon wrote:
Maybe someone else can explain why line 35 compiled. AFAIK, that's not a valid statement.
Perhaps you need to take a look here:
http://ideone.com/xI4uFD
@LB - I realize void(); and the others you linked compile. What I'm missing is why they are valid statements.
They're valid statements because they are valid expressions. None of it wuld compile in Java because Java does no allow statements that have no effect, but C and C++ do allow statements with no effect.

The first three lines are just literal constants with the same types as the next three lines, which explicitly call the default constructor of those types.

Here's another example:
http://ideone.com/pg27w2

Since you made me research this, return void(); is my new favorite obscure obfuscation in C++. Imagine making a type alias for void and then using it for non-returning functions at random, and using the void-return feature a lot. Great way to confuse people about the flow of the code.
Last edited on
Thanks guys I been working on this project for two weeks and is due tomorrow. I have looked at all the advice you guys gave me, but I still cannot get the program to run correctly. I hate to turn it in and get a failing grade but I just cannot figure it out. I do appreciate all the help and I only have one more exercise to do this semester. I'm hoping and praying that I don't have to take this class over again.
Topic archived. No new replies allowed.
Pages: 12