Saving Code

I've made a quiz template to help me review my school material. It's fairly easy to make a new exe by just changing the couts and one variable. However, it would be a lot easier to be able to be able to have it say something like: "What is your first question?" and you be able to put the question in, saving it as a string like "questionOne," and reopening a file with that questionOne, as well as other questions, possible choices, and the correct answer, saved. I know this is possible, but I've looked at writing and reading to files but it doesn't really work too well for me. Any ideas? Here's the code for it:
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>

using namespace std;

int answer;

double totalquestions = 0;
double correctanswers = 0;
double incorrectanswers = 0;

double questions;
int questionnumber = 1;

int correctchoice;

void correction(){
            if(answer == correctchoice){
                cout << "You are correct!" << endl << endl;
                correctanswers++;
            }else{
                cout << "You are incorrect! The correct choice was " << correctchoice << endl << endl;
                incorrectanswers++;
            }
        questions++;
        questionnumber++;
}

int main()
{
    cout << "TITLE" << endl << endl;
    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();

    cout << questionnumber << ". " << endl;
        cout << "1: " << endl;
        cout << "2: " << endl;
        cout << "3: " << endl;
        cout << "4: " << endl;
        cin >> answer;
        correctchoice = 1;
        correction();


    double percent = correctanswers/questions * 100;
    cout << "Your score is " << correctanswers << " out of " << questions << "." << endl;
    cout << percent << "%." << endl << endl;

        if(percent>=90){
        cout << "You got an A. Great job!" << endl;
    }
    else if(percent >=80){
        cout << "You got a B. Good job, but you can still improve!" << endl;
    }
    else if(percent >=70){
        cout << "You got a C. Not bad, but study!" << endl;
    }
    else if(percent>=60){
        cout << "You got a D. Study, study, study!" << endl;
    }
    else if(percent<60){
        cout << "You got an F. You have to do better!" << endl;
    }

    return 0;
}
Topic archived. No new replies allowed.