1
#include <stdio.h>

int main(){

    float x,y;

    printf("Enter 2 numbers: \n");
    scanf_s("%f %f", &x, &y);

    if (getchar() == '\n')
    {
    printf("Division: %f\n", x / y);
    printf("Product: %f\n", x * y);
    printf("Sum: %f\n", x + y);
    printf("Difference: %f\n", x - y);
    }
    else
    {
        printf("no Float-Value!");
    }

getchar();
return 0;
}

we need to get a loop in so if we enter the wrong format the program should ask again to enter two numbers

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • And what's the question ? What problem did you run into when trying to do this ? What did you try ? – Sander De Dycker Jun 13 '18 at 14:46
  • What constitutes a wrong format? – ltd9938 Jun 13 '18 at 14:47
  • i need to add a loop in code so the program asks me again to enter 2 values if you enter the wrong format – Efkan Özkeles Jun 13 '18 at 14:47
  • @itd9938 anything else than float – Efkan Özkeles Jun 13 '18 at 14:47
  • I'm assuming perhaps a string can be entered instead of a float, which would case issues with scanf_s using the "%f %f" format specifier. – h0r53 Jun 13 '18 at 14:47
  • 2
    And to check for the "wrong format", you should get the input as a string and parse it... Using `scanf` will cause all sorts of trouble. Look for `fgets`. – Enzo Ferber Jun 13 '18 at 14:48
  • 1
    @EnzoFerber is correct. your scanf_s will not parse anything other than floats by using that format string. If you want to validate the input, you should scan a broader range of inputs then check to see if they are indeed floats. – h0r53 Jun 13 '18 at 14:49
  • I think what you really need is this great tutorial on reading input: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html or this: https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq – Yunnosch Jun 13 '18 at 15:35

2 Answers2

5

The best way of checking the validity of the input is to check the return value of scanf_s which tells you the number of variables that were successfully set. In your case, if it's 2 then all is well; otherwise you need to repeat.

In other words

do {
    int c;
    while ((c = getchar()) != '\n' && c != EOF); // clear the input stream
    printf("Enter 2 numbers: \n");
} while (scanf_s("%f %f", &x, &y) != 2);

is an appropriate control structure, rather than if (getchar() == '\n'), which you should drop.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

What I don't like about using scanf family of functions is that it will go berserk and crash your program when you input something wrong. For example, if you have scanf("%f", &a), try inputing stack overflow. It goes nuts!

So, as I said in the comments, I think you should build your own validating function, and get user input as just a string using fgets.

Here is a simple code that will get you started in it. This is very ugly code and you should refactor it.

#include <stdio.h>  /* printf, fgets */
#include <ctype.h>  /* isdigit, isspace */
#include <string.h> /* strlen */

int is_valid_float(char *s, int len)
{
    int i;

    for(i = 0; i < len; i++) {
        /* floats may have a decimal point */
        if(s[i] == '.') continue;

        /* spaces should be ignored, since they separete the nubmers */
        if(isspace(s[i])) continue;

        /* is there's anything besides a number, we abort */
        if(!isdigit(s[i])) return 0;
    }

    /* if we got here, then the input contains two valid floats.
     * 
     * Does it? Test it!
     */
    return 1;
}


int main(void)
{
    float a, b;
    char buf[100];
    int len;

    do {
        fprintf(stderr, "Enter A and B: ");
        fgets(buf, sizeof buf, stdin);

        /* fgets will store a newline at the end of the string, we
         * just overwrite it with a null terminator
         *
         * Thanks to @chux for the strcspn() suggestion.
         */
        buf[strcspn(buf, "\n")] = 0;    
    } while(!is_valid_float(buf, len));

    /* now, after we know the string is valid and won't cause scanf to go
     * berserk, we can use it in our validated string.
     *
     * Here is where you should really take a moment and look at the
     * validation function and improve it. Not valid strings will cause
     * your program to go nuts.
     */
    sscanf(buf, "%f %f", &a, &b);

    /* Did it scan the numbers correctly? */
    printf("A: %f\n", a);
    printf("B: %f\n", b);

    return 0;
}
Enzo Ferber
  • 3,029
  • 1
  • 14
  • 24
  • `len = strlen(buf) - 1; buf[len] = 0;` can be hacked and a `'\n'` may not exist in `buf`. Consider `buf[strcspn(buf, "\n")] = '\0';` instead to lop off the potential trailing `\n`. – chux - Reinstate Monica Jun 13 '18 at 15:37
  • @chux I like the `strcspn` suggestion and changed the answer to use it. (about the `90`, not even I know why... I updated it) – Enzo Ferber Jun 13 '18 at 16:23