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.
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.
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:
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;
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.
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.
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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");
}