File Input and Output

Pages: 12
hey guys,I'm trying to figure this out but I'm stuck somewhere. Can someone help me out.

The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to design two programs.
(1) A program that will read each player’s name and golf score as keyboard input, and then save these as records in a file named golf.dat. (Each record will have a field for the player’s name and a field for the player’s score.)
(2) A program that reads the records from the golf.dat file and displays them.


Last edited on
There are a huge amount of C++ syntactic errors. Without trying to be rude, you seem to need to pay more attention to your classes. Besides that, you should state the precise errors you are having.
6. `def' does not name a type

10:19 [Warning] character constant too long for its type
I'm still a beginner and still learning, but I went ahead and gave adding players to a text file a shot. I do see a lot of errors in your code though. It may not be the most efficient / best way of doing things but I figured I'd help out:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>

using namespace std;

//adding players to file 'golf.txt' in program directory

int main()
{
    string select;
    char player_name[20];
    int player_score;

    cout << "Enter the name of the player: ";
    cin.getline(player_name, 20);
    cout << "Enter their score: ";
    cin >> player_score;

    ofstream file ("golf.txt", ios::app);

    if (file.is_open()) {
        file << endl << endl;
        file << "Player name: " << player_name << endl;
        file << "Player score: " << player_score << endl;
        cout << "Successfully added player!" << endl;
        cout << "Would you like to add another player? (y/n): ";
        cin >> select;
        if (select == "y")
        {
            main();
        }
        else {
            cout << "Thanks! Closing file - press ENTER to exit";
            file.close();
        }

    }
    else
        cout << "Error opening file!";

    cin.ignore();
    cin.get();
}


Should be easy enough to read through the file considering the information available through this (and many others) website.

If anyone sees any bad habbits i'm using, please do let me know :)
Last edited on
Line 30: Never ever call main recursively.

edit: From the standard:
3.6.1.3 "The function main shall not be used within a program."

5.2.2.9 "Recursive calls are permitted, except to the function named main"


Last edited on
Ah, didn't know that.

What's another way to loop back to the beginning? Would using a separate function work?
Would using a separate function work?

Yes. You can move the contents of main to another function, then it's legal for that function to call itself.

However, that's not a good idea in this program. Note that at line 19 you open the file for output. If you call a new function recursively that also attempts to open golf.txt for output you're probably going to get an error because the file is already open in the outer function. A simple loop is a much better solution than using recursion for this.
unknown thanks for your help, I modified the program you helped me with and it works fine. However I can not figure out the second part of the program.

A program that reads the records from the golf.dat file and displays them.


#include <iostream>
#include <fstream>

using namespace std;

//adding players to file 'golf.txt' in program directory

int main()
{
string select;
char playerName[20];
int playerScore;
ofstream file ("golf.dat", ios::app);
cout << "Enter the name of the player: ";
cin.getline(playerName, 20);
cout << "Enter their score: ";
cin >> playerScore;


if (file.is_open()) {
file << endl;
file << "player name: " << playerName << endl;
file << "player score: " << playerScore << endl;
cout << "Successfully added player!" << endl;


cout << "Would you like to add another player? (y/n): ";
cin >> select;
// go to start of file and read each line to a cout

if (select == "y")
{
main();
}
else {
cout << "Thanks! Closing file - press ENTER to exit";
file.close();
}

}
else
cout << "Error opening file!";

cin.ignore();
cin.get();
system("PAUSE");
}

any help that you can provide would be greatly appreciated.
You still have the same problem of calling main recursively that was pointed out earlier.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
The code is fine. The problem I'm having is with line 29 how do I (go to start of file and read each line to a cout). This is what I am supposed to be doing. I just can't figure out the second part of it, and how to add more than one gofer and score.

(1) A program that will read each player’s name and golf score as keyboard input, and then save these as records in a file named golf.dat. (Each record will have a field for the player’s name and a field for the player’s score.)
(2) A program that reads the records from the golf.dat file and displays them.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>

using namespace std;

//adding players to file 'golf.txt' in program directory

int main()
{
    string select;
    char playerName[20];
    int playerScore;
    ofstream file ("golf.dat", ios::app);
    cout << "Enter the name of the player: ";
    cin.getline(playerName, 20);
    cout << "Enter their score: ";
    cin >> playerScore;


    if (file.is_open()) {
        file << endl;
        file << "player name: " << playerName << endl;
        file << "player score: " << playerScore << endl;
        cout << "Successfully added player!" << endl;
        
        
        cout << "Would you like to add another player? (y/n): ";
        cin >> select;
        // go to start of file and read each line to a cout
        
        if (select == "y")
        {
            main();
        }
        else {
            cout << "Thanks! Closing file - press ENTER to exit";
            file.close();
        }

    }
    else
        cout << "Error opening file!";

    cin.ignore();
    cin.get();
     system("PAUSE");
}
Last edited on
No, the code is not fine. As pointed out above, you're violating the C++ stadard by calling main recursively. The behavior of doing that is undefined.
I understand what you are saying, but I really don't know how to perform the actions you are talking about. I am very new at C++ programming and I am doing my best.
Use a looping structure instead of calling the main function. For one, recursion is a bad way to loop (leave me alone, LISP programmers!), and two, it's actually not allowed to call the main function at all - it's not even a real function, it just looks like one.
I need help guys, I have been working on this program for two days and I know you guys keep telling me the main should be changed, but every time I change it I get a bunch of error messages. This is what I have so far. Can anybody point me in the right direction. And tell me what to put where I have the main.

(1) A program that will read each player’s name and golf score as keyboard input, and then save these as records in a file named golf.dat. (Each record will have a field for the player’s name and a field for the player’s score.)
(2) A program that reads the records from the golf.dat file and displays them.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//adding players to file 'golf.txt' in program directory

int main()
{
   
   string select;
    char playerName[20];
    int playerScore;
    ofstream file ("golf.dat", ios::app);
    cout << "Enter the name of the player: ";
    cin.getline(playerName, 20);
    cout << "Enter their score: ";
    cin >> playerScore;


    if (file.is_open()) {
        file << endl;
        file << "player name: " << playerName << endl;
        file << "player score: " << playerScore << endl;
        cout << "Successfully added player!" << endl;
        file.close();
        
        cout << "Would you like to add another player? (y/n): ";
        cin >> select;
        
        if (select == "y")
        {
            main();
        }
        else {
		ifstream readFile; 
		string golfScore;
		readFile.open("golf.dat");
		cout << "Here are the records from the golf.dat file:" << endl;
		while (readFile >> golfScore) 
		{ 
			cout << golfScore << endl;
		}  
		readFile.close();
            cout << "Thanks! Closing file - press ENTER to exit";
            
        }

    }
    else
        cout << "Error opening file!";
     system("PAUSE");
}
Last edited on
I need help guys!
If you don't take the suggestions that have already been offered, you're not likely to get further help, at least from me.
I understand what you're saying. but every time I take the main functions out and replace it with something else it gives me a lot of error messages.
do you have any suggestions of how to replace the main functions. and how to add more than one golfer and scores
Work through the error messages. The compiler is trying to tell you what it thinks is wrong. Those messages can be cryptic, but usually point to the line and column (or slightly past) where the compiler encountered a problem. Often times a single error can generate multiple errors from the compiler. Focus on the first error. Try and correct that, then move on to the next.

If you get stuck, post your code and the error messages you're getting.
I changed the main function to vold and it will compile and run, but I need to add more than one golfer. I'm going to enter my code again. Please look over it and tell me where I'm making my mistakes.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//adding players to file 'golf.txt' in program directory

int main()
{
   
    string select;
    char playerName[20];
    int playerScore;
    ofstream file ("golf.dat", ios::app);
    cout << "Enter the name of the player: ";
    cin.getline(playerName, 20);
    cout << "Enter their score: ";
    cin >> playerScore;


    if (file.is_open()) {
        file << endl;
        file << "player name: " << playerName << endl;
        file << "player score: " << playerScore << endl;
        cout << "Successfully added player!" << endl;
        file.close();
        
        cout << "Would you like to add another player? (y/n): ";
        cin >> select;
        // go to start of file and read each line to a cout
        
        if (select == "y")
        {
            void();
        }
        else {
		ifstream readFile;
		// that is read from the file. 
		string golfScore;
		readFile.open("golf.dat");
		cout << "Here are the records from the golf.dat file:" << endl;
		while (readFile >> golfScore) 
		{ 
			cout << golfScore << endl;
		}  
		readFile.close();
            cout << "Thanks! Closing file - press ENTER to exit";
            
        }

    }
    else
        cout << "Error opening file!";
     system("PAUSE");
}

Pages: 12