hi, i have this code

#include <fstream.h> 
#include<iostream>
using namespace std;
int main (char argc)
{
    for(;;)
    {
	fstream file;
	file.open("file.txt");
	
	cout << "Enter new high score." << endl;
	double n2;
	cin >> n2;
	double n1;
	file >> n1;
	
	if(n1 > n2)
	{
          cout << "You didn't beat the high score of "<<n1<<"."<< endl;
    }
    
    if(n1 < n2)
    {
          cout << "You beat the high score of "<<n1<<"."<< endl;
          file << n1 << endl;
          
    }
    
    if(n1 == n2)
    {
          cout << "Your score is the same as the high score" << endl;
    }
    file.close();
    
}
}

and for some reason, after i enter a score higher than the high score, the text file doesnt change. any solutions?

//...

if(n1 < n2)
{
    cout << "You beat the high score of " << n1 << "." << endl;
    
    file.seekp(0, ios::beg); // move to the beginning of the file before writing
    file << n2 << endl; // I believe you want to write the new high score
}

//...

ok, thanks for that. now i have some other code with the same problem and i put file.seekp(0, ios::beg); where its supposed to go but when i beat the high score, the text file doesnt change

#include<iostream>
#include<time.h>
#include<windows.h>
#include<conio.h>
#include<fstream.h>
using namespace std;
int main (char argc)
{
    for(;;)
    {
    
    srand( (unsigned)time(NULL));
    system("color 0f");
    system("cls");
    cout << "Welcome to the speed test game!" << endl;
    cout << "1: Easy \n2: Medium \n3: Hard \n4: Extreme\n5: Exit" << endl;
    
    int z = getch();
    
    if(z == 53)
    {
         return 0;
    }
    
    if(z == 49)
    {
         restart:
         system("color 0f");
         system("cls");
         cout << "Press enter as fast as you can after the screen turns green." << endl;
         cout << "And if you press enter when the screen is NOT green, your score will come out\n as zero." << endl;
         
         system("pause");
                 
         int n1 = rand() % 10;
         int n2 = rand() % 6;
         
         n1 = n1 * 1000;
         n2 = n2 * 1000;
         Sleep(n2);
         system("color cf");
         Sleep(n1);
         system("color af");
         DWORD before = GetTickCount();
         int i = getch();
         cout << "Your reaction speed is "<<GetTickCount() - before<< " milliseconds." << endl;
         double score1 = GetTickCount() - before;
         fstream score;
         score.open("score.txt");
         double score2;
         score >> score2;
         double bac;
         bac = score2;
         if(score2 < score1)
         {
                   cout << "You didn't beat the high score." << endl;
                   cout << "The high score is "<<score2<<"."<< endl;
         }
         
         if(score2 > score1)
         {
                   cout << "You beat the high score of "<<score2<<"!"<< endl;
                   
         if(score1 == 0)
         {
                   cout << "But since you didnt press enter when the screen was green, your\n score was not recorded." << endl;
                   score.seekp(0, ios::beg);
                   score << bac << endl;
         }
         else
         {
         score.seekp(0, ios::beg);
         score << score1 << endl;
         }
         }
         
         if(score2 == score1)
         {
                   cout << "Your score is the same as the high score." << endl;
         }
         
         score.close();
         cout << "1: Menu \n2: Exit \n3: Restart" << endl;
         srand(n1);
         srand(n2);
         
         int a = getch();
         
         if(a == 51)
         {
              goto restart;
         }
         
         if(a == 50)
         {
              return 0;
         }
         
         if(a == 49)
         {
         }
         }
         
         if(z == 50)
         {
              restart1:
         system("color 0f");
         system("cls");
         cout << "Press enter as fast as you can after the screen turns green." << endl;
         cout << "And if you press enter when the screen is NOT green, your score will come out\n as zero." << endl;
         
         system("pause");
                 
         int n1 = rand() % 10;
         int n2 = rand() % 6;
         int n3 = rand() % 8;
         
         n1 = n1 * 1000;
         n2 = n2 * 1000;
         n3 = n3 * 1000;
         Sleep(n2);
         system("color cf");
         Sleep(n3);
         system("color ef");
         Sleep(n1);
         system("color af");
         DWORD before = GetTickCount();
         int i = getch();
         cout << "Your reaction speed is "<<GetTickCount() - before<< " milliseconds." << endl;
         double score1 = GetTickCount() - before;
         fstream score;
         score.open("score.txt");
         double score2;
         score >> score2;
         double bac;
         bac = score2;
         
         if(score2 < score1)
         {
                   cout << "You didn't beat the high score." << endl;
                   cout << "The high score is "<<score2<<"."<< endl;
         }
         
         if(score2 > score1)
         {
                   cout << "You beat the high score of "<<score2<<"!"<< endl;
                   
         if(score1 == 0)
         {
                   cout << "But since you didnt press enter when the screen was green, your\n score was not recorded." << endl;
                   score.seekp(0, ios::beg);
                   score << bac << endl;
         }
         else
         {
         score.seekp(0, ios::beg);
         score << score1 << endl;
         }
         }
         
         if(score2 == score1)
         {
                   cout << "Your score is the same as the high score." << endl;
         }
         
         score.close();
         cout << "1: Menu \n2: Exit \n3: Restart" << endl;
         srand(n1);
         srand(n2);
         srand(n3);
         
         int a = getch();
         
         if(a == 51)
         {
              goto restart1;
         }
         
         if(a == 50)
         {
              return 0;
         }
         
         if(a == 49)
         {
         }
    }
    if(z == 51)
    {
         restart2:
         system("color 0f");
         system("cls");
         cout << "Press enter as fast as you can after the screen turns green." << endl;
         cout << "And if you press enter when the screen is NOT green, your score will come out\n as zero." << endl;
         
         system("pause");
                 
         int n1 = rand() % 10;
         int n2 = rand() % 6;
         int n3 = rand() % 8;
         int n4 = rand() % 5;
         
         n1 = n1 * 1000;
         n2 = n2 * 1000;
         n3 = n3 * 1000;
         n4 = n4 * 1000;
         Sleep(n2);
         system("color cf");
         Sleep(n3);
         system("color ef");
         Sleep(n4);
         system("color 9f");
         Sleep(n1);
         system("color af");
         DWORD before = GetTickCount();
         int i = getch();
         cout << "Your reaction speed is "<<GetTickCount() - before<< " milliseconds." << endl;
         double score1 = GetTickCount() - before;
         fstream score;
         score.open("score.txt");
         double score2;
         score >> score2;
         double bac;
         bac = score2;
         
         if(score2 < score1)
         {
                   cout << "You didn't beat the high score." << endl;
                   cout << "The high score is "<<score2<<"."<< endl;
         }
         
         if(score2 > score1)
         {
                   cout << "You beat the high score of "<<score2<<"!"<< endl;
                   
         if(score1 == 0)
         {
                   cout << "But since you didnt press enter when the screen was green, your\n score was not recorded." << endl;
                   score.seekp(0, ios::beg);
                   score << bac << endl;
         }
         else
         {
         score.seekp(0, ios::beg);
         score << score1 << endl;
         }
         }
         
         if(score2 == score1)
         {
                   cout << "Your score is the same as the high score." << endl;
         }
         
         score.close();
         cout << "1: Menu \n2: Exit \n3: Restart" << endl;
         srand(n1);
         srand(n2);
         srand(n3);
         srand(n4);
         
         int a = getch();
         
         if(a == 51)
         {
              goto restart2;
         }
         
         if(a == 50)
         {
              return 0;
         }
         
         if(a == 49)
         {
         }
}

         if(z == 52)
         {
              restart3:
         system("color 0f");
         system("cls");
         cout << "Press enter as fast as you can after the screen turns green." << endl;
         cout << "And if you press enter when the screen is NOT green, your score will come out\n as zero." << endl;
         
         system("pause");
                 
         int n1 = rand() % 10;
         int n2 = rand() % 6;
         int n3 = rand() % 8;
         int n4 = rand() % 5;
         int n5 = rand() % 4;
         
         n1 = n1 * 1000;
         n2 = n2 * 1000;
         n3 = n3 * 1000;
         n4 = n4 * 1000;
         n5 = n5 * 1000;
         Sleep(n2);
         system("color cf");
         Sleep(n3);
         system("color ef");
         Sleep(n4);
         system("color 9f");
         Sleep(n5);
         system("color 0a");
         Sleep(n1);
         system("color af");
         DWORD before = GetTickCount();
         int i = getch();
         cout << "Your reaction speed is "<<GetTickCount() - before<< " milliseconds." << endl;
         double score1 = GetTickCount() - before;
         fstream score;
         score.open("score.txt");
         double score2;
         score >> score2;
         double bac;
         bac = score2;
         
         if(score2 < score1)
         {
                   cout << "You didn't beat the high score." << endl;
                   cout << "The high score is "<<score2<<"."<< endl;
         }
         
         if(score2 > score1)
         {
                   cout << "You beat the high score of "<<score2<<"!"<< endl;
                   
         if(score1 == 0)
         {
                   cout << "But since you didnt press enter when the screen was green, your\n score was not recorded." << endl;
                   score.seekp(0, ios::beg);
                   score << bac << endl;
         }
         else
         {
         score.seekp(0, ios::beg);
         score << score1 << endl;
         }
         }
         
         if(score2 == score1)
         {
                   cout << "Your score is the same as the high score." << endl;
         }
         
         score.close();
         cout << "1: Menu \n2: Exit \n3: Restart" << endl;
         srand(n1);
         srand(n2);
         srand(n3);
         srand(n4);
         srand(n5);
         
         int a = getch();
         
         if(a == 51)
         {
              goto restart3;
         }
         
         if(a == 50)
         {
              return 0;
         }
         
         if(a == 49)
         {
         }
}
}
}

It works for me. At least on the easy level. Are you sure the file exists?

Another thing. I noticed that if the old score has more digits than the new one,
some of the old digits remain to the file. To take care of this, use different objects
for reading and writing from/to the file. If I were you, I would create two functions:

//...

double read_score()
{
    double score;

    ifstream fin("score.txt");

    fin >> score;

    fin.close();

    return score;
}

void write_score(double score)
{
    // opening for output erases 
    // the contents of the file

    ofstream fout("score.txt");

    fout << score << endl;

    fout.close();
}

//...

double old_score = read_score();

//...

double new_score = //...
write_score(new_score);

//...

heres another problem that i found, when i delete the text file, the program doesnt create a new one to store the score. also i dont think that its the amount of digits that causes the problem. my first code snippet works about the same as my second. I just dont know why it doesnt work.

To check whether the file exists or not, you can try opening it for input and see if it's open. If it's
not open, it means it doesn't exist. If it doesn't exist, opening it for output will automatically create it.

Here's some pseudocode:

open file for input (use an ifstream object)

check if the file is open using the is_open member function

if (the file is open) everything is fine

else 
{
    open the file for output (using an ofstream object)
    output a big value to the file (e.g. 10000),
    which will be the default high score
}

ok, i got it working. i kind of summerized your second code snippet into my own code and that seem to fix it. thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.