Thread: Problems calling functions

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    Problems calling functions

    Hi all,

    I have a program that has a basic menu which uses a switch statement.

    Option 1 - Allows the user to print a set of numbers on the screen and then returns to the menu.

    Option 2 - Allows the user to print the same set of numbers to an external text file and then returns to the menu.

    Option 3 - Allows the user to print the numbers to both the screen and the text file and then returns to the menu.

    All options are calling a separate function within the program. Options 1 and 2 are working fine. The problem i have is with option 3. I have got it working by copying and pasting the code from the functions for options 1 and 2 into the function for option 3, but i know this is not the ideal way of doing it.

    I have tried having the option 3 selection in the switch statement call the function for option 1 and the function for option 2, but what it does is run the first function and then return to the menu without running the second function.

    Can you please offer me some guidance?

    Thanks
    Barry

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What type of guidance you want from us. Start writing code than expect guidance. . Start writing code dude what r u waitin for

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    The code is all written, i just want to know how i can get option 3 to call both functions instead of just calling and running the first function and then jumping back to the menu.

    will post the code?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Yes, please post the code

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    switch(a)
    {
       case 1: f1();
          break;
       case 2: f2();
          break;
       case 3: f1(); f2();
          break;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Sorry, i was just trying to figure the best way of doing this. Its a college assignment so i removed the code. I was going to PM it to you but there is no PM option available.

    Have you enough information in the following or is there any other way i can send the complete code to you? I dont want to posit it on an open forum as its a college assignment.

    Code:
    void menu2(void)
    {
    void PrintVariances(void) ;
    void OutputVariances(void);
    void BothVariances(void);
    int iChoice;
    puts("\n\n\n\t\t\t\tMENU\n"); 
    puts("\t\t\t1.\tView Variances on the Screen");
    puts("\t\t\t2.\tOutput Variances to Text File");
    puts("\t\t\t3.\tBoth 1 and 2");
    puts("\t\t\t4.\tMain Menu");
    puts("\t\t\t5.\tExit");
    printf("Enter your choice: ");
    scanf_s("%d", &iChoice);
    
    switch (iChoice)
    {
    case 1:
    	PrintVariances();
    	break;
    case 2:
    	OutputVariances();
    	break;
    case 3:
    	PrintVariances();
                    OutputVariances();
    	break;
    case 4:
    	menu1();
    	break;
    case 5:
    	exit(0);
    default:
    	puts("\nPlease select either 1, 2, 3, 4 or 5");
    	menu2();
    } 
    }
    
    
    void PrintVariances(void)
    {
    
    	Code to print the numbers to the screen is here
    
    puts("ENTER to continue");
    	 _getch();
    menu2();
    } 
    
    
    void OutputVariances(void)
    {
    
    Code to open the file and print to it is here
    
    puts("ENTER to continue");
    	 _getch();
    menu2();
    }
    I can pretty much see what the eproblem is but i dont know how to get around it. When i pick option 3 it opens the print variances function, when it gets to the bottom it opens the menu 2 function and then ignores the output variances function.

    I need the program to go back to menu 2 if option 1 is selected and same for option 2 but like i said if i pick option 3 i want it to run both functions.

    Any ideas?

    Thanks
    Last edited by barryr; 12-03-2009 at 05:17 AM.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    If you want the OutputVariances() to be called after PrintVariances() has been called while choosing option 3, you simply need to remove the menu2() call from the PrintVariances() function. I mean, just remove the menu2() from the PrintVariances() function, it'll take you to the immediate next point from where you called PrintVariances().
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Quote Originally Posted by BEN10 View Post
    If you want the OutputVariances() to be called after PrintVariances() has been called while choosing option 3, you simply need to remove the menu2() call from the PrintVariances() function. I mean, just remove the menu2() from the PrintVariances() function, it'll take you to the immediate next point from where you called PrintVariances().
    I only want it to run both Output Variances and Print Variances together when option 3 is selected.

    When i remove the menu2() function like you suggested from the print variances function and select option 1, it prints the variances on the screen, asks me to press enter to continue and then comes up with press any key to continue, and then closes out the program.

    I want the program to run Print Variances() and return to menu 2() if option 1 is selected


    I want the program to run Output Variances() and return to menu 2() if option 2 is selected

    But i want it to run both Print Variances() and Output Variances() and then return to menu 2() if option 3 is selected.

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by barryr View Post
    I only want it to run both Output Variances and Print Variances together when option 3 is selected.

    When i remove the menu2() function like you suggested from the print variances function and select option 1, it prints the variances on the screen, asks me to press enter to continue and then comes up with press any key to continue, and then closes out the program.

    I want the program to run Print Variances() and return to menu 2() if option 1 is selected


    I want the program to run Output Variances() and return to menu 2() if option 2 is selected

    But i want it to run both Print Variances() and Output Variances() and then return to menu 2() if option 3 is selected.
    If I understood you correctly, then this is what you should do. Call menu2() before break statement in option 1, 2 and 3 as well.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You can do one more thing, simply run a loop before printing the menu. This will save you from calling menu2() recursively which will be more efficient.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Quote Originally Posted by BEN10 View Post
    If I understood you correctly, then this is what you should do. Call menu2() before break statement in option 1, 2 and 3 as well.
    Thanks very much.... this worked a treat. Its working exactly as i want it now.

    Thanks again

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    But I would advise you to take my last post into consideration too. It'll make your program more efficient and the hardwork of calling menu2() again and again will also get eliminated.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Quote Originally Posted by BEN10 View Post
    But I would advise you to take my last post into consideration too. It'll make your program more efficient and the hardwork of calling menu2() again and again will also get eliminated.
    Will do. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions between two different projects
    By axr0284 in forum Windows Programming
    Replies: 2
    Last Post: 01-12-2005, 06:58 AM
  2. calling functions from functions
    By algi in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2004, 06:16 PM
  3. Replies: 1
    Last Post: 11-05-2004, 02:05 AM
  4. Problems with functions in a class with pointer to self
    By BigDaddyDrew in forum C++ Programming
    Replies: 6
    Last Post: 02-03-2003, 11:24 PM
  5. problems with functions
    By catalyst in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 06:55 AM