My program closes unexpectedly

I built a program that asks for numbers and you have to do tasks with them (add one, double). The first "level" works fine, but the second one wont start. What have I done wrong?
This is the related code:
cout<< "The Rules of the Game are: enter the number equal to the number of times you have been asked to input a number. Start with 2\n";
do
{
cout << "Enter the number\n";
cin>> urnumber;
thenumber++;
}
while (urnumber==thenumber && thenumber<14);
if (urnumber!=thenumber)
{
cout<< "Game Over! You Lose";
cin.ignore ();
}
else if (thenumber>=15)
{
cout<< "Congratulations You've passed level 1! Now you have to enter the number double the number of times you've been asked! The current number is 15!";
int level2;
}



and this is the function level2:



int level2 ()
{
int doubler=15;
int rightanswer=30;
do
{
std::cout<< "Enter The Number";
std::cout;
rightanswer=thenumber+doubler;
thenumber++;
doubler++;
}
while (urnumber==rightanswer && rightanswer<50);
return 0;
}

You have to call the function. Here is how to call it:

level2();

Here is NOT how to call it:
int level2;
Oh thanks! crap i shoulda seen this XD
It still doesnt work tho...
ill put in all the code

#include <iostream>

int go_to;
int thenumber=1;
int urnumber;

int level2 ()
{
int doubler=15;
int rightanswer=30;
do
{
std::cout<< "Enter The Number";
std::cout;
rightanswer=thenumber+doubler;
thenumber++;
doubler++;
}
while (urnumber==rightanswer && rightanswer<50);
return 0;
}
using namespace std;
int main ()
{
int doubler=15;
int rightanswer=30;
cout<< "The Rules of the Game are: enter the number equal to the number of times you have been asked to input a number. Start with 2\n";
do
{
cout << "Enter the number\n";
cin>> urnumber;
thenumber++;
}
while (urnumber==thenumber && thenumber<15);
if (urnumber!=thenumber)
{
cout<< "Game Over! You Lose";
cin.ignore ();
}
else if (thenumber=15)
{
cout<< "Congratulations You've passed level 1! Now you have to enter the number double the number of times you've been asked! The current number is 15!";
level2 ();
}
if (urnumber!=rightanswer)
{
cout<< "Game Over! You Lose!";
cout<< "Press Enter To Exit...";
cin.ignore();
}
else
{
cout<< "You Win! More Levels Coming Soon!";
cout<< "Press Enter to Exit..";
cin.ignore();
}
return 0;
}


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
#include <iostream>

int go_to;
int thenumber=1;
int urnumber;

int level2 ()
{
    int doubler=15;
    int rightanswer=30;
    do
    {
        std::cout<< "Enter The Number";
        std::cout;
        rightanswer=thenumber+doubler;
        thenumber++;
        doubler++;
    }
    while (urnumber==rightanswer && rightanswer<50);
    return 0;
}

using namespace std;

int main ()
{
    int doubler=15;
    int rightanswer=30;
    cout<< "The Rules of the Game are: enter the number equal to the number of times you have been asked to input a number. Start with 2\n";
 
   do 
    {
        cout << "Enter the number\n";
        cin>> urnumber;
        thenumber++;
    }
    while (urnumber==thenumber && thenumber<15);

    if (urnumber!=thenumber)
    {
        cout<< "Game Over! You Lose";
        cin.ignore ();
    }
    else if (thenumber=15)
    {
        cout<< "Congratulations You've passed level 1! Now you have to enter the number double the number of times you've been asked! The current number is 15!";
        level2 ();
    }

    if (urnumber!=rightanswer)
    {
        cout<< "Game Over! You Lose!";
        cout<< "Press Enter To Exit...";
        cin.ignore();
    }
    else 
    {
        cout<< "You Win! More Levels Coming Soon!";
        cout<< "Press Enter to Exit..";
        cin.ignore();
    }
    return 0;
}


Code tag please!

What error did you get? can you post it here?
It will be useful to locate the error in your code.
Last edited on
else if (thenumber=15)?
2 things:

1- when you have this
 
using namespace std;

put it after the #include so that you don't have to call std:: so many times

2- I dont understand the use of level2()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int level2 ()
{
    int doubler=15;
    int rightanswer=30;
    do
    {
        std::cout<< "Enter The Number";
        std::cout;
        rightanswer=thenumber+doubler;
        thenumber++;
        doubler++;
    }
    while (urnumber==rightanswer && rightanswer<50);
    return 0; //this function will always return a value: 0
}


level2() always return 0, is that what you wanted? if you want to print and do things, you could've used void function instead of having a int value return function
Topic archived. No new replies allowed.