Small mistake somewhere

This doesnt execute properly. It executes the exp gain no matter if you got it wrong or right.

1
2
3
4
5
6
7
if (answer1==1){
                     exp=500+1000;  
                     cout << "Gained 500 exp!" << endl;
                     cout << exp << endl;
                     }else{
                           cout << "Incorrect!"<< endl;
                           };


Lemme know if you'll need the entire code but i think i jus wrote the if statemnet wrong.

Last edited on
I just made this code, just to try the if statement, and I input answer1 '1' the first time and '2' the second time and it worked just fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
void main()
{
	int answer1;
	int exp;
	cin>>answer1;
        if (answer1==1){
                     exp=500+1000;  
                     cout << "Gained 500 exp!" << endl;
                     cout << exp << endl;
                     }else{
                           cout << "Incorrect!"<< endl;
                           };
}


BTW I use Microsoft Visual Studio 6.0. Not sure if it makes a difference though :D
Last edited on
That does help because i now know that its not my if statement thats messed up, its the rest of my code, so all the help is appriciated on this one. its a baby code im tryin to right up real fast.

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
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main(){
    
    int answer1;
    char name[20];
    int hp=100;
    int exp=1000;
    int xparray[10];
    int level=1;
    
    
      
    cout << "Name" << endl;
    cin >> name;
    cout << "Welcome!"<< endl;
    cout << "HP:";
    cout << hp << endl;
    cout << "Exp:";
    cout << exp << endl;
    cout << "This is the world of the programmer of this game. Unknown to even me!" << endl;
    cout << "In this game, you as the begginer must answer general knowledge questions to level, and meet your maker!" << endl;
    cout << "Right answers yield experience!! Play wisely...." << endl;
    cout << "Question 1:";
    cout << "In 1492 Columbus sailed the ocean ____" << endl;
    cout << "1.Blue" << endl;
    cout << "2.Yellow" << endl;
    cout << "3.Purple" << endl;
    cout << "4.I never went to school....ever!" << endl;
    cin >> answer1;
    
    if (answer1==1){
                     exp=500+4500;  
                     cout << "Gained 500 exp!" << endl;
                     cout << exp << endl;
                     }else{
                           cout << "Incorrect!"<< endl;
                           };
                 
    // Experience Array
    xparray[1]=5000;
    xparray[2]=10000;
    xparray[3]=25000;
    xparray[4]=100000;
    xparray[5]=275000;
    xparray[6]=400000;
    xparray[7]=550000;
    
    for (int exp=5000; level < 1; level++) {
        cout << "You have reached level 2!" << endl;
        cout << level; 
        };
    for (int exp=10000; level < 2; level++) {
        cout << "You have reached level 3!" << endl;
        cout << level;
        };
    for (int exp=25000; level < 3; level++) {
        cout << "You have reached level 4!" << endl;
        cout << level;
        };
    for (int exp=100000; level < 4; level++) {
        cout << "You have reached level 5!" << endl;
        cout << level;
        };          
    system("pause");
    return 0;
    
};


The array at this point and time im still wondering if i want to use so im trying different methods. this would be the first(the if statement i mean) and then i will try the array, see if thats an easier approach.

So did i forget a ';' or somethin.....from wat i can tell the code jus contiues to execute the exp=500+100 but not the cout's. so when i run it it comes out

Incorrect!
1500(curent exp after calc which shouldnt happen)

and i did a check on my for statements and thats screwy to.
comes out the same ass above but jus replace 1000 with 4500 and no matter what it executes all for statements

Incorrect!
10500
2You have Reached lvl 2
3You have reached lvl 3
4You have reached lvl 4

EDIT: Will someone please help :(
Last edited on
Well the problem is in your for statements.
I am not sure, but I don't think you can say

for (int exp=100000; level < 4; level++)

This means if exp was 100000 and level was less than 4, increase the level. I don't think you can say
for(x=5;y<4;y++)

You can say this

1
2
3
4
5
6
for(x=5;x<y;x++)

//OR..

for(x=5;x<10;x++)
   for(y=5;y<10;y++)


But you can't include two variables in the same for loop unless it was like the x<y thing.

comes out the same ass above but jus replace 1000 with 4500 and no matter what it executes all for statements


Yes. I think it's for the reason I mentioned above, also because in the for loops, you have initialized the exp variable in all for loops after you have declared it with the rest of the variables in the beginning of the program. Remove the int inside the for loops, or ever remove the whole for loop, because I don't understand what a for loops is for over here? I mean you can do this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (answer1==1)
{
    exp = exp +500; //Or exp+=500;
    cout << "Gained 500 exp!" << endl;
    cout << exp << endl;
}
else
cout << "Incorrect!"<< endl;

//...
//...

//Instead of the for loops do this for increasing levels.

if(exp>=1000)
{
        level++;
        cout << "You have reached level 2!" << endl;
}

//...
//...

//Do the same for all the other levels!! 


Try this and see if it helps :D
Last edited on
Well the problem is in your for statements.
I am not sure, but I don't think you can say

for (int exp=100000; level < 4; level++)

This means if exp was 100000 and level was less than 4, increase the level.


Well, actually that means "create an int called exp, set it to 100000, then if level is less then 4, do stuff and increment level."

And as hannad said, your addition to exp is really screwed up...did you even test this in your head...?
@LovetoCpp

there seems to be a lot of problems in the design I think. Especially with the for loops, The main reason we use loops/iterations are for repetitive tasks. I'm not sure what you're actually trying to do but it seems like you're trying to check the xp then say what level the user is at. So perhaps if statements are better suited for the job? And another note you don't need semi-colons after the for loops or if statements. The xp array wasn't used as well. :P

@hannad

But you can't include two variables in the same for loop unless it was like the x<y thing.

Actually you can use multiple variables in for loops, it's not really used much however.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <limits>

int main()
{
    for(int i = 0, j = 0; i < 10; i++, j+=2)
    {
         std::cout << "i : " << i << std::endl;
         std::cout << "j : " << j << std::endl;
    }

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}
Last edited on
Ok.
But you can't do as he did. You put two variables, put the increment for both and put the condition for both. He put the 1st variable. then the condition and increment was for another variable. That was what I was talking about. Thanks though.
Topic archived. No new replies allowed.