-2

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;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
George
  • 3
  • 3
  • 1
    the statement: `fflush(stdin)` is explicitly stated in the C standard to not work. That is because 'fflush()` is only for output streams, not input streams. suggest using: `while( int ch = getchar() && ch != EOF && ch != '\n');` – user3629249 Sep 07 '15 at 20:25
  • 1
    the variable names: 'a' 's' and 'd' are meaningless. Your code should always use meaningful names for variables other than index/loop counter variables. Suggest: rectHeight rectWidth rectArea. – user3629249 Sep 07 '15 at 20:29
  • 1
    this line: `printf("Enter the lengths of the rectangle\n");` doesn't really tell the user what to enter. suggest: `printf("Enter length width of a rectangle\n");` Always check the returned value from a call to the scanf() family of functions to assure the operation was successful. – user3629249 Sep 07 '15 at 20:32

2 Answers2

2

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;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16
1

A foolproof scanf for a float will look like this

float getFloat() {
    float in;
    while(scanf("%f", &in) != 1 && getchar() != '\n') {
        fflush(stdin);
        fprintf(stderr, "Wrong input, enter again : ");
    }
    return in;
}

This works for me. Your code will then simplify to

float a = getFloat();
float b = getFloat();
printf("Area is %.1f", a * b);
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50