![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Re: Returning to the main function from another
Yes please,i would be more then grateful,afterall i am a student and i'd like to learn as much as possible.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3
![]() |
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.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#5 | |
|
hi: for(;;) goto hi;
|
Re: Returning to the main function from another
Quote:
#include <cstdio>
int foo() { return 0; }
int bar() { return foo(); }
int main() {
printf("%d",bar());
return bar();
}
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4
![]() |
Re: Returning to the main function from another
Quote:
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
|
#7 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
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, c++ Syntax (Toggle Plain Text)
output Hello. This is the first function. Hello. This is the main function. Makes sense? But what happens when there is a second function (we dig even deeper)? c++ Syntax (Toggle Plain Text)
output Hello. This is the main function. Hello. This is the first function. Hello. This is the second function. Hello again. We're back in the first function. Hello again. We're back in the main function. Does that help at all?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
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.
<c++> Syntax (Toggle Plain Text)
And yet it won't return me to the original menu,instead when i press 0,press any key to continue appears. P.C orry for being annoying. |
|
|
|
|
|
#9 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Re: Returning to the main function from another
cpp Syntax (Toggle Plain Text)
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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2008
Posts: 15
Rep Power: 0
![]() |
Re: Returning to the main function from another
Thanks again,you helped me a lot
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Call the class main function | quantalfred | Java | 6 | Jul 23rd, 2006 1:38 PM |
| Recommended Practice for returning data from function | Arla | C# | 1 | Aug 16th, 2005 12:21 PM |
| Returning a value from a variable to the main function | colt | C | 3 | Apr 28th, 2005 7:56 AM |
| Problem when returning a string from a function in VC++ | vjkancha | C++ | 10 | Mar 7th, 2005 1:18 PM |
| Returning An Array From a Function | ViZioN | C++ | 5 | Feb 21st, 2005 6:45 PM |