Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2008, 7:06 AM   #1
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
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.
Marijan is offline   Reply With Quote
Old Apr 26th, 2008, 7:50 AM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 640
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Apr 26th, 2008, 10:11 AM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
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)
Soulstorm is offline   Reply With Quote
Old Apr 26th, 2008, 11:25 AM   #4
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: Returning to the main function from another

Quote:
Originally Posted by Jessehk View Post
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.
Marijan is offline   Reply With Quote
Old Apr 26th, 2008, 3:00 PM   #5
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 87
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Returning to the main function from another

Quote:
Originally Posted by Soulstorm View Post
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();
}
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Apr 26th, 2008, 3:27 PM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 640
Rep Power: 4 Jessehk is on a distinguished road
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)
  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)?
c++ Syntax (Toggle Plain Text)
  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?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 26th, 2008, 3:56 PM   #7
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
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)
  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.Corry for being annoying.
Marijan is offline   Reply With Quote
Old Apr 26th, 2008, 7:07 PM   #8
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4 Benoit is on a distinguished road
Re: Returning to the main function from another

Quote:
Originally Posted by peaceofpi View Post
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.
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Apr 27th, 2008, 10:13 AM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 640
Rep Power: 4 Jessehk is on a distinguished road
Re: Returning to the main function from another

cpp Syntax (Toggle Plain Text)
  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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 27th, 2008, 12:28 PM   #10
Marijan
Newbie
 
Join Date: Feb 2008
Posts: 15
Rep Power: 0 Marijan is on a distinguished road
Re: Returning to the main function from another

Thanks again,you helped me a lot
Marijan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC