highscores from a text file

Write your question here.
Hi, I am having a problem with being able to save a score using a counter to a file and then reopening that file and sorting the previous counter number with a new one and then placing both values in the file. This is my code. I am not using all right now because I am lost with it.
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <vector>
#include <iomanip>
#include <math.h>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;

struct highScores{
string name;
int score;
};

int main(){
int maxScores = 10;
highScores scores[maxScores];
int counter = 0;
ofstream outfile;
ifstream infile;
int num;
int counterr;

do{
cout << "Please enter a number: ";
cin >> num;
if(num == 1)
counter++;
else if(num == 2){
outfile.open("highscores.txt");
if(outfile.fail()){
cout << "file failed to open" << endl;
exit(1);
}
outfile << counter << endl;
}
else if(num == 3){
infile.open("highscores.txt");
if(infile.fail()){
cout << "file failed to open" << endl;
exit(1);
}
infile >> counterr;
cout << counterr << endl;
int comparisons = 0;//counter value to track comparisons
int shifts = 0;//counter value to track shifts

for (int i = 1; i < arrayUsed; i++) {//for loop condition
int element = array2[i];//sets number equal to place in array
int j = i;//sets j equal to i
while ( (j > 0) && (array2[j-1] > element) ) {//while loop condition
swap(array2[j], array2[j-1]);//swaps the values/shifts the values using a swap
comparisons++;
j--;//subtracts each time to see if needed to move number over more
}//end while loop
array2[j] = element;//sets new array value to place in array
shifts++;//counts the amount of shofts
}//end for loop
for(int i = 0; i < arrayUsed; i++){//condition for for loop
cout << array2[i] << " ";//outputs sorted array
}
}
}while(num != 0);



return 0;
}
Put the code you need help with here.
[/code]
You used the code tags wrong lol

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
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <vector>
#include <iomanip>
#include <math.h>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;

struct highScores{
string name;
int score;
};

int main(){
int maxScores = 10;
highScores scores[maxScores];
int counter = 0;
ofstream outfile;
ifstream infile;
int num;
int counterr;

do{
cout << "Please enter a number: ";
cin >> num;
if(num == 1)
counter++;
else if(num == 2){
outfile.open("highscores.txt");
if(outfile.fail()){
cout << "file failed to open" << endl;
exit(1);
}
outfile << counter << endl;
}
else if(num == 3){
infile.open("highscores.txt");
if(infile.fail()){
cout << "file failed to open" << endl;
exit(1);
}
infile >> counterr;
cout << counterr << endl;
int comparisons = 0;//counter value to track comparisons
int shifts = 0;//counter value to track shifts

for (int i = 1; i < arrayUsed; i++) {//for loop condition
int element = array2[i];//sets number equal to place in array
int j = i;//sets j equal to i
while ( (j > 0) && (array2[j-1] > element) ) {//while loop condition
swap(array2[j], array2[j-1]);//swaps the values/shifts the values using a swap
comparisons++;
j--;//subtracts each time to see if needed to move number over more
}//end while loop
array2[j] = element;//sets new array value to place in array
shifts++;//counts the amount of shofts
}//end for loop
for(int i = 0; i < arrayUsed; i++){//condition for for loop
cout << array2[i] << " ";//outputs sorted array
}
}
}while(num != 0);



return 0;
}
What do you mean?
Topic archived. No new replies allowed.