save to a text file...plzz read..

guyyss need your code help to save
1
2
cout << "You've made 3 mistakes ! Game is now over !" << endl;
    cout << "You had " << correctGuesses << " correct guesses before the game was over" << endl;
this info into a text file...


example of a guessing game code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <time.h>
 
using namespace std;
 
int main()
{
    cout << "This is a very simple number guessing game. Each time you will be given a number of the range 0-10." << endl;
    cout << "The objective of the game is to guess whether the next number is going to be higher or not. As simple as that." << endl;
    cout << "You have the ability to make no more than 3 mistakes before you lose, so guess wisely." << endl << endl;
    cout << "You are starting with number 5. Is the next number higher(write H) or lower(write L) ?" << endl;
 
    int mistakes = 0;
    int correctGuesses;
    int prevNum = 5, nextNum;
    char choice;
 
    do
    {
        srand ( time(NULL) );
        do
            nextNum = rand() % 11;
        while (nextNum == prevNum);
 
        cin >> choice;
 
        if (choice == 'H')
        {
            if (prevNum < nextNum)
            {
                cout << "Correct ! The new number is " << nextNum << endl;
                correctGuesses++;
            }
            else if (prevNum > nextNum)
            {
                cout << "Wrong, you made a mistake ! The new number is " << nextNum << endl;
                mistakes++;
            }
        }
 
        if (choice == 'L')
        {
            if (prevNum > nextNum)
            {
                cout << "Correct ! The new number is " << nextNum << endl;
                correctGuesses++;
            }
            else if (prevNum < nextNum)
            {
                cout << "Wrong, you made a mistake ! The new number is " << nextNum << endl;
                mistakes++;
            }
        }
 
        prevNum = nextNum;
    }
    while(mistakes < 3);
 
    cout << "You've made 3 mistakes ! Game is now over !" << endl;
    cout << "You had " << correctGuesses << " correct guesses before the game was over" << endl;
 
    return 0;
}
Last edited on
http://cplusplus.com/reference/fstream/ofstream/
Look at example on the bottom of page: http://cplusplus.com/reference/fstream/ofstream/open/
You can use operator << with filestream as with ostream (cout)
Topic archived. No new replies allowed.