HELP!!!

How do you give a value to each element in a string c++. Where I want this part: "What is 1+1?" is equal to 1

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
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));

    string wordList[10] = {
        "What is 1+1?",
        "What is 1+2?", 
        "What is 2+2?",
        "What is 3+2?", 
        "What is 4+2?", 
        "What is 4+8?", 
        "What is 8+8?", 
        "What is 8+9?", 
        "What is 9+1?",
        "What is 9+2?"};
    string question = wordList[rand() % 10];

    cout << question << endl;


    return 0;
}
Last edited on
What you need is a parallel array of the correct answers.
Save the random number generated at line 23 and use that to index the parallel array.
If the indexed item in the parallel array matches the users input, the the answer is correct.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{   srand(time(0));
    string wordList[10] = {
        "What is 1+1?",
        "What is 1+2?", 
        "What is 2+2?",
        "What is 3+2?", 
        "What is 4+2?", 
        "What is 4+8?", 
        "What is 8+8?", 
        "What is 8+9?", 
        "What is 9+1?",
        "What is 9+2?"};
    const int answers[10] = {2,3,4,5,6,12,16,17,10,11};
    int answer;
    int quest;
    quest = rand() %10; 
    string question = wordList[quest];
    cout << question << endl;
    cin >> answer;
    if (answer == answers[quest])
      cout << "That is incorrect" << endl;
    else
      cout << "Wrong!" << endl;
    system ("pause");      
    return 0;
}



This is the assignment:
"You are going to write a program for Computer test which will read 10
multiple choice questions from a file, order them randomly and provide the test to
the user. When the user done the program must give the user his final score"

I've learned a little about arrays, but have not heard of a parallel array one
It needs to score it based on what the user got correct out of 10 displayed questions. As for multiple choice, true or false is ok to use
Last edited on
Pseudocode

step 1. Display random question "What is 1+1?"

step 2. input answer "2"
           Display "correct"

step 3. store correct into a variable with a value of 1

step 4. repeat step 10 times with a value of 1 incremented in to the variable correct

Step 5. Display the value of the variable correct


At first I got errors because of namespace std, it solves the problem. Thanks for the help c:
Topic archived. No new replies allowed.