C语言不必要的重复

时间:2018-05-29 02:53:27

标签: c

我试图创建一个C程序来计算一些阶乘,方形等。我使用函数向用户显示一个菜单,当用户输入错误的字符时会出现问题使用警告信息两次使用户变暖。

在用户输入错误的字符后,为了只显示一次选项菜单,我需要更改什么?感谢

以下是我的结果:Image

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define ACTION "Enter a number inside the range specified above:"

// Function declaration

void factorialeven();
void factorialodd();
void square();
void cube();
void fifthpower();
void mainmenu();
void secondmenu();

int main()
{
    int y = 1;
    char q;
    mainmenu();
    while (y)
    {
        printf("\n");
        printf("%s", ACTION);

        q = getchar();
        // reading user input and assign it to variable q

        switch (q)
        {
        case '1':
            factorialeven();
            break;

        case '2':
            factorialodd();
            break;

        case '3':
            square();
            break;

        case '4':
            cube(); // option 4 for cube
            break;  // End case 4

        case '5':
            fifthpower(); // option 5 for 5th power
            break;  // End case 5

        case '6':
            exit(0);
            break;  // End case 6

        default:    // catch all other options
            /*telling the user that the input is invalid */
            printf("\n**********You have entered unavailable option");
            printf("***********\n");
            printf("\n*****Please Enter any one of below available 
      options****\n ");
            secondmenu();
            // displaying the menu without header
        }       // End switch
    }               // End while
}                   // End main

// Function definitions
void mainmenu()                 // Menu of selection
{
    printf("\n---------- Welcome to our Project 1 ---------- \n");
    printf("\nPress 1 to calculate Factorials for Even Numbers");
    printf(" between 2 and 16\n");
    printf("\nPress 2 to calculate Factorials for Odd Numbers");
    printf(" between 1 and 15\n\n");
    printf("Press 3 to calculate the Square of number between -14 
      and 14\n\n");
    printf("Press 4 to show the Cube of numbers between -14 and 
      14\n\n");
    printf("Press 5 to show Fifth Power numbers between -12 and 
      12\n\n");
    printf("--------- Press '6' to quit ");
    printf("the program ---------\n");
}

void secondmenu()
{
    printf("\nPress 1 to calculate Factorials for Even Numbers");
    printf(" between 2 and 16\n\n");
    printf("\nPress 2 to calculate Factorials for Odd Numbers");
    printf(" between 1 and 15\n\n");
    printf("Press 3 to calculate the Square of number between -14 and 14\n\n");
    printf("Press 4 to show the Cube of numbers between -14 and 14\n\n");
    printf("Press 5 to show Fifth Power numbers between -12 and 12\n\n");
    printf("--------- Press '6' to quit ");
    printf("the program ---------\n");
}               // End function

int fact(int b)
{
    if (b <= 1)
    {
        return 1;
    }
    else
    {
        return b * (fact(b - 1));
    }
}

void factorialeven()
{
    int t = 0;
    printf("\nEven numbers\tFactorials\n");
    while (t <= 15)
    {
        t += 2;
        printf("%d\t\t%d\n", t, fact(t));   // Displays the results
    }               // End while
}               // End function

void factorialodd()
{
    int r = 17;
    printf("\nOdd numbers\tFactorials\n");
    while (r >= 2)
    {
        r -= 2;
        printf("%d\t\t%d\n", r, fact(r));
    }
}

void square()
{
    int g = 15;
    int square;
    printf("\nNumbers\t\tSquare\n");
    while (g >= -13)
    {
        g -= 1;
        square = pow(g, 2);
        printf("%d\t\t%d\n", g, square);
    }
}

void cube()
{
    int h = -15;
    int cube;
    printf("\nNumbers\t\tCube\n");
    while (h <= 13)

    {
        h += 1;
        cube = pow(h, 3);
        function printf("%d\t\t%d\n", h, cube);
    }
}

void fifthpower()
{
    int l = -13;
    int fifthpower;
    printf("\nNumbers\t\tFifth Power\n");
    while (l <= 11)
    {
        l += 1;         // Increases the counter by 2
        fifthpower = pow(l, 5);
        printf("%d\t\t%d\n", l, fifthpower);
    }
}

1 个答案:

答案 0 :(得分:1)

您的程序会提示输入单个字符并使用getchar()(其返回int而不是char来读取,因此q需要定义为{ {1}}并检查与int)的相等性。

如果输入的字符不正确(例如EOF),则实际输入两个字符:'x'和输入时的换行符Enter 。它需要两次'x'次调用才能读取它们,因此您会收到两条错误消息。

如果输入是面向行的,你可以使用getchar()将一行读入一个字符串(但处理输入的时间比你的目标数组要长),或者你可以读取一个字符然后读取并丢弃最多fgets()'\n'的字符。