C void菜单程序

时间:2013-10-28 04:07:11

标签: c

我不懂。 嗯,这是代码,首先。

#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;
}

但问题在于,在“差异部分”中,在完成计算后,如果按Enter键,则退出。它应该清除屏幕并重复菜单,但它只是退出??? 其他部分工作正常,但差异部分究竟出了什么问题呢? 你能找到任何错误或建议吗?我认为这与返回部分有关.. 我试过系统PAUSE但它没有做什么,只是暂停,我不得不退出。

那么您认为这是什么问题? 提前致谢。

1 个答案:

答案 0 :(得分:4)

do ... while循环的条件为while (choice != 3)difference()对应于选项3.因此,如果选择3,{{1}运行,然后循环终止,结束程序。