Fin>> & Fout<<

I made a quiz-game.It is able to write the name(you have already chosen)and the score in a file named "Quiz-Score". But when i run the program again and write another name it erases the previous one.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<string>
#include<windows.h>
#include<fstream>

using namespace std;

int main()
{

    ofstream fout ("Quiz-Score");

    int score=0;

    string ans1,ans2,ans3,ans4,ans5;
    string name;
    string check;

    cout<<"Welcome to the Quiz!"<<endl;
    cout<<"The rules:"<<endl<<"You win one point for each right answer"<<endl;
    cout<<"For a wrong answer you lose tha game(GAME OVER)!"<<endl;
    cout<<"There are 5 questions"<<endl;
    cout<<"Write the letter of the choice example:"<<endl;
    cout<<"When did the second world war start?:";
    cout<<"a.1943 b.1945 c.1939 d.1934"<<endl;
    Sleep(100);
    cout<<"c."<<endl;
    cout<<"Correct!"<<endl;
    cout<<endl;
    cout<<"Who was the first emperor of the Byzantine empire?"<<endl<<"a.Valens b.Gratian c.Theodosius d.Constantine The Great"<<endl;
    cin>>ans1;
    if(ans1=="d")
    {
        score++;
        cout<<"Correct!"<<endl;
        cout<<"Who was the designer of ENIAC, the first digital computer?"<<endl;
        cout<<"a.John Maucley b.Bill Gates c.Steve Jobs d.Bob Miner"<<endl;
        cin>>ans2;
        if(ans2=="a")
        {
            score++;
            cout<<"Correct!"<<endl;
            cout<<"What country has borders with Uzbekistan?"<<endl;
            cout<<"a.Turkey b.Egypt c.Turkmenistan d.Iran?"<<endl;
            cin>>ans3;
            if(ans3=="c")
            {
                score++;
                cout<<"Correct!"<<endl;
                cout<<"When did the first world war begin?"<<endl;
                cout<<"a.1914 b.1915 c.1913 d.1916"<<endl;
                cin>>ans4;
                if(ans4=="a")
                {
                    score++;
                    cout<<"Correct!"<<endl;
                    cout<<"What metal's symbol is FE?"<<endl;
                    cout<<"a.Gold b.Iron c.Bronze d.Copper"<<endl;
                    cin>>ans5;
                    if(ans5=="b")
                    {
                        score++;
                        cout<<"Correct!"<<endl;
                        cout<<"Please write your name:";
                        cin>>name;
                        cout<<endl;
                        cout<<"Your score is in the file Quiz-Score"<<endl;
                        fout<<name<<"-"<<score;
                    }
                }
                else
                {
        cout<<"Wrong"<<endl;
        cout<<"Please enter your name:";
        cin>>name;
        fout<<name<<"-"<<score;
        cout<<endl;
        cout<<"Your score is in the file Quiz-Score"<<endl;

        Sleep(1000);
        return 0;
                }
            }
            else
            {
        cout<<"Wrong"<<endl;
        cout<<"Please enter your name:";
        cin>>name;
        fout<<name<<"-"<<score;
        cout<<endl;
        cout<<"Your score is in the file Quiz-Score"<<endl;

        Sleep(1000);
        return 0;
            }
        }
        else
        {
        cout<<"Wrong"<<endl;
        cout<<"Please enter your name:";
        cin>>name;
        fout<<name<<"-"<<score;
        cout<<endl;
        cout<<"Your score is in the file Quiz-Score"<<endl;

        Sleep(1000);
        return 0;
        }
    }
    else
    {
        cout<<"Wrong"<<endl;
        cout<<"Please enter your name:";
        cin>>name;
        fout<<name<<"-"<<score;
        cout<<endl;
        cout<<"Your score is in the file Quiz-Score"<<endl;

        Sleep(1000);
        return 0;
    }
}

Can anybody help me?
Last edited on

you need to open the file for "append"

http://www.cplusplus.com/reference/fstream/fstream/open/

1
2
ofstream fout ;
fout.open ("Quiz-Score", std::ofstream::app);
Last edited on
Topic archived. No new replies allowed.