Question: C+++ PROGRAM Homework Assignment Due 12/4/17: Modify the game.cpp program to allow no more than five tries. At the end of a game, whether the

C+++ PROGRAM

Homework Assignment Due 12/4/17: Modify the game.cpp program to allow no more than five tries. At the end of a game, whether the user guessed correctly or not, the program should ask if he/she wants to play again.

How to program with files

Include fstream

declare file stream variable (object), like

ifstream fin;

ofstream fout;

use open() to initialize file stream variable

use file stream variable as you would use cin and cout

use close() to close the file when finished with it

Example program

#include

#include

#include

#include

using namespace std;

int main()

{ //Create three columns of data for plotting.

double x;

ifstream xdata; //declare input stream

ofstream plotdata; //declare output stream

xdata.open("data1"); //xdata uses file data1

if( xdata.fail() ) //check for error

{

cout << "error opening input file" << endl;

exit(1);

} //end if fail

plotdata.open("plot1");//plotdata uses file plot1

xdata >> x; //input x from file

while(!xdata.eof()) //while not end of file

{

if( x>0 ) //write to plot file

plotdata<

xdata >> x; //get next data point

} //end while

xdata.close();

plotdata.close();

return 0;

} //end main

NOTE: to run the program, you need to have a file called data1

Create it using NANO. For instance,

23

56

18

6

7

43

Copy an Array to File

// crFile.cpp

#include

#include

#include

#include

using namespace std;

int main()

{

double x[100];

srand(time(NULL));

ofstream plotdata; //declare output stream

plotdata.open("plot1");//plotdata uses file plot1

for (int i=0;i<100;i++){

x[i] = (double)(rand()%(150-75+1) + 75)/1.50;

plotdata << x[i] << endl;

}

plotdata.close();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!