Basic input validation using if-else

Hello everyone, been modifying a lab assignment to do some basic input validation using if-else.

There's one error, I'd really appreciate hints for.

If the user selects option C, and then enters invalid input like "@", the error message should trigger like how it triggers for options A and B.

But for some reason, the else block for the area of a circle is triggered:

Enter your choice [A-C]:
c

Enter the radius(in cm):
*

The area of the circle is 0.00 cm²

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <conio.h>
#include <ctype.h> //for toupper to convert lower-case to upper-case

int main (void)
{
    float area,length,width,radius,PI=3.142;
    char select;

    printf("Area Calculation Menu\n");
    printf("---------------------\n");
    printf("\nA. The area of a square.\n");
    printf("\nB. The area of a rectangle.\n");
    printf("\nC. The area of a circle\n");

    printf("\nEnter your choice [A-C]:\n");

    scanf(" %c", &select);
    select=toupper(select);

    if (select=='A')
    {
        printf("\nEnter the length of the square(in cm):\n");
        scanf(" %f",&length);

        if (length==0 || length<0)
        {
            printf("\a\nError: Invalid value for length of square!");
        }

        else
        {
            area=length*length;
            printf("\nThe area of the square is %.2f cm%c",area,253);
        }
    }

    else if (select=='B')
    {
        printf("\nEnter the width of the rectangle(in cm):\n");
        scanf(" %f",&width);

        if (width==0 || width<0)
        {
            printf("\a\nError: Invalid value for width of rectangle!");
        }

        else
        {
            printf("\nEnter the length of the rectangle(in cm):\n");
            scanf(" %f",&length);

            if (length==0 || length<0)
            {
                printf("\a\nError: Invalid value for length of rectangle!");
            }

            else
            {
                area=width*length;
                printf("\nThe area of the rectangle is %.2f cm%c",area,253);
            }
        }
    }

    else if (select='C')
    {
            printf("\nEnter the radius(in cm):\n");
            scanf(" %f", &radius);

            if (radius==0 || radius<0)
            {
                printf("\a\nError: Invalid value for radius!");
            }

            else
            {
                area=PI*(radius*radius);
                printf("\nThe area of the circle is %.2f cm%c",area,253);
            }

    }

    else
    {
        printf("\a\nError: Invalid choice!");
    }

    getch();
}
Last edited on
Take a look at this:http://www.cplusplus.com/reference/cctype/. The "isdigit" function might be what you are looking for. You could determine if your radius is a number.
Topic archived. No new replies allowed.