greguk Posted July 19, 2008 Posted July 19, 2008 Hi, I'm trying to revise my "knowledge" in C++ and program probably worked is doing something stupid. Program should read from file First name then last name and finally mark that person got, then move to another person and so on. Next step program asks user to enter his score and compare this score with scores which read from file (Mark < Score) and display and write to file everyone who got higher mark than user. Instead of that program writes to file details of first person from file either she got higher score than user or not (doesn't follow program condition). Here is a code: // lab5_q1.cpp : Defines the entry point for the console application. // fstream.cpp // #include #include #include using namespace std; void main() { ifstream InFile; ofstream OutFile; char FirstName[20], LastName[20]; int Score; int Mark; cout << "Please enter your mark: "; cin >> Mark; InFile.open("Records.txt"); InFile >> FirstName >> LastName >> Score; OutFile.open("HighScores.txt"); OutFile << FirstName << LastName << Score; while (!InFile.eof()) { InFile >> FirstName >> LastName >> Score; if (Mark < Score) { cout << FirstName << " " << LastName << " " << Score << endl; OutFile << FirstName << " " << LastName << " " << Score << endl; } } InFile.close(); OutFile.close(); system("pause"); } Note: if you want to test this code just create file records.txt with example names i.e John Doe 65 Jane Smith 42 Carl Young 59 Sarah Jones 77 Kevin Harris 68 Chris Billington 73 I bet it's simple but i'm groping. Thanks
dhicks Posted July 20, 2008 Posted July 20, 2008 I'm trying to revise my "knowledge" in C++ It's been a while since I did any C++, I should do this too... Instead of that program writes to file details of first person from file either she got higher score than user or not That's because that's exactly what you tell it to do: InFile.open("Records.txt"); InFile >> FirstName >> LastName >> Score; OutFile.open("HighScores.txt"); OutFile << FirstName << LastName << Score; Try just: InFile.open("Records.txt"); OutFile.open("HighScores.txt"); // lab5_q1.cpp Lab 5 question 1? Are you sure this isn't homework? #include Note that this is Windows-specific. I bet it's simple but i'm groping. Maybe that's your problem - try keeping both hands on the keyboard next time, aids concentration. -- David Hicks
greguk Posted July 20, 2008 Author Posted July 20, 2008 Works.. Thank You David.. Seems like i didn't pay attention somewhere Greg
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now