View Single Post
Old Apr 27th, 2008, 10:13 AM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
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