1

So I'm trying to make certain a user inputs a variable in a specific set of conditions for use in a later calculation. Namely, they cannot exceed vmax, not be lower than zero and not be a string.

This is what I've got.

    do
    {
        scanf("%f", &vkmh);
        if (vkmh <= vmax)
        {
        }           
        else if (vkmh < 0)
        {
            printf("Error, speed must be positive.\n");
        }
        else if (vkmh > vmax)
        {
            printf("Error, the max speed of this vehicle is listed as %.fkm/h.\nIt cannot exceed that value. Please enter a value under %.f.\n", vmax, vmax);
        }
        else if (vkm != getchar())
        {
            printf("Error in input. Please only use numbers\n");
        }
    }
    while(vkmh > vmax || vkmh < 0 || vkm != getchar());

Theoretically valid values return a valid response, and a value above vmax returns an invalid response and requests the user to reenter. But a negative or a string doesn't return anything.

Any ideas on how I can get this to work?

Sylvain P.
  • 153
  • 6
Eureka KN
  • 57
  • 7
  • Just a notice. Construct `if (vkmh <= vmax) { X } else if (vkmh < 0) { Y }` will never execute the branch `Y` when `vmax >= 0`. – Marian Mar 26 '16 at 08:19
  • So how would I go about stating separate operations for those conditions? – Eureka KN Mar 26 '16 at 08:39
  • Have you heard of the concept of doing one thing at a time? – Ed Heal Mar 26 '16 at 09:25
  • when calling any of the `scanf()` functions, always check the returned value (not the parameter value) to assure the operation was successful. Note: if the user entered other than a digit for the first character, the returned value from the posted call to `scanf()` would be 0. – user3629249 Mar 26 '16 at 22:09
  • given these lines: ` if (vkmh <= vmax) { } else if (vkmh < 0)` if `vkmh < 0` it is also less than `vmax` so the wrong questions (or at least in the wrong order) are being ask. – user3629249 Mar 26 '16 at 22:11

2 Answers2

2

You can use the code below to achieve what you are looking for. Note that the answer is very similar to that of this answer:

How to scanf only integer and repeat reading if the user enter non numeric characters?

#include<stdlib.h>
#include<stdio.h>

int clean_stdin()
{
    while (getchar()!='\n');
    return 1;
}

int main ()
{
    float vkmh, vmax = 100.0;

    setbuf(stdout, NULL);

    vkmh = vmax + 1.0; /* value to start the loop */
    while ( vkmh < 0 || vkmh > vmax) {
        printf("Please enter value for the velocity in km/h: ");
        if (scanf("%f",&vkmh) == 1) {
            if (vkmh < 0) {
                /* This was your 2nd if condition, but would never have executed assuming that vmax > 0. */
                printf("Error, speed must be positive.\n");
                exit(1);
            } else if (vkmh <= vmax) {
                /* Do something */
            } else {
                /* No need for the else if, this is the only other possibility */
                printf("Error, the max speed of this vehicle is listed as %.fkm/h.\n"
                       "It cannot exceed that value. Please enter a value under %.f.\n", vmax, vmax);
            }
        } else {
            printf("Error in input.\n");
            clean_stdin();
        }
    }
    printf("\nValue read: %f km/h\n",vkmh);
    return 0;
}
Community
  • 1
  • 1
PZwan
  • 101
  • 2
  • 6
0

First this is C, not C#. getchar read from standard input so in every iteration you call (up to) 3 times getchar() and read 3 times user input. So you can remove those calls.

scanf function return the number of successful conversions so you can use this (==1) to check if the user has not entered a correct float value.

Edit: i've removed the code as I can't compile on my phone, sorry Use fgets / atof like in the example on this page http://www.cplusplus.com/reference/cstdlib/atof/

Sylvain P.
  • 153
  • 6