Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need help with "Calling"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 WunderStrudel   Members   -  Reputation: 156

Like
0Likes
Like

Posted 17 June 2013 - 12:14 AM

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 



Sponsor:

#2 bradbobak   Members   -  Reputation: 1022

Like
0Likes
Like

Posted 17 June 2013 - 12:26 AM

 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.


Edited by bradbobak, 17 June 2013 - 12:28 AM.


#3 WunderStrudel   Members   -  Reputation: 156

Like
0Likes
Like

Posted 17 June 2013 - 10:01 AM


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 (: 



#4 Godmil   Members   -  Reputation: 740

Like
0Likes
Like

Posted 17 June 2013 - 10:26 AM

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.


Edited by Godmil, 17 June 2013 - 10:28 AM.


#5 WunderStrudel   Members   -  Reputation: 156

Like
0Likes
Like

Posted 17 June 2013 - 11:44 AM


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; 



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5150

Like
1Likes
Like

Posted 17 June 2013 - 12:13 PM

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

#7 Khatharr   Crossbones+   -  Reputation: 2852

Like
0Likes
Like

Posted 17 June 2013 - 09:35 PM

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.

#8 Paradigm Shifter   Crossbones+   -  Reputation: 5150

Like
0Likes
Like

Posted 17 June 2013 - 09:42 PM

 

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

#9 Khatharr   Crossbones+   -  Reputation: 2852

Like
0Likes
Like

Posted 18 June 2013 - 01:42 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS