I'm writing a game in c++ with 2d opengl graphics and i'm on a Mac OS X 10.5 computer using Xcode 3.0
I want to add highscore functionality to my game but i cant get it to write the names of the people to the file
for saving data
heres most if not all the relevent code to the problem could some one please help:
from chase.h
CODE
. . .
void initgame();
char *itoa(int n);
void printboard();
void thirdroundtests();
//Globals:
ifstream ifile;
ofstream ofile;
const unsigned int ARRAY_COLUMNS = 11, ARRAY_ROWS = 11;
char board[ARRAY_ROWS][ARRAY_COLUMNS], character = 'o';
string names[10], names2[10], new_highscore = "", final_name = "";
int board2[ARRAY_ROWS][ARRAY_COLUMNS][2], pos[10], elapsedtime = 0, restarttime = 0, wintime = 0, wini = 0,
highscores[10], highscores2[10], numhighscores = 0, place, counter_name = 0;
bool round1comp = false, round2comp = false, gun = false, choice = '\0', instructions = false, gameover = false,
restart = false, win = false, menu = true, game = false, show_highscores = false, accepting_input = false, highscore = false, prevhigh = false;
button button1( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "New Game") )/2, 180, GLUT_BITMAP_HELVETICA_18, "New Game"),
button2( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Instructions") )/2, 220, GLUT_BITMAP_HELVETICA_18, "Instructions"),
button3( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "High Scores") )/2, 260, GLUT_BITMAP_HELVETICA_18, "High Scores"),
button4( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Quit") )/2, 300, GLUT_BITMAP_HELVETICA_18, "Quit"),
button5( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Back") )/2, 410, GLUT_BITMAP_HELVETICA_18, "Back"),
button6( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Menu") )/2, 320, GLUT_BITMAP_HELVETICA_18, "Menu"),
button7( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Restart") )/2, 360, GLUT_BITMAP_HELVETICA_18, "Restart"),
button8( (639 - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Quit") )/2, 400, GLUT_BITMAP_HELVETICA_18, "Quit"),
button9( 450 + ( (640 - 450) - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Menu") )/2, 320, GLUT_BITMAP_HELVETICA_18, "Menu"),
button10( 450 + ( (640 - 450) - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Restart") )/2, 360, GLUT_BITMAP_HELVETICA_18, "Restart"),
button11( 450 + ( (640 - 450) - bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Quit") )/2, 400, GLUT_BITMAP_HELVETICA_18, "Quit");
//:Globals
char* itoa (int n)
{
int i=0,j;
char* s;
char* u;
s= (char*) malloc(17);
u= (char*) malloc(17);
do{
s[i++]=(char)( n%10+48 );
n-=n%10;
}
while((n/=10)>0);
for (j=0;j<i;j++)
u[i-1-j]=s[j];
u[j]='\0';
return u;
}
. . .
The problem code is in the following segment
CODE
ofile.open("High Scores.txt");
for(int i = 0; i < numhighscores; i++)
{
if(i > 0)
{
ofile << endl; // <-- this does
}
ofile << highscores[i] << endl; // <-- this does
ofile << names[i]; // <-- this doesn't work
}
ofile.close();
from display() in main.cpp
CODE
. . .
else if(show_highscores == true)
{
int x[10], x1[10], y[10];
glBegin(GL_LINE_LOOP);
glVertex2f( 200, 11);
glVertex2f( 460, 11);
glVertex2f( 460, 400 );
glVertex2f( 200, 400 );
glEnd();
for(int i = 0, j = 41; i < 10; i++, j += 35)
{
y[i] = j;
}
for(int i = 0; i < 10; i++)
{
renderBitmapString( 205, y[i], 0, GLUT_BITMAP_HELVETICA_18, itoa(i+1));
x[i] = bitmapStringWidth( GLUT_BITMAP_HELVETICA_18, itoa(i+1));
renderBitmapString( (205 + x[i]), y[i], 0, GLUT_BITMAP_HELVETICA_18, ".) ");
x[i] += bitmapStringWidth( GLUT_BITMAP_HELVETICA_18, ".) ");
}
for(int i = 0; i < numhighscores; i++)
{
renderBitmapString( 205 + x[i] + 10, y[i], 0, GLUT_BITMAP_HELVETICA_18, names[i]);
x1[i] = bitmapStringWidth( GLUT_BITMAP_HELVETICA_18, names[i]);
renderBitmapString( 205 + x[i] + x1[i] + 50, y[i], 0, GLUT_BITMAP_HELVETICA_18, itoa(highscores[i]));
}
if( prevhigh == true)
{
button5.set_text(GLUT_BITMAP_HELVETICA_18, "Menu");
prevhigh = false;
}
button5.draw();
button5.set_text(GLUT_BITMAP_HELVETICA_18, "Back");
}
else if(win == true)
{
place = numhighscores;
game = false;
wini++;
if(wini == 1)
{
wintime = elapsedtime - restarttime;
}
renderBitmapString(10 + (639 - bitmapStringWidth(GLUT_BITMAP_TIMES_ROMAN_24, "YOU WIN!!!") )/2, 200, 0, GLUT_BITMAP_TIMES_ROMAN_24, "YOU WIN!!!");
int x = bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in");
renderBitmapString( (639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2, 230, 0, GLUT_BITMAP_HELVETICA_18, "You finished in");
renderBitmapString( ((639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2 + x + 10), 230, 0, GLUT_BITMAP_HELVETICA_18, itoa(wintime));
int x1 = bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime));
renderBitmapString( ((639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2 + x + x1 + 20), 230, 0, GLUT_BITMAP_HELVETICA_18, "Seconds");
button6.draw();
button7.draw();
button8.draw();
for(int i = numhighscores; i >= 0; i--)
{
if( wintime < highscores[i])
{
place--;
}
}
if(place != 10)
{
if(numhighscores < 10)
{
numhighscores++;
}
highscore = true;
win = false;
accepting_input = true;
}
}
else if(highscore == true)
{
glBegin(GL_LINE_LOOP);
glVertex2f( 200, 50);
glVertex2f( 460, 50);
glVertex2f( 460, 90 );
glVertex2f( 200, 90 );
glEnd();
renderBitmapString(250, 31, 0, GLUT_BITMAP_HELVETICA_18,"New Highscore");
renderBitmapString(210, 119, 0, GLUT_BITMAP_HELVETICA_18,"(type Your name and hit return)");
renderBitmapString(145, 75, 0, GLUT_BITMAP_HELVETICA_18,"Name: ");
renderBitmapString(210, 75, 0, GLUT_BITMAP_HELVETICA_18, new_highscore);
renderBitmapString(10 + (639 - bitmapStringWidth(GLUT_BITMAP_TIMES_ROMAN_24, "YOU WIN!!!") )/2, 200, 0, GLUT_BITMAP_TIMES_ROMAN_24, "YOU WIN!!!");
int x = bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in");
renderBitmapString( (639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2, 230, 0, GLUT_BITMAP_HELVETICA_18, "You finished in");
renderBitmapString( ((639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2 + x + 10), 230, 0, GLUT_BITMAP_HELVETICA_18, itoa(wintime));
int x1 = bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime));
renderBitmapString( ((639 - (bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "You finished in") + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, itoa(wintime)) + bitmapStringWidth(GLUT_BITMAP_HELVETICA_18, "Seconds") ) )/2 + x + x1 + 20), 230, 0, GLUT_BITMAP_HELVETICA_18, "Seconds");
button5.set_text(GLUT_BITMAP_HELVETICA_18,"Menu");
button5.draw();
button5.set_text(GLUT_BITMAP_HELVETICA_18,"Back");
place = place;
if(accepting_input == false)
{
place = place;
highscores[place] = wintime;
place = place;
names[place] = final_name;
for(int i = (place+1); i < numhighscores; i++)
{
highscores[i] = highscores2[i-1];
names[i] = names2[i-1];
}
ofile.open("High Scores.txt");
for(int i = 0; i < numhighscores; i++)
{
if(i > 0)
{
ofile << endl;
}
ofile << highscores[i] << endl;
ofile << names[i];
}
ofile.close();
ifile.open("High Scores.txt");
string input_string = "";
numhighscores = 0;
for(int i = 0; !ifile.eof() && i < 10; i++, numhighscores++)
{
getline(ifile, input_string);
stringstream(input_string) >> highscores[i];
getline(ifile, names[i]);
}
for(int i = 0; i < numhighscores; i++)
{
highscores2[i] = highscores[i];
names2[i] = names[i];
}
ifile.close();
highscore = false;
show_highscores = true;
prevhigh = true;
}
}
. . .
from main.cpp
CODE
void keyboard(unsigned char key, int x, int y)
{
if(accepting_input == true )
{
if(key == 13 || key == 10 || key == '\n' || key == 12 || key == 3)
{
final_name = new_highscore;
accepting_input = false;
}
else if( (key == '\b' || key == 127 || key == '\x7f') && counter_name > 0)
{
counter_name--;
new_highscore[counter_name] = '\0';
}
else
{
new_highscore[counter_name] = key;
counter_name++;
}
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
srand(time(NULL));
initgame();
ifile.open("High Scores.txt");
string input_string;
numhighscores = 0;
for(int i = 0; !ifile.eof() && i < 10; i++, numhighscores++)
{
getline(ifile, input_string);
stringstream(input_string) >> highscores[i];
getline(ifile, names[i]);
}
for(int i = 0; i < numhighscores; i++)
{
highscores2[i] = highscores[i];
names2[i] = names[i];
}
ifile.close();
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("Chase Game");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (0, glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ), 0, 0, 1);
glMatrixMode (GL_MODELVIEW);
glutKeyboardFunc(keyboard);
glutSpecialFunc(arrows);
glutMouseFunc(MouseButton);
glutPassiveMotionFunc(MousePassiveMotion);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
}
This post has been edited by spykid0001: 5 Oct, 2008 - 01:53 PM