Random arithmetic answers.

I'm pretty new to this, and I'm trying to make a simple quiz game. My code is compiling and running exactly how I want it too, apart from line 11(where I assign a value to the 'score integer). It produces a random stupid number, e.g. the last answer was: -394100307. Why is this happening and how do I solve 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
#include <iostream>

using namespace std;
int main()
{
   int one;
   int two;
   int three;
   int four;
   int score=(one+two+three+four);

    cout<<"This quiz will test your knowledge of Disney"<<endl;
    cout<<endl;
    cout<<"What is the name of the Reggae singing Caribbean crab in the Little Mermaid?"<<endl<<"1.Sebastien"<<endl<<"2.Mushu"<<endl<<"3.Tinkerbell"<<endl;
    int a;
    cout<<"Answer 1,2 or 3: ";
    cin>>a;
    cout<<endl;
    cout<<"What does Dumbo hold in his trunk to help him fly?"<<endl<<"1.A banana"<<endl<<"2.Prawn crackers"<<endl<<"3.A magic feather"<<endl;
    int b;
    cout<<"Answer 1,2 or 3: ";
    cin>>b;
    cout<<"What type of animal is a flounder?"<<endl<<"1.A fish"<<endl<<"2.A dodo"<<endl<<"3.A small cat"<<endl;
    int c;
    cout<<"Answer 1,2 or 3: ";
    cin>>c;
    cout<<"Which Disney character sings the song 'Mother knows best"<<endl<<"1.Mulan"<<endl<<"2.Mother Gothel"<<endl<<"3.The witch from Tangled"<<endl;
    int d;
    cout<<"Answer 1,2 or 3: ";
    cin>>d;

if(a==1)
    one=1;
if(b==3)
    two=1;
if(c==1)
    three=1;
if(d==2)
    four=1;
cout<<"Your score is: "<<score<<"/4";
return 0;
}
You haven't initialised any of your variables on lines 6 to 9, so why should those numbers be sensible when you add them up on line 10?

i *think* you need to move your line 10 to under line 39. in other words dont tell it to calculate score until you have sensible values for a, b c and d.

I would however still initialise your variables.
Last edited on
6
7
8
9
10
int one;
int two;
int three;
int four;
int score=(one+two+three+four);

Line 10 does not "bind" the formula score = one+two+three+four to the variable score.
All it does is make score equal to the sum of the current values of one, two, three, and four -- which, as they're uninitialized at this point, is essentially a garbage value.

Move line 10 to right before line 40 and you should be fine.
Moved it to line 39 and it worked, thanks a lot guys :)
Topic archived. No new replies allowed.