![]() |
Returning to the main function from another
As the tittle says i want my program to return to the main() from another function if a certain condiction is true.Here is the code:
[code=c++] #include <iostream> #include <cmath> #include <stdlib.h> using namespace std; void Sobiranje() { float v_broj,rezultat=0; system("cls"); cout<<endl; cout<<" ---------------------------------"<<endl; cout<<" ***** Marijan's Claculator *****"<<endl; cout<<" ---------------------------------"<<endl; cout<<endl; cout<<endl; cout<<endl; float broj,a,broj_nasob; cout<<"Insert the numbers you would like me to calculate,to return to the main menu press 0 :"<<endl; while(broj!=0) { cin>>v_broj; if(v_broj==0) //If the number you insert is 0 return main();//Get back to the main function to start it all over else rezultat=rezultat+v_broj; cout<<"Rezultatot e :"<<rezultat<<endl; return main(); } int main() { int Izbor=0; cout<<endl; cout<<" ---------------------------------"<<endl; cout<<" ***** Marijan's Calculator *****"<<endl; cout<<" ---------------------------------"<<endl; cout<<endl; cout<<endl; cout<<" *-------------------------------------------------*"<<endl; cout<<" |1.For addiction of two or more numbers select 1|"<<endl; cout<<" |2.For subtraction select 2| "<<endl; cout<<" |3.For multipication select 3|"<<endl; cout<<" |1.For division select 4|"<<endl; cout<<" *-------------------------------------------------* "<<endl; cout<<"Izbor :"<<endl; cin>>Izbor; if(Izbor==1) Sobiranje(); system("PAUSE"); return 0; } [\code] But some problems appear when i try to compile it,first it says to use the main function first and i do so,then it says something else.I am a tottal begginer in C++ so don't get angry if i have mistaken some simple things.Thanks in advance. |
Re: Returning to the main function from another
In your
Sobiranje() function, to return to main(), just replace your return main() code (which makes absolutely no sense :)) with a return statement.If you'd like to know why, I (or someone else I'm sure) would be happy to explain. |
Re: Returning to the main function from another
You can't return whole functions in C++. You can return pointer to functions, but that is a whole different story.
|
Re: Returning to the main function from another
Quote:
|
Re: Returning to the main function from another
Quote:
:
#include <cstdio> |
Re: Returning to the main function from another
The best analogy I can come up with regarding this is like digging a hole.
When you call a function, you dig a hole into the ground. To get back to where you started, you have to climb out of the hole. For example, :
output :
Hello. This is the first function.Makes sense? But what happens when there is a second function (we dig even deeper)? :
output :
Hello. This is the main function.Does that help at all? |
Re: Returning to the main function from another
Thanks for the detailed explanation it really helped me to understand it better so i reconstructed my code and compiled it again so it would be like this,the whole program is not written,but this is the important part i need help on.As you can see i changed using the advice i got but it won't return me to the main function when i say so.
:
And yet it won't return me to the original menu,instead when i press 0,press any key to continue appears. P.C:Sorry for being annoying. |
Re: Returning to the main function from another
Quote:
|
Re: Returning to the main function from another
:
The problem you were having is that when your Sobiranje() function was returning to main(), the program quickly finished because there was nothing else to do. What I've done in the above is inserted a while loop that runs forever. When you return from the function, the while loop will make the program execute from the beginning of the loop again. |
Re: Returning to the main function from another
Thanks again,you helped me a lot :D
|
| All times are GMT -5. The time now is 9:28 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC