-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.cpp
More file actions
87 lines (74 loc) · 2.09 KB
/
temp.cpp
File metadata and controls
87 lines (74 loc) · 2.09 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Cricket {
public:
string playerName;
int age;
int score;
void readData() {
cout << "Enter player name: ";
cin.ignore();
getline(cin, playerName);
cout << "Enter age: ";
cin >> age;
cout << "Enter score: ";
cin >> score;
}
void displayData() {
cout << "Name: " << playerName << endl;
cout << "Age: " << age << endl;
cout << "Score: " << score << endl;
cout << "---------------------" << endl;
}
};
int main() {
int n;
cout << "Enter number of players: ";
cin >> n;
Cricket* players = new Cricket[n];
// Read data
for (int i = 0; i < n; i++) {
cout << "\nEnter details for player " << i + 1 << ":" << endl;
players[i].readData();
}
// Write to text file (SIMPLE WAY)
ofstream outFile("cricket.txt");
outFile << n << endl; // Store count first
for (int i = 0; i < n; i++) {
outFile << players[i].playerName << endl;
outFile << players[i].age << endl;
outFile << players[i].score << endl;
}
outFile.close();
// Read from text file (SIMPLE WAY)
ifstream inFile("cricket.txt");
int count;
inFile >> count;
inFile.ignore(); // Clear newline
Cricket* filePlayers = new Cricket[count];
for (int i = 0; i < count; i++) {
getline(inFile, filePlayers[i].playerName);
inFile >> filePlayers[i].age;
inFile >> filePlayers[i].score;
inFile.ignore(); // Clear newline after numbers
}
inFile.close();
// Find and display highest scorers
int highest = 0;
for (int i = 0; i < count; i++) {
if (filePlayers[i].score > highest) {
highest = filePlayers[i].score;
}
}
cout << "\nHighest Scorers (" << highest << "):" << endl;
for (int i = 0; i < count; i++) {
if (filePlayers[i].score == highest) {
filePlayers[i].displayData();
}
}
delete[] players;
delete[] filePlayers;
return 0;
}