Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am looking for some help in writing information to a file in C++ for my guessi

ID: 3621625 • Letter: I

Question

I am looking for some help in writing information to a file in C++ for my guessing game. I am stuck on one part. here is the description: I am to output the game number followed by each guess to a file. I believe it is supposed to be in the loop but not sure how or if that is even possible. It is supposed to output to the file for each game played. Enclosed is my code so far.

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
ofstream outputFile;
outputFile.open("guessinggame.txt");

int nOuter;
int i;
int nRand;
int nGuess;
int totalguesses = 0;
int nGuessCount;
int max = -1;
int min = 100;
int number = -1;



cout << "Welcome to the best guessing game EVER! ";
cout << "I am thinking of a number between 1 to 10, ";
cout << "so guess what it is. ";
cout << "How many times do you want to play? ";
cin >> nOuter;

srand(time(0));

for (i = 1; i <= nOuter; ++i) //expressions for loop
{
nRand = (rand() % 10) + 1;
nGuessCount = 0;

cout << "Let the guessing begin! ";
cout << "Game: " << i << endl;

do
{
cout << " Next guess: ";
cin >> nGuess;
++nGuessCount;
}
while (nGuess != nRand);

cout << "You guessed the correct number in " << nGuessCount << " tries! I was thinking of the number " << nRand << endl;
if (nGuessCount < min)
min = nGuess;
if (nGuessCount > max)
max = nGuess;
totalguesses += nGuess;
cout << endl;

}

cout << "lowest number of guesses in a game: " << min << endl;
cout << "Highest number of guesses in a game: " << max << endl;
cout << "Average guesses in a game " << totalguesses / nOuter << endl;


//output the average, highest, and lowest number of guesses to my file.
outputFile << "lowest number of guesses in a game: " << min << endl;
outputFile << "Highest number of guesses in a game: " << max << endl;
outputFile << "Average guesses in a game " << totalguesses / nOuter << endl;

//closing the file
outputFile.close();
cout << "Done. ";


system("PAUSE");
return 0;
}

Explanation / Answer

please rate - thanks

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;


int main()
{
ofstream outputFile;
outputFile.open("guessinggame.txt");

int nOuter;
int i;
int nRand;
int nGuess;
int totalguesses = 0;
int nGuessCount;
int max = -1;
int min = 100;
int number = -1;



cout << "Welcome to the best guessing game EVER! ";
cout << "I am thinking of a number between 1 to 10, ";
cout << "so guess what it is. ";
cout << "How many times do you want to play? ";
cin >> nOuter;

srand(time(0));

for (i = 1; i <= nOuter; ++i) //expressions for loop
{
nRand = (rand() % 10) + 1;
nGuessCount = 0;

cout << "Let the guessing begin! ";
cout << "Game: " << i << endl;
outputFile<<"Game Guess ";
outputFile<<" # # Guess ";
do
{
cout << " Next guess: ";
cin >> nGuess;
++nGuessCount;
outputFile<<i<<" "<<nGuessCount<<" "<<nGuess<<endl;
}while (nGuess != nRand);
cout << "You guessed the correct number in " << nGuessCount << " tries! I was thinking of the number " << nRand << endl;
if (nGuessCount < min)
min = nGuess;
if (nGuessCount > max)
max = nGuess;
totalguesses += nGuessCount;
cout << endl;


}

cout << "lowest number of guesses in a game: " << min << endl;
cout << "Highest number of guesses in a game: " << max << endl;
cout << "Average guesses in a game " << totalguesses / nOuter << endl;


//output the average, highest, and lowest number of guesses to my file.
outputFile << "lowest number of guesses in a game: " << min << endl;
outputFile << "Highest number of guesses in a game: " << max << endl;
outputFile << "Average guesses in a game " << totalguesses / nOuter << endl;

//closing the file
outputFile.close();
cout << "Done. ";


system("PAUSE");
return 0;
}