cfoolproof-validation

Area of a rectangle foolproof


Who can help me with the area of ​​a rectangle. I can't make foolproof, ie the ban on entering letters and negative numbers.I just can not create a second condition when creating the first that refers to the letters. How can I do this, or I must even change the program from scratch. I just need to find the area of ​​a rectangle.

#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <locale.h>
#include <Windows.h>

int _tmain(int argc, char* argv[])
{
    printf("Area of a Rectangle. Press Enter to start\n");
    system("pause");

    float a, s, d;

    do
    {
        fflush(stdin);
        system("cls");
        printf("Enter the lengths of the rectangle\n");
        scanf_s("%f", &a);
        scanf_s("%f", &s);

        if (getchar() != '\n') {
            while (getchar() != '\n')
                ;
            printf("Your data is wrong\n");
            system("pause");
            continue;
        }

        break;
    } while (true);

    d = a*s;
    printf(" Area is %.1f ", d);
    system("pause");
    return 0;
}

Solution

  • This program will ask for a numeric value and accept only positive values. An input of letters or symbols will cause scanf() to fail and the program will clear the input buffer and try again. In this example, inputting the letter 'q' will quit the program. You could adapt this to your situation.

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
        int i = 0;
        float n = 0.0f;
    
        do {
            printf("enter numeric value or q to quit\n");
            if ( scanf("%f",&n) != 1) {// scan one float
                while ( ( i = getchar ( )) != '\n' && i != 'q') {
                    //clear buffer on scanf failure
                    //stop on newline
                    //quit if a q is found
                }
                if ( i != 'q') {
                    printf ( "problem with input, try again\n");
                }
            }
            else {//scanf success
                if ( n == fabs ( n)) {
                    printf("number was %f\n", n);
                }
                else {
                    printf("positive numbers only please\n");
                }
            }
        } while ( i != 'q');
    
        return 0;
    }
    

    This has adapted the above into a function.

    #include <stdio.h>
    
    float getfloat ( char *prompt, int *result);
    
    int main ( int argc, char* argv[])
    {
        float width, height, area;
        int ok = 0;
    
        do {
            printf("\n\tArea of a Rectangle.\n");
            width = getfloat ( "Enter the width of the rectangle or q to quit\n", &ok);
            if ( ok == -1) {
                break;
            }
            height = getfloat ( "Enter the height of the rectangle or q to quit\n", &ok);
            if ( ok == -1) {
                break;
            }
    
            area = width * height;
            printf(" Area is %.1f\n", area);
        } while (1);
    
        return 0;
    }
    
    float getfloat ( char *prompt, int *result)
    {
        int i = 0;
        float n = 0.0f;
    
        *result = 0;
    
        do {
            printf("%s", prompt);
            if ( scanf("%f",&n) != 1) {// scan one float
                while ( ( i = getchar ( )) != '\n' && i != 'q') {
                    //clear buffer on scanf failure
                    //stop on newline
                    //quit if a q is found
                }
                if ( i != 'q') {
                    printf ( "problem with input, try again\n");
                }
                else {
                    *result = -1;
                    n = 0.0f;
                }
            }
            else {//scanf success
                if ( n == fabs ( n)) {
                    *result = 1;
                }
                else {
                    printf("positive numbers only please\n");
                }
            }
        } while ( *result == 0);
    
        return n;
    }