Loading a Function that is Before a Function

What I want to do is make this work:

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
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;

int ending()
{
	int pin2;
	cin >> pin2;

	if (pin2 == 1)
		{
			guessing();
		}
	
	return 0;
}

int guessing()
{
	cout << "Guess one number between 1 and 3\n";
	
	int guess;
	guess = rand() % 3+1;
	
	int pin;
	cin >> pin;

	if (pin == guess)
		{
			cout << "You did it!\n"
				<< "Type <1> if you want to play again\n";
				ending();
		}
	else if (pin != guess)
		{
			cout << "You FAIL!\n"
				<< "Type >1< if you want to play again\n";
				ending();
		}

	return 0;
}

int main()
{
	srand((unsigned int)time(0));
	
	cout << "This is the guess a number game!\n";

	guessing();

	return 0;
}


I want the "guessing()" to go to "ending()", but since it is before the guessing part, it can't go there. I tried putting "guessing()" after "ending()", but that wouldn't work also.
Anybody know a functioning solution?
Last edited on
Since both functions need to use each other, you can't achieve this by just rearranging them, you can however declare a function without giving the full definition

1
2
3
4
5
6
7
8
9
10
11
int guessing(); // declare the function

int ending() // now ending knows about guessing
{
    //....
}

int guessing() // and of course guessing knows about ending as the full definition is above
{
    //....
}
Last edited on
Alright, thanks!
Aka prototyping, read under Declaring Functions @ http://www.cplusplus.com/doc/tutorial/functions2/
Topic archived. No new replies allowed.