0

When I run my program it works the first time, the correct input is read and output is written to my output file but once I run it a second time it doesn't write anything the file is just blank even though it reads everything correctly. I can't seem to understand where its messing up or how and I really want to know. The two images are my first and second runs just that after the second run my output file is blank.

first run

second run

#include<iostream>
#include<iostream>
#include<cstdlib>
#include<time.h>
#include<fstream>
#include<string>
using namespace std;



int main()
{
    int i=0;
    int number; // The correct number
    int guess=0;  // The user's guess
    int numGuesses=0; // The number of times the user has guessed
    string lines;
    string player;
    ifstream ifile;
    ofstream ofile;
    //ofstream myfile;
    //
    string names[10];
    int scores[10];
    ifile.open("high_score.txt");

    string first_last_name;
    string temp;
    int score;
    int index=0;
    string title;
    bool topten=false;


    cout <<"Welcome to the number guessing game. The top 10 best scores so far are: "<<endl;
    while(!ifile.eof())
    {

        //getline(ifile,lines);
        ifile >>first_last_name;
        //cout << first_last_name;
        ifile >> temp;
        first_last_name.append(" ");
        first_last_name.append(temp);
        ifile >> score;
        names[index]=first_last_name;
        scores[index++]=score;

    }
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    for (int i=0; i < 10; i++)
    {
        cout << names[i] << " " << scores[i] <<endl;

    }
    // Seed the random generator.
    srand(static_cast<int> (time(NULL)));

    // Generate a random number between 1 and 100.
    number = (rand() % 100) + 1;

    cout << "Let's play the number guessing game! What is your name? " <<endl; 
    getline(cin,player);
    cout << endl;   
    while(guess!=number)
    {
        cout << "guess the number the computer randomly picked between 1 - 100: ";
        cin >> guess;
        numGuesses++;

        // Check if the user has guessed the correct number.
        // If not, tell him if his guess is too low or too high
        if(number > guess)
        {
            cout << "sorry, your guess is too low" << endl;
        }
        else if(number < guess)
        {
            cout << "sorry, your guess is too high" << endl;
        }
        else
        {
            cout << "You guessed right!!!!"<<endl;
            cout << "You win!!!" << endl;
            break;
        }

    }
       cout << "It took you "<< numGuesses << " guesses "<< player<< endl;
////////////////////////////////////////////////////////////////////////
       if (numGuesses<4)
       {
       cout << "Amazing! Or was it luck" << endl;
       }
       else if(numGuesses<6)
       {
           cout <<"That's a very good score..." <<endl;
       }

       else if (numGuesses<8)
       {
        cout << "That's pretty good but you can do better..." << endl;
       }

       else if ( numGuesses<10)
       {
       cout << "Not too shabby, but not too good either..."<< endl;
       }

       else
       {
       cout << "What a terrible score!..." << endl;
       }

    for(int i=0; i < 10; i++)
    {
        if(numGuesses <= scores[i])
        {
            for( int k=9;k>i;k--)
            {   
                scores[k]=scores[k-1];
                names[k]=names[k-1];
            }

            scores[i]=numGuesses;
            names[i]=player;
            topten=true;
            break;
        }


    }
    if(topten==true)
    {

        cout << "Hey, you made it to the top ten , Congratzzzzzzzz!!!!" <<endl;
    }
    ofile.open("high_score.txt");
    for(int i=0;i<10;i++)
    {
        ofile <<"\t"<< names[i] << " " << scores[i] <<endl;
        ofile << endl;

    }

    return 0;
}
Realzen
  • 11
  • 2
  • I'd be checking the error codes on your open functions at the least. See http://www.cplusplus.com/doc/tutorial/files/ for some help on file I/O – Michael Dorgan Jan 24 '18 at 00:57
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Retired Ninja Jan 24 '18 at 01:03

2 Answers2

1

You need to close the input file before you open the output file, since they are both referring to the same file.

The closing can be done explicitly by calling ifile.close() or implicitly by making ifile go out of scope before you open the output file.

The latter can be done like this:

{
    ifstream ifile;
    ifile.open("high_score.txt");
    // do stuff with ifile
}
ofstream ofile;
ofile.open("high_score.txt");
// do stuff with ofile
Sid S
  • 5,887
  • 2
  • 12
  • 23
0

Where is ifile.close() and ofile.close() you should close else your stream will go into invalid state.

Nitin
  • 135
  • 9
  • 1
    this may be right, but on the official docs it says: Note that any open file is automatically closed when the ofstream object is destroyed.which should happen shortly after return 0; is executed – cptwonton Jan 24 '18 at 00:56
  • yes I've never had issues before leaving them out – Realzen Jan 24 '18 at 01:07
  • You should atleast correct the obvious errors first. I compiled your program and it crashed, I will need first debug the crash. – Nitin Jan 24 '18 at 01:18
  • I don't have any errors when i compile? but I am using terminal gcc to compile and using Vim to write to the code. – Realzen Jan 24 '18 at 01:29
  • 1
    thanks Nitin. Even though I am barely learning how to code and this is the most advanced stuff I Know. Im pretty sure there are faster ways but this is all i know. thanks for the help though you sure are smart. – Realzen Jan 24 '18 at 02:17