Thread: Case: Problem...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17

    Case: Problem...

    Code:
    #include <stdio.h>
    
    void menu();
    void add();
    void substract();
    void multiply();
    void devide();
    void quit();
    
    int main(void)
    {
        menu();
    }
    
    void menu()
    {
         int choice;
         printf("\n############################################\n");
         printf("\n\t\tCalculator\n");
         printf("\n############################################\n\n");
         printf("\n [1] Add\n [2] Substract\n [3] Multiply\n [4] Devide\n [5] Quit\n");
         printf("\n\n\nChoice:  ");
         scanf("%d", &choice);
         
         switch(choice) {
                        case 1:
                             add();
                        case 2:
                             substract();
                        case 3:
                             multiply();
                        case 4:
                             devide();
                        case 5:
                             quit();
                        default:
                                printf("Not a valid entry\n");
                                system("pause");
                                main();
                        }
    }
    
    void add()
    {
         int n1, n2, ans;
         printf("Enter the first number:  ");
         scanf("%d", &n1);
         printf("Enter the second number:  ");
         scanf("%d", &n2);
         ans=n1+n2;
         printf("%d + %d = %d\n", n1, n2, ans);
         system("pause");
         menu();
    }
    
    void substract()
    {
         int n1, n2, ans;
         printf("Enter the first number:  ");
         scanf("%d", &n1);
         printf("Enter the second number:  ");
         scanf("%d", &n2);
         ans=n1-n2;
         printf("%d - %d = %d\n", n1, n2, ans);
         system("pause");
         menu();     
    }
    
    void multiply()
    {
         int n1, n2, ans;
         printf("Enter the first number:  ");
         scanf("%d", &n1);
         printf("Enter the second number:  ");
         scanf("%d", &n2);
         ans=n1*n2;
         printf("%d x %d = %d\n", n1, n2, ans);
         system("pause");
         menu();     
    }
    
    void devide()
    {
         int n1, n2, ans;
         printf("Enter the first number:  ");
         scanf("%d", &n1);
         printf("Enter the second number:  ");
         scanf("%d", &n2);
         ans=n1/n2;
         printf("%d : %d = %d\n", n1, n2, ans);
         system("pause");
         menu();     
    }
    
    void quit()
    {
         int yesno;
         printf("\nAre u sure you want to exit?\n");
         printf("\n[1] Yes\n[2] No\n");
         printf("\n");
         printf("\nChoice:  ");
         scanf("%d", &yesno);
         
         switch(yesno) {
                       case 1:
                            printf("\nGoodbye..\n");
                            system("pause");
                            exit(0);
                       case 2:
                            menu();
                       default:
                               printf("Not a valid entry\n");
                               system("pause");
                               quit();
                       }
                            
    }
    The quit and menu function both have the switch { case 1: } system, when I want to choose an option, in both quit and menu and I input a letter instead of a number it sais Not a valid entry and does the system("pause") thing after that it returns to the function that I defined and it keeps saying the same thing, whatever button I press, it just keeps saying the same thing...

    Btw: Any tips and improvements of my code are welcome .

    Tnx
    Jst.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    you need to "break" out of your case statement at the end of each cases code block or it will just keep executing subsequent lines.
    Code:
    case 2:
      menu();
      break;

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    default:
        printf("Not a valid entry\n");
        system("pause");
        main();
    Do not call main recursively. Since all your other functions seem to call the menu function and that just calls main recursively, you are just asking for trouble. The structure of your program should be closer to the following:

    Code:
    int main(void)
    {
        int choice, yesno = 2;  // Initialize yesno to NO
        
        do
        {
            choice = menu();
            switch( choice )
            {
                case 1:
                    add();
                    break;
                case 2:
                    substract();
                    break;
                case 3:
                    multiply();
                    break;
                case 4:
                    devide();
                    break;
                case 5:
                    yesno = quit();
                    break;
                default:
                    printf("Not a valid entry\n");
                    system("pause");
            }
        } while ( yesno == 2 );
    
        return 0;
    }
    Rely less on recursion to accomplish things and more on looping. Your menu function should simply display the choices and get the users input and return it to the calling function. Each of the other functions should not call the menu function but simply return to the calling function naturally. Your quit function should ask if the user really wants to quit and then get the input from the user and return that choice back to the calling function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    It's not completely working. The choice = menu(); part and the yesno = quit(); part aren't working. It says: "void value not ignored as it ought to be"

    Jst.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it doesn't work. You have your functions all declared void and then you try to get them to return something. How could that possibly work? If you want them to return a value, don't make them void functions.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I was just demonstrating how the program should be structured, you have to actually make more changes to the code to get it working.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Apr 2005
    Location
    Netherlands
    Posts
    17
    Although you did help me with improving my C program, it still has the original problem with inputting letters at the scanf("%d", &choice); part..

    Jst.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because you're not supposed to input letters when you tell scanf to expect numbers. Read the FAQ if you want a better way to read numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  5. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM