-1

I want to print out the last 10 numbers in my file, but when i try and print it out, my program prints out the last number inputted 10 times. How do i fix this so it inputs the last 10 numbers instead of just the last number?

else if (option == 2)
    {
        writefile.open("highscores.txt");
        cout << "You have selected '2.' Here are the current highscores" << endl;
        for (int i = 0; i < 10 && !writefile.eof(); i++) 
        {
            writefile >> x;
            cout << x << endl;
        }

Here is the input for the program.

if (option == 1)
    {
        myfile.open("highscores.txt");
        writefile.open("highscores.txt");
        myfile << "Top Ten Highscores.\n";

        cout << "You have chosen '1.' Please enter 10 scores!" << endl;
        for (int i = 0; i < 10; i++)
        {
            cin >> x;
            myfile << x << "\n";
        }

        cout << "Your number has been entered!" << endl;
        myfile.close();
        writefile.close();
        continue;
    }
klass
  • 9
  • 1

1 Answers1

0

Your txt file contains 11 line, you first need to read string part that you added in option 1.

myfile << "Top Ten Highscores.\n";

For read this part, you can simlply use getline(myfile, line) and line should be string. Then you can read numbers from txt file.

yenicead
  • 15
  • 5