Thread: call the function but cannot run

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    48

    call the function but cannot run

    Code:
    
    
    Code:
    void additionmenu()
    {
        int choice;
        printf("Please select the level\n");
        printf("1 level 1\n");
        printf("2 level 2\n");
        printf("3 level 3\n");
        scanf("%d", &choice);
        if (choice == 1)
        {
            printf("You have selected level 1\n");
            addEasy();
        }
        else if (choice == 2)
        {
            printf("You have selected level 2\n");
            addMedium();
        }
        else if (choice == 3)
        {
            printf("You have selected level 3\n");
            addDifficult();
        }
        else
        {
            printf("Invalid choice\n");
            menu();
        }
    }
    
    
    
    here my menu
    int main()
    {
    	int choice = menu();
    	printf("You have selected the choice %d\n", choice);
    
    
    	if (choice == 1)
    	{
    		printf("You have selected addition\n");
    		additionmenu();
    	}
    	else if (choice == 2)
    	{
    		printf("You have selected subtraction\n");
    		subtractionmenu();
    	}
    	else if (choice == 3)
    	{
    		printf("You have selected multiplication\n");
    		multiplicationmenu();
    	}
    	else if (choice == 4)
    	{
    		printf("You have selected division\n");
    		divisionmenu();
    	}
    	else
    	{
    		printf("Invalid choice\n");
    	}
    	system("pause");
    }
    
    
    int menu()
    {
    	int choice;
    	printf("1. Addition\n");
    	printf("2. Subtraction\n");
    	printf("3. Multiplication\n");
    	printf("4. Dividion\n");
    	printf("Please choose either 1,2 ,3 or 4 :\n");
    	scanf_s("%d", &choice);
    	(getchar() != '\n');
    	return choice;
    }

    after invalid i call the menu and menu can display but when i choose i show nothing, cannot continue to do

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to have a loop somewhere, if you want the option of going round again.
    Code:
    int main ( ) {
      int choice;
      while ( (choice=menu()) != 5 ) {  // Add "5. Quit" to the menu() function
        printf("You have selected the choice %d\n", choice);
        // etc
      }
    }
    > printf("Invalid choice\n");
    > menu();
    No, you return to main() and let it call menu() again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    thank you for your help ....i appreciate that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  4. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM

Tags for this Thread