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);
}