|
C-/C++-Quelltext
|
1
2
3
4
5
6
|
//Wegen Faulheit Global deklariert
struct HighscoreEntry//Struktur für die Highscorespeicherung
{
int Points;
string Name;
};
|
?? Wieso wegen Faulheit ?
Ich glaube du verwechselst da was.
Das ist keine Globale Variable
![;)](wcf/images/smilies/wink.png.pagespeed.ce.L9LRa_F2a5.png)
Globale Variable wäre z.B. so:
HighscoreEntry globalVariable;
Die Struct gehört da hin, und nicht unbedingt in die main.
EDIT: Mit dem Code funzts bei mir:
|
C-/C++-Quelltext
|
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
//Wegen Faulheit Global deklariert
struct HighscoreEntry//Struktur für die Highscorespeicherung
{
int Points;
char Name[256];
};
std::list<HighscoreEntry*> Rank;
std::list<HighscoreEntry*>::iterator ir;
ifstream Input;
ofstream Output;
void setDefaultHigh();//Funktion, wenn kein Highscore da ist einen zu machen
void readHigh();//Funktion, die den Highscore ausliest
int main (int argc, char *argv[])
{
int Selection;
int i;
std::string TempName;
int TempPoints;
do
{
Input.open("Highscore.hsc",ios::binary);
if(Input==NULL)//Wenn kein Highscore dann einen schreiben
{
Input.close();
setDefaultHigh();
}
else//Wenn ein Highscore dann auslesen
{
Input.close();
readHigh();
}
Selection=2;
while(Selection!=1&&Selection==2)
{
cout<<"*****Menue*****\n";
cout<<"(1) Spiel starten\n";
cout<<"(2) Highscore einblenden\n";
cout<<"(3) Spiel beenden\n";
cin>>Selection;
cout<<endl;
if (Selection==1)
{
cin.ignore();
cout<<"Ihren Namen bitte: ";
cin>>TempName;
cin.ignore();
}
if (Selection==2)
{
i=1;
for(ir=Rank.begin();ir!=Rank.end();++ir)
{
std::string Name ((*ir)->Name);
cout<<i<<". "<<Name.c_str()<<" "<<(*ir)->Points<<endl;
i++;
}
}
if(Selection==3)
{
for (ir=Rank.begin();ir!=Rank.end();++ir)
{
delete (*ir);
(*ir)=NULL;
}
Rank.clear();
return (0);
}
}
TempPoints=516;//Hierhin wo Highscore eingefügt wird
i=0;
for(ir=Rank.begin();ir!=Rank.end()&&i==0;++ir)
{
HighscoreEntry* TempHigh = new HighscoreEntry;
if(TempPoints >((*ir)->Points))
{
TempHigh = new HighscoreEntry;
sprintf (TempHigh->Name, "%s", TempName.c_str());
TempHigh->Points=TempPoints;
Rank.insert(ir,TempHigh);
Rank.pop_back();
i=1;
}
}
Input.open("Highscore.hsc",ios::binary);
if(Input==NULL)
{
Input.close();
setDefaultHigh();
}
Input.close();
readHigh();
Output.open("Highscore.hsc",ios::binary);
for(ir=Rank.begin();ir!=Rank.end();++ir)
{
Output.write((char*)(*ir),sizeof(*ir));
Output.close();
}
}while(Selection!=3);
for (ir=Rank.begin();ir!=Rank.end();++ir)
{
delete (*ir);
(*ir)=NULL;
}
Rank.clear();
return (0);
}
void setDefaultHigh()
{
int i;
HighscoreEntry* TempHigh = new HighscoreEntry;
for(i=10;i!=0;i--)
{
TempHigh = new HighscoreEntry;
sprintf (TempHigh->Name, "Default Name %i", i);
TempHigh->Points=(i);
Rank.push_back (TempHigh);
}
Output.open("Highscore.hsc",ios::binary);
for(ir=Rank.begin();ir!=Rank.end();++ir)
{
Output.write((char*)&((*ir)->Points), sizeof (int));
Output.write(&((*ir)->Name[0]),sizeof(char)*256);
}
Output.close();
}
void readHigh()
{
int i;
Input.open("Highscore.hsc",ios::binary);
if (!Input)exit (-33);
for(i=10;i>0;i--)
{
HighscoreEntry* TempHigh = new HighscoreEntry;
TempHigh = new HighscoreEntry;
Input.read((char*)&(TempHigh->Points),sizeof (int));
Input.read((&TempHigh->Name[0]), sizeof (char) * 256);
Rank.push_back (TempHigh);
}
Input.close();
}
|
Hab halt das ganze SDL-Zeug weggemacht, damit ichs bei mir ausprobieren kann.
Musste mal Abgleichen. WICHTIG: HighscoreEntry benutzt nun keine std::strings mehr, sondern char - Arrays fester grösse.
Ausserdem nicht vergessen Highscore.hsc zu löschen, da es das ganze jetzt anderst in die Datei schreibt. (ist nun auch 2.4KB statt 40 Bytes gross
![;)](wcf/images/smilies/wink.png.pagespeed.ce.L9LRa_F2a5.png)
)