James
James

Reputation: 190

C void menu program

I don't get this. Well, here's the code, first.

#include <stdio.h>

void load_menu(void);
void sum(void);
void product(void);
void difference(void);
void rest(void);

int main(int argc, char** argv)
{
load_menu();
return 0;
}

void load_menu(void)
{
int choice;

do
{
    printf("Menu\n\n");
    printf("1. Sum\n");
    printf("2. Product\n");
    printf("3. Difference\n");
    printf("4. Rest\n");
    printf("5. Exit(Bad choice)\n");
    scanf_s("%d", &choice);

    switch (choice)
    {
    case 1: sum();
        break;
    case 2: product();
        break;
    case 3: difference();
        break;
    case 4: rest();
        break;
    case 5: printf("Quitting program!\n");
        system("PAUSE");
        exit(0);
        break;
    default: printf("Invalid choice!\n");
        break;
    }

  } while (choice != 3);

}

    void sum(void)
    {
      int num1, num2;
      int ch;

      printf("Enter number 1: ");
      scanf_s("%d", &num1);
      printf("Enter number 2: ");
      scanf_s("%d", &num2);

      printf("\nThe sum of the numbers was: %d", num1 + num2);


      while ((ch = getchar()) != '\n' && ch != EOF);

      printf("\n\nPress ENTER to continue.");
      while ((ch = getchar()) != '\n' && ch != EOF)
       ;

system("cls");
return;
}

    void product(void)
    {
     int num1, num2;
     int ch;

     printf("Enter a number 1: ");
     scanf_s("%d", &num1);
     printf("Enter number 2: ");
     scanf_s("%d", &num2);

     printf("\nThe product of the numbers was: %d", num1 * num2);

     while ((ch = getchar()) != '\n' && ch != EOF);

     printf("\n\nPress ENTER to continue.");
     while ((ch = getchar()) != '\n' && ch != EOF)
       ;

system("cls");
return;
}

    void difference(void)
    {
    int num1, num2;
    int ch;

    printf("Enter a number 1: ");
    scanf_s("%d", &num1);
    printf("Enter a number 2: ");
    scanf_s("%d", &num2);

    printf("\nThe difference of the numbers was: %d", num1 - num2);

    while ((ch = getchar()) != '\n' && ch != EOF);

    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;

system("cls");
return;
}

    void rest(void)
    {
    int ch;
    printf("Sleepy sleepy... zZZzZzZz\n");
    printf("You now feel awake again!\n");


    while ((ch = getchar()) != '\n' && ch != EOF);

    printf("\n\nPress ENTER to continue.");
    while ((ch = getchar()) != '\n' && ch != EOF)
    ;

system("cls");
return;
}

But the problem is that in the "difference part", after it finishes calculating it just quits if I press Enter. It should clear the screen and repeat to menu, but it just quits??? Other part works fine, but what the heck is wrong with the difference part? Can you find any mistakes or suggestions? I think it's got to do with return part.. I tried system PAUSE but it doesn't do anything but just pause and I have to quit.

So what do you think it's the problem? Thanks in advance.

Upvotes: 0

Views: 8238

Answers (1)

jwodder
jwodder

Reputation: 57470

The condition on your do...while loop is while (choice != 3), and difference() corresponds to choice 3. Thus, if 3 is selected, difference() is run, and then the loop terminates, ending the program.

Upvotes: 4

Related Questions