I am trying to use freopen() to print to a text file and the screen, but I am only achieving the printing to a file.
I was wondering if there was an easy to save the programs output to a file and print it to the screen? Because I had this working another way, but I ended up having to print out every statement twice. One being for the file the other just for the output.
Note: I am new to C++ and I am trying to learn it for a class next semester so direct answer are needed as I have already look online and couldn't find any simple answers to this solution besides.
Here is what I have so far:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
void menu(){
cout << "\t********************************************************\n"
<< "\t* Welcome to slot machine. *\n"
<< "\t* Would you like to play? (1 to play, 2 not to play) *\n"
<< "\t********************************************************\n\n";
return;
}
void update(int arr[], int &token) {
if (arr[0]==arr[1] && arr[1]==arr[2]) {
token+=4;
cout << "You win\n\n";
} else if (arr[0]==arr[1] || arr[1]==arr[2] || arr[0]==arr[2]) {
token+=1;
cout << "You got two out of three\n\n";
} else {
token-=1;
cout << "You lose\n\n";
}
}
int main() {
freopen("file.txt", "w", stdout);
int x, arr[3], token=4;
srand(time(0));
menu();
cin >> x;
while(token!=0) {
cout << "You have " << token << " tokens\n\n"
<< "Pull? (1 to pull, 2 not to pull)\n\n";
cin>>x;
if(x==1) {
for(int i=0; i<3; i++) {
arr[i]=1+rand()%10;
}
cout << "\t\t";
for(int j=0; j<3; j++) {
cout << arr[j] << " ";
}
cout << "\n\n";
update(arr,token);
}
else{
cout << "OK\n";
}
}
cin.get();
return 0;
}