Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Returning to the main function from another (http://www.programmingforums.org/showthread.php?t=15700)

Marijan Apr 26th, 2008 7:06 AM

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.

Jessehk Apr 26th, 2008 7:50 AM

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.

Soulstorm Apr 26th, 2008 10:11 AM

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.

Marijan Apr 26th, 2008 11:25 AM

Re: Returning to the main function from another
 
Quote:

Originally Posted by Jessehk (Post 144364)
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.

Yes please,i would be more then grateful,afterall i am a student and i'd like to learn as much as possible.

peaceofpi Apr 26th, 2008 3:00 PM

Re: Returning to the main function from another
 
Quote:

Originally Posted by Soulstorm (Post 144370)
You can't return whole functions in C++. You can return pointer to functions, but that is a whole different story.

Do I misunderstand you? 'Cause this works.

:

#include <cstdio>
int foo() { return 0; }
int bar() { return foo(); }
int main() {
    printf("%d",bar());
    return bar();
}


Jessehk Apr 26th, 2008 3:27 PM

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

  1. #include <iostream>
  2.  
  3. void first() {
  4.     std::cout << "Hello. This is the first function." << std::endl;
  5.  
  6.     // We are returning back to where we started (climbing back out of the hole)
  7.     return;
  8. }
  9.  
  10. int main() {
  11.     // Dig the hole
  12.     first();
  13.  
  14.     std::cout << "Hello. This is the main function." << std::endl;
  15. }


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)?
:

  1. #include <iostream>
  2.  
  3. // Declarations so that the definitions can be in an order that
  4. // clearer for the purposes of this example.
  5. void first();
  6. void second();
  7.  
  8. int main() {
  9.     std::cout << "Hello. This is the main function." << std::endl;
  10.  
  11.     // Dig the hole
  12.     first();
  13.  
  14.     std::cout << "Hello again. We're back in the main function." << std::endl;
  15. }
  16.  
  17. void first() {
  18.     std::cout << "Hello. This is the first function." << std::endl;
  19.  
  20.     // Dig the hole even deeper.
  21.     second();
  22.  
  23.     // After climbing out of the hole from the second function,
  24.     // we're back in the first.
  25.     std::cout << "Hello again. We're back in the first function." << std::endl;
  26.  
  27.     // Now, we climb out of the hole we made by this function
  28.     // and return to main().
  29.     return;
  30. }
  31.  
  32. void second() {
  33.     std::cout << "Hello. This is the second function." << std::endl;
  34.  
  35.     // Climb back out of the hole and return to the first function
  36.     // where we started this hole.
  37.     return;
  38. }


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?

Marijan Apr 26th, 2008 3:56 PM

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.

:

  1.  
  2.  
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <stdlib.h>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. void Sobiranje()
  12. {
  13. float v_broj,rezultat=0;
  14.  
  15. system("cls");
  16.  
  17. cout<<endl;
  18. cout<<" ---------------------------------"<<endl;
  19. cout<<" ***** Marijan's Claculator *****"<<endl;
  20. cout<<" ---------------------------------"<<endl;
  21. cout<<endl;
  22. cout<<endl;
  23. cout<<endl;
  24.  
  25. float broj,a,broj_nasob;
  26. cout<<"Insert the numbers you would like me to calculate,to return to the main menu press 0 :"<<endl;
  27.  
  28. while(broj!=0)
  29. {
  30. cin>>v_broj;
  31. if(v_broj!=0) //If the number you insert is not 0
  32. rezultat=rezultat+v_broj;//keep adding them all
  33. else    //if it is 0
  34. return; //Get back to the main function to start it all over
  35.  
  36.  
  37.  
  38. cout<<"Rezultatot e :"<<rezultat<<endl;
  39. }
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. int Izbor=0;
  46.  
  47.  
  48.  
  49. cout<<endl;
  50. cout<<" ---------------------------------"<<endl;
  51. cout<<" ***** Marijan's Calculator *****"<<endl;
  52. cout<<" ---------------------------------"<<endl;
  53.  
  54. cout<<endl;
  55. cout<<endl;
  56. cout<<" *-------------------------------------------------*"<<endl;
  57. cout<<" |1.For addiction of two or more numbers select 1|"<<endl;
  58. cout<<" |2.For subtraction select 2| "<<endl;
  59. cout<<" |3.For multipication select 3|"<<endl;
  60. cout<<" |1.For division select 4|"<<endl;
  61. cout<<" *-------------------------------------------------* "<<endl;
  62. cout<<"Izbor :"<<endl;//Means "choice"
  63. cin>>Izbor;//enter your choice
  64. if(Izbor==1)//if you choose 1
  65. Sobiranje();//run the Sobiranje function(addition)
  66.  
  67.  
  68. system("PAUSE");
  69. return 0;
  70.  
  71.  
  72. }



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.

Benoit Apr 26th, 2008 7:07 PM

Re: Returning to the main function from another
 
Quote:

Originally Posted by peaceofpi (Post 144385)
Do I misunderstand you? 'Cause this works.

:

#include <cstdio>
int foo() { return 0; }
int bar() { return foo(); }
int main() {
    printf("%d",bar());
    return bar();
}


In that code snipper, an integer value is being returned. I think Soulstorm was referring to returning a chunk of data that can be executed somehow.

Jessehk Apr 27th, 2008 10:13 AM

Re: Returning to the main function from another
 
:

  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. void Sobiranje()
  9. {
  10.     float v_broj,rezultat=0;
  11.  
  12.     system("cls");
  13.  
  14.     cout<<endl;
  15.     cout<<" ---------------------------------"<<endl;
  16.     cout<<" ***** Marijan's Claculator *****"<<endl;
  17.     cout<<" ---------------------------------"<<endl;
  18.     cout<<endl;
  19.     cout<<endl;
  20.     cout<<endl;
  21.  
  22.     float broj,a,broj_nasob;
  23.     cout<<"Insert the numbers you would like me to calculate,to return to the main menu press 0 :"<<endl;
  24.  
  25.     while(broj!=0)
  26.         {
  27.             cin>>v_broj;
  28.             if(v_broj!=0) //If the number you insert is not 0
  29.                 rezultat=rezultat+v_broj;//keep adding them all
  30.             else    //if it is 0
  31.                 return; //Get back to the main function to start it all over
  32.  
  33.  
  34.  
  35.             cout<<"Rezultatot e :"<<rezultat<<endl;
  36.         }
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.     int Izbor=0;
  43.     while ( true ) {
  44.         cout<<endl;
  45.         cout<<" ---------------------------------"<<endl;
  46.         cout<<" ***** Marijan's Calculator *****"<<endl;
  47.         cout<<" ---------------------------------"<<endl;
  48.  
  49.         cout<<endl;
  50.         cout<<endl;
  51.         cout<<" *-------------------------------------------------*"<<endl;
  52.         cout<<" |1.For addiction of two or more numbers select 1|"<<endl;
  53.         cout<<" |2.For subtraction select 2| "<<endl;
  54.         cout<<" |3.For multipication select 3|"<<endl;
  55.         cout<<" |1.For division select 4|"<<endl;
  56.         cout<<" *-------------------------------------------------* "<<endl;
  57.         cout<<"Izbor :"<<endl;//Means "choice"
  58.  
  59.         cin>>Izbor;//enter your choice
  60.         if(Izbor==1)//if you choose 1
  61.             Sobiranje();//run the Sobiranje function(addition)
  62.  
  63.     }
  64.  
  65.     system("PAUSE");
  66.     return 0;
  67.  
  68.  
  69. }


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.

Marijan Apr 27th, 2008 12:28 PM

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