guasabito
guasabito

Reputation: 1

C language unnecessary repetition

I'm trying to create a C program to calculate some factorial ,square, etc. I'm using function to display a menu to the user that has a problem when the user types a wrong character the programs warms the user using the warning message two times.

What I have to change in order to display only once the menu of options after the user types a wrong character? Thanks

Here is the result I have : 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);
    }
}

Upvotes: 0

Views: 111

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263577

Your program prompts for a single character and reads it using getchar() (which returns an int, not a char, so q needs to be define as an int and checked for equality to EOF).

If you enter an incorrect character (say, 'x'), you're actually entering two characters: the 'x' and a newline when you type Enter. It takes two getchar() calls to read them both, so you get two error messages.

If the input is meant to be line-oriented you can read a line into a string using fgets() (but handling input longer than your target array is tricky), or you can read a single character and then read and discard characters up to a '\n' or EOF.

Upvotes: 1

Related Questions