Use arrray and for/while loop to create a quiz game

Thank you joinnin and Thomas for your reply! I almost had a panic attack this morning as this homework due tomorrow. After revised some of bad syntax, I am still confused about the condition of my if statement. I really don't know how to relate userAnswer to each individual index of solution array I declared. Additionally, I am not sure if my for loop with an if statement really make sense.


This is again the requirement of the assignment:
"I was asked to create an easy version of Who wants to be a millionaire game. Players keep answer questions until they are wrong, each time the value of question they answer correctly will be added to the total point. Whether they lose or win, they take the home the total value of all the questions."

I am supposed to use array and flow control.





#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[]){
//Do not change code above this line!

unsigned int total = 0;
char userAnswer;

string questions[5]{
questions[0]="Question #0($100): How many rings are there on the Olympic flag?",
questions[1]="Question #1($100): How many holes are on a standard bowling ball?",
questions[2]="Question #2($100): How many points is the letter X worth in English-language Scrabble?",
questions[3]="Question #3($100): Which of these animals does NOT appear in the Chinese zodiac?",
questions[4]="Question #4($100): What is a pomelo?"
};//Do I need to specify what each index is or I can just list what they are without using question[]=...

string choices[5]{
choices[0] ="\na.0\nb.4\nc.5\nd.7",

choices[1] = "\na. 2\nb. 3\nc. 5\nd. 10",

choices[2] = "\na. 0\nb. 8\nc. 10\nd. 11",

choices[3] = "\na. Bear\nb: Rabbit\nc: Dragon\nd: Dog",

choices[4] = "\na. An old-fashioned punching bag\nb. A breed of dog\nc.The largest citrus fruit\nd. Something that cheerleaders hold"
};

char solutions [5] {
solutions[0]='a',
solutions[1]='b',
solutions[2]='c',
solutions[3]='d',
solutions[4]='a'
};




for (size_t i = 0; i < 5; i++) {
cout << questions <<endl<< choices << endl;
cout << "Enter your answer in lower case letter: " << endl;
cin >> userAnswer;

if (userAnswer== solutions) {/*I am so lost because I don't know relate userAnswer to each individual solutions..*/
total += 100;
cout << "Correct!" << "You get " << total << " points!";
}
else {
cout << "Game Over!" << " You get " << total << " points!" << endl;
}//my code does not compile because it says line 49, "operand types are incompatible("char" and "char")
}
return 0;
}


Much appreciation for all of your support!!
Last edited on
code tags would help.
first thing I see is 'e' does not exist for the last response so they can't win.

I think you also have some bad syntax. Does this thing compile at all? My brain compiler says no but it isn't perfect.

I would put an end of line between question and first choice and each choice. you can chain
cout << question << endl << choices << endl; //start thinking about using '\n' instead of endl unless you need to flush the stream (force immediate write to screen).

here is a way to do it all in one place cleanly:
choices[3] = "\na. Bear\nb: Rabbit\nc: Dragon\nd: Dog\n";

if (userAnswer == solutions) //this is not right.
solutions is an array...

using namespace std is frowned upon. It says not to change it but its a bad habit so be aware that it is not good.

get it so it will compile, repost it in code tags, tell us anything that isn't working from your testing, ask about anything you don't understand, and if you thnk it works and want more advice, we can try again.
Last edited on
Hey nice challenge. I try it too:

PS: Your Code does not compile.

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
#include <iostream>
#include <array>
#include <string>
#include <vector>

struct Card {
    std::string question;
    std::array<std::string,4> answers;
    char solution=0;
    double points=0;
};

int main(){
    std::vector<Card> cards;
    cards.push_back(Card{"How many rings are there on the Olympic flag?", {"0","4","5","7"}, 'c', 100.0});
    cards.push_back(Card{"How many holes are on a standard bowling ball?", {"2","3","5","10"}, 'b', 200.0});
    cards.push_back(Card{"How many points is the letter X worth in English-language Scrabble?", {"0","8","10","11"}, 'c', 300.0});
    cards.push_back(Card{"Which of these animals does NOT appear in the Chinese zodiac?", {"Bear","Rabbit","Dragon","Dog"}, 'd', 400.0});
    cards.push_back(Card{"What is a pomelo?", {"An old-fashioned punching bag","A breed of dog","The largest citrus fruit","Something that cheerleaders hold"}, 'c', 500.0});

    double totalPoints = 0;

    for(const Card& card : cards) {
        std::cout << card.points << " points question:\n";
        std::cout << card.question << "\n";
        std::cout << "a: " << card.answers[0] << "\n";
        std::cout << "b: " << card.answers[1] << "\n";
        std::cout << "c: " << card.answers[2] << "\n";
        std::cout << "d: " << card.answers[3] << "\n";
        cout << "Enter your answer in lower case letter: " << endl;
        char userAnswer;
        std::cin >> userAnswer;

        if(userAnswer == card.solution) {
            totalPoints += card.points;
            std::cout << "Correct! You get " << card.points << " points!\n";
        } else {
            std::cout << "Wrong!\n";
            break;
        }
        std::cout << "\n\n";
    }

    std::cout << "You get total of " << totalPoints << " points!\n";

    return 0;
}

Yes, my code still does not compile...
Well then, that should be your first task. Look at the compiler errors, understand what it is they are telling you about the mistakes in your code, and then fix those mistakes.
Fix 3 things first and then re-post in code tags.

(1) Initialisation of arrays questions[], choices[], solutions[] is wrong. Just give values in these initialisations; e.g.
1
2
3
4
5
6
7
string choices[5]{
"\na.0\nb.4\nc.5\nd.7",
"\na. 2\nb. 3\nc. 5\nd. 10",
"\na. 0\nb. 8\nc. 10\nd. 11",
"\na. Bear\nb: Rabbit\nc: Dragon\nd: Dog",
"\na. An old-fashioned punching bag\nb. A breed of dog\nc.The largest citrus fruit\nd. Something that cheerleaders hold"
};

You fix questions[] and solutions[] likewise.

(2) questions[] and choices[] are arrays, so line ... whatever-it-would-be-if-you-used-code-tags ... should be
cout << questions[i] <<endl<< choices[i] << endl;

(3) Solutions[] is also an array, so you need to compare userAnswer to a SINGLE ELEMENT of it on line ... another-line-not-numbered-because-you-didn't use-code-tags:
if (userAnswer== solutions[i])

Try those.


PLEASE USE CODE TAGS
Registered users can post here. Sign in or register to post.