Need help with "Calling"

Started by
7 comments, last by Khatharr 10 years, 10 months ago

Hi guys (:

I'm kinda new to C++ , just started today so bare with me d;

I made a Calculator from a tutorial, and after that i used my knowledge of python and normal logic to write this piece of code.


#include <string>
#include <iostream>
using namespace std;


int main()
{

	string lBrugerNavn, lPassword;
	cout << "Brugernavn: ";
	cin >> lBrugerNavn; 
	cout << endl;
	cout << "Password: ";
	cin >> lPassword;
	if (lBrugerNavn == "Jokke"
		&& lPassword == "1234")
	{
		system("CLS");
		menu();
	}
	else 
	{
		cout << "Bad Logon. Try again!" << endl;
		system("CLS");
		main();
	}
	cin.get();
	cin.get();


}

int menu()
{
	string lBrugerNavn, Choice;
	cout << "Welcome" << lBrugerNavn;
	cout << endl;
	cout << endl;
	cout <<"1 - Calculator" << endl;
	cout << endl;
	cout <<"2 - Random Option" << endl;
	cout << endl;
	cout <<"3 - Random Option" << endl;
	cout << endl;
	cout <<"4 - Random Option" << endl;
	cout << endl;
	cin >> Choice;
	if (Choice == 1)
		claculator();
	else if (Choice == 2)
		randomoption();
}

int calculator()
{

}

I know that the top part is working, but my problem is "Calling" the "Menu" after login passed.

i dont really know if my menu is gonna work yet, cos i cant relly test it before getting the "Calling" working d:

I hope there is somebody out there with a fix, i'm on a steep learning curve here :D

Advertisement

The compiler needs to know what 'menu()' is. Basically you 'forward declare' it before your main() function. Something like "int menu();" before the "int main() { ... }" should do the trick. Same goes for the other functions that you call before they appear in the source code.

edit: this should probably be in the 'for beginners' forum.


edit: this should probably be in the 'for beginners' forum.

Thanks for you post (:
And yeah maybe, though the beginner forum was more generel question than coding questions, and didn't wanna place it in the wrong forum d;

And well, i'm not rly sure i understand what you mean with the "Calling" can you make a quick line of code, with your ex. or something? Thanks a lot on advance mate (:

Copy the menu function above the main function. Basically the compiler is starting at the very top of your file... coming down to main, then seeing the call to menu() and going "I have no idea what this function is", if you had the menu function at the top of the page (below the includes and namespace) then the compiler would have already seen the function before it got to it in main, and therefore it would know what it was.

bradbobak suggested just writing the line "int menu();" before main(), that would say to the compiler "There is a function called menu, I'm not going to tell you what it does yet, but just know if you see it in main() then all will be explained later on." smile.png

Also you may want to correct the spelling of 'calculator()' after (Choice==1) in the menu function.


Copy the menu function above the main function. Basically the compiler is starting at the very top of your file... coming down to main, then seeing the call to menu() and going "I have no idea what this function is", if you had the menu function at the top of the page (below the includes and namespace) then the compiler would have already seen the function before it got to it in main, and therefore it would know what it was.

Thanks alot man! :D


Also you may want to correct the spelling of 'calculator()' after (Choice==1) in the menu function.

Haha i will d;

You are still calling main() from inside main(). That should be an error - it is illegal in C++ to call main recursively. If you want the stuff inside main to repeat endlessly wrap it in a for(;;) or while(true) loop.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

You are still calling main() from inside main(). That should be an error - it is illegal in C++ to call main recursively. If you want the stuff inside main to repeat endlessly wrap it in a for(;;) or while(true) loop.

I was pretty impressed by the recursion there. If you fail the logon repeatedly it continues to accumulate cin.get() calls for when the program finally exits.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

You are still calling main() from inside main(). That should be an error - it is illegal in C++ to call main recursively. If you want the stuff inside main to repeat endlessly wrap it in a for(;;) or while(true) loop.

I was pretty impressed by the recursion there. If you fail the logon repeatedly it continues to accumulate cin.get() calls for when the program finally exits.

Well you could throw and catch an exception to avoid that ;) A DoS attack is going to make the stack overflow though ;)

The reason you aren't allowed to call main recursively is because main can do extra work such as call constructors for global/static objects if the compiler implementer feels like it... all compilers I have used do that before main is actually called though.

It's allowed in C unless they have also banned it now.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Well you could throw and catch an exception to avoid that ;)


I'd prefer a goto. <3
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement