Trying to write to a file with this function in my program, for some reason itl make the file, it'l output the text on screen, it all works as intended, but i go to check on the file, and its blank inside. so im guessing im doin somethin dumb, help/tips appreciated.

void rollh()
{
     
     
     int total;
     int roll;
     int rolls;
     int dice;
     int side;
     string dices;
     string sides;
     bool choose;
     char fname[50];
     ofstream rollfile;
     choose = true;
     
     cout << "DND Dice Roller" << endl
          << "-------------------" << endl
          << "Please enter the name you want to save the file as..." << endl
          << "(.txt will be added to the end automagically!)" << endl
          << "-------------------" << endl
          << "Filename: ";
    
    cin >> fname;
    strcat(fname, ".txt");
    
    
           
    cout << "DND Dice Roller" << endl
         << "-------------------" << endl
         << "Please enter the number of dice and then how many sides." << endl
         << "(If you want to return to main menu just put 0 for either)" << endl
         << "-------------------" << endl;
    
    do
    {
           rollfile.open(fname);
           
           unsigned seed = time(0);
           srand(seed);
           int i;
           rolls = 0;
           roll = 0;
           
                
           cout << "dice: ";
           cin >> dices;
           
           if(dices == "0")
           {
               choose = false;
               cout << endl << "Returning to Main Menu..." << endl;
               break;
           }
           
           stringstream d(dices);
           
           if( (d >> i).fail() )
           {
               cout << endl << "Invalid entry, try again..." << endl;
               system("pause");
               continue;
           }
           
           
           cout << "Sides: ";
           cin >> sides;
           
           if(sides == "0")
           {
               choose = false;
               cout << endl << "Returning to Main Menu..." << endl;
               break;
           }
           
           stringstream s(sides);
           
           if( (s >> i).fail() )
           {
               cout << endl << "Invalid entry, try again..." << endl;
               system("pause");
               continue;
           }
           
           
           cout << endl 
                << "--------------------------------------------" << endl
                << "Rolling " << dices << " dice, each with " << sides << " sides:" << endl;
                
           rollfile << endl 
                    << "--------------------------------------------" << endl
                    << "Rolling " << dices << " dice, each with " << sides << " sides:" << endl;
           
           
           total = 0;
           dice = GetIntVal(dices);
           side = GetIntVal(sides);
           
           for(int d = dice; d > 0; d--)
           {
               roll = 1 + rand() % side;
               ++rolls;
               
               total += roll;
               cout << rolls << ". " << roll << endl;
               rollfile << rolls << ". " << roll << endl;
           }
           
           cout << "--------------------------" << endl
                << "With a sum total of: " << total << endl
                << "--------------------------" << endl << endl;
           
           rollfile << "--------------------------" << endl
                    << "With a sum total of: " << total << endl
                    << "--------------------------" << endl << endl;
           
           rollfile.close();
           
    }while(choose);
}

Recommended Answers

All 2 Replies

Not sure if you intend this, but you have you open and close your file within your do... while loop. That means that you're going to continuously overwriting it.

Beyond that, assuming that the above isn't the problem, as far as debugging, stick some pauses in there after it opens, after it writes something, etc., and do a directory listing, see if there file is increasing in size. If not, then you have found your error presumably. Check the failure bits of the ofstream to make sure the stream is in a non-failed state.

But I'm betting you aren't intending to continually open and close it in that do...while loop?

Yea I just pulled it out of the loop, forgot I had put it in there to see if that would fix it, getting same issue. ill try some failchecks with the ofstream, hopefully I can find it :D

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.