Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,115 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,029 people online right now. Registration is fast and FREE... Join Now!




C++ File Output Problems

 
Reply to this topicStart new topic

C++ File Output Problems, I'm Having some trouble and can't figure out what is wrong

spykid0001
post 5 Oct, 2008 - 01:42 PM
Post #1


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 12


My Contributions


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 mad.gif

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
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 5 Oct, 2008 - 02:35 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


What is the error message it is giving you?
User is offlineProfile CardPM

Go to the top of the page

spykid0001
post 5 Oct, 2008 - 02:39 PM
Post #3


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 12


My Contributions


I don't get an error it compiles fine no warnings nothing but the names aren't written to the file when the program is running the score is but not the name
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 5 Oct, 2008 - 02:56 PM
Post #4


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


Are you sure that the names have a value when they're written?

This post has been edited by GWatt: 5 Oct, 2008 - 03:01 PM
User is online!Profile CardPM

Go to the top of the page

Martyr2
post 5 Oct, 2008 - 03:14 PM
Post #5


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


There is some fishy stuff here....

cpp

place = place; // <--- No purpose
highscores[place] = wintime;
place = place; // <--- No purpose
names[place] = final_name;

// I assume this is for copying all those names from place until the end of highscores list?
// If so, why aren't you just inserting the name into the highscores list and
// move all down without the need of this highscores2 and names2 arrays?
// As far as I am concerned you should only need one set of arrays for this
// operation.

for(int i = (place+1); i < numhighscores; i++)
{
highscores[i] = highscores2[i-1];
names[i] = names2[i-1];
}


In this part of the code make sure that names2 has values, that it is copying the right values over and that by the time it goes out that names[] has names in it.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

spykid0001
post 5 Oct, 2008 - 03:36 PM
Post #6


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 12


My Contributions


QUOTE(Martyr2 @ 5 Oct, 2008 - 04:14 PM) *

There is some fishy stuff here....

cpp

place = place; // <--- No purpose
highscores[place] = wintime;
place = place; // <--- No purpose
names[place] = final_name;

// I assume this is for copying all those names from place until the end of highscores list?
// If so, why aren't you just inserting the name into the highscores list and
// move all down without the need of this highscores2 and names2 arrays?
// As far as I am concerned you should only need one set of arrays for this
// operation.

for(int i = (place+1); i < numhighscores; i++)
{
highscores[i] = highscores2[i-1];
names[i] = names2[i-1];
}


In this part of the code make sure that names2 has values, that it is copying the right values over and that by the time it goes out that names[] has names in it.

smile.gif


the place = place stuff is left over that i forgot to delete

and i have already tested to make sure that everything has the values it's supposed to when the code is running using xcode's debugger
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 5 Oct, 2008 - 03:43 PM
Post #7


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


I'm looking through the code here, and while I'm not experienced with opengl, I never see where you set final_name, or new_highscore. Empty values are written in place of names, and when you read from the file, the program correctly reads empty lines.
At least, that's what I can tell.

Try seeding the file and see if you get results.
User is online!Profile CardPM

Go to the top of the page

spykid0001
post 5 Oct, 2008 - 04:19 PM
Post #8


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 12


My Contributions


the final_name variable and stuff is set in keyboard()
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;                                                         //here i set them
            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++;
        }
    }


User is offlineProfile CardPM

Go to the top of the page

GWatt
post 5 Oct, 2008 - 04:49 PM
Post #9


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


Could you post/send me the entire project. I can't tell what's not working right, from the code I have so far.
User is online!Profile CardPM

Go to the top of the page

spykid0001
post 7 Oct, 2008 - 03:57 PM
Post #10


New D.I.C Head

*
Joined: 6 Aug, 2008
Posts: 12


My Contributions


smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif
smile.gif Problem resolved thanks to GWatt smile.gif

smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:18AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month