Hi folks!

I'm not a student, so this isn't a homework assignment, I'm just trying to learn C++ for my own benefit and my career. My question is: is it possible to call a function that's below another function? If not, how could I do this? For example, in the code I posted below, I want my "attack()" to call "options()" after the cat defeats a marble. The thing is, I have "options()" below "attack()". I know, this is trivial, but I'm just trying to learn this. I know the code is a mess 'n stuff...I'm just looking for advice on how to do this. Thank you!

#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;


int attack()
{
	/* 
	Declare variables
	So far, we're declaring:
	Kitty cat's health
	Enemy's health
	The Nth Turn
	How hard kitty hits
	How much damage Kitty takes
	*/
	int health;
	int enemy;
	int turn;
	int dmghit = 100;
	int dmgrecvd;

	srand(unsigned (time(NULL)));
	
	health = 99;
	enemy = rand() % 100-1;
	cout << enemy;
	for(turn=0;health>enemy;turn++)
	{
		Sleep(500);
		cout << "Kitty attacks a marble for " << dmghit << "points\n";
		enemy = enemy - dmghit;
		if (enemy<=0)
		{
			cout << "On the " << turn << "try\n";
			cout << "You defeated the marble!!!";
			cout << "Kitty receives 89 exp!";
			options();
		}
	}
	Sleep(1000);
cout << "Space filler...I guess we can exit here";
return 0;
}

int options()
{
	int choice;
	cout << "\tOptions:\n1) Attack\n2) Exit\n";
	cout << "COMMAND? \n";
	cin >> choice;
	
switch(choice)
	{
	case 1:
		attack();
		break;
	case 2:
		return 0;
		break;
	default:
		cout << "Select an option dude";
		options();
	}
return 0;
}

int main()
{
cout << "WELCOME!!! It's Thriller the Kitty Kat!\n";
cout << " (\\~/)  '\n";
cout << " ). .(  ()\n";
cout << "(-(Y)-) ))\n";
cout << " )  (  //\n";
cout << "(    )//\n";
cout << "( | | )\n";
cout << "(m   m)\n";
cout << "_______\n";
Sleep(2000);
options();

return 0;
}

Recommended Answers

All 3 Replies

you can use a prototype. In c++ a prototype is declared at the top of your program, outside of any functions. the prototype is the return type of a function, the name of the function and any arguments that get passed to the function. the prototypes for your program would look like this:

#include <iostream>
#include <time.h>
#include <windows.h>

using namespace std;


int options();
int attack();

//rest of code here

This Wikipedia article should help as well, as would doing a Google search for "c++ function prototype."

mattwaab;

Maat, thank you sir. That worked great. Didn't know what a Prototype was until this thread. Thanks again!

glad to help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.