In the game I wanted to like put the win's & lose's in a report file so I used file streams, but there are some errors which I don't know the reason of..
All the error comes in this part when I put it outside the main (i.e. gloabal):
Code:
ofstream target;
string fname = "report.txt";
target.open(fname.c_str());
But when I put this code inside the main I get no errors from the compiler.. but the problem is that when I put it inside the main function everytime I call the main function the file is re-opened so all the old data inside it are erased.. what can I do..
Thanks
-Amy
The whole program code:
Code:
// Including librarie(s) needed for running the program
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
// declaring a prototype functions
int rng();
char winorlose();
int budgetcalculator(ofstream, char, float, float);
// Start report file preparation
ofstream target;
string fname = "report.txt";
target.open(fname.c_str());
// End report file preparation
// declaring a global variable
float budget = 10000;
// starting the main function
int main()
{
// declaring local variables
float bet;
char winlose;
cout<<"Your budget is: $"<<budget<<endl;
cout<<"Please input the amount of money you want to bet: ";
cin>>bet;
srand ( (unsigned) time (NULL) ); //Initialize RNG
do
{
winlose = winorlose();
}while(winlose=='n');
budget = budgetcalculator(target, winlose, budget, bet);
cout<<"Your budget now is: "<<budget<<endl<<endl;
// Start play again option
char playagain;
cout<<"Do you want to play again? [press (y) for yes, and (n) for no]: ";
toupper(playagain);
cin>>playagain;
if(playagain=='y')
main();
// End play again option
// closing the output stream
// Start showing game report
string line;
string filename = "report.txt";
ifstream source;
source.open(filename.c_str());
cout<<endl<<"This is a report for money won and lost from the orignial budget: "<<endl;
cout<<"Original budget: $10000"<<endl;
cout<<"Final budget: $"<<budget<<endl;
cout<<"---------------------"<<endl;
while(!source.eof())
{
getline(source,line);
cout<<line<<endl;
}
// End showing game report
target.close();
return 0;
}
// end of the main function
int rng()
{
// declaring local variables
const int n = 6; // the number of faces of the dice
int x1, x2, sum; // the 2 number generated by the RNG (x1 & x2), and their sum
x1 = rand( ) % n + 1; // Generate a number from the sequence
cout<<"The 2 dice threw have the numbers: "<<x1<<" "; // Print it
x2 = rand( ) % n + 1; // Generate another number from the sequence
cout<<"& "<<x2<<endl; // Print it
sum = x1 + x2; // summing the two randomly generated numbers
cout<<"Thir sum is: "<<sum<<endl;
return sum;
}
//end of function
char winorlose()
{
int dicesum;
dicesum = rng();
// if statment for checking if the sum of the 2 randomly generated numbers is 7 or 11
if((dicesum==7) || (dicesum==11))
return 'w';
else if((dicesum==2) || (dicesum==3) || (dicesum==12))
return 'l';
else
return 'n';
// end of if statment
}
int budgetcalculator(ofstream destination, char state, float credit, float moneybet)
{
if(moneybet<=credit)
{
if(state=='w')
{
credit += moneybet;
destination<<"$"<<moneybet<<" won."<<"\n";
}
else if(state=='l')
{
credit -= moneybet;
destination<<"$"<<moneybet<<" lost."<<"\n";
}
}
else
{
cout<<"Bet is larger than your current budget."<<endl;
main();
}
return credit;
}