carrias
carrias

Reputation: 47

use of input validation requires entering the wrong number of inputs in c

I've been attempting to add input validation to the ui for a piece of code that performs a scientific simulation. when values are manually entered that are flagged by the validation process they should look until one valid replacement value is entered - two are required by program for reasons I don't understand and sometimes when this validation loop runs the next input set is ignored (for example when wrong values for x2 were validated I was required to enter two values instead of one and then entering a value for speed was skipped entirely). I've posted the first section of the program only, brackets may need to be closed and return values added etc for it to run as is, but the issue is only with this section - I can add the rest if that would be helpful.

thanks for your help.

int main (int argc, char *argv[])
{
double  time[10000], x1[10000], x2[10000], v1[10000], v2[10000];
double  m1, m2, k1, k2, r1, r2, w;
int     count, inmethod, savein, flappy;

/*in my experience it is good practice to only use variable names that c believes to be arbitrary - I'm using flappy as a flag having considered flag likely to be unusable.*/

time[0] = 0;
x1[0] = 0;  x2[0] = 10;
v1[0] = 0;  v2[0] = 0;
m1 = 5;     m2 = 5;
k1 = 5;     k2 = 5;
r1 = 5;     r2 = 10;
w = 0;

sscanf(argv[1], "%d", &inmethod);

if((argc == 2) && ((inmethod == 1) || (inmethod == 0)))
{
}

else
{
    printf("enter 1 for file input or 0 to enter input via the keyboard. you will be able to save your input for future use.\n");
    return(EXIT_FAILURE);
}

if(inmethod == 0)
{
    FILE    *input;

    input = fopen("inputfile.dat", "r");

    fscanf(input,"%lf", &m1);   fscanf(input,"%lf", &m2);
    fscanf(input,"%lf", &k1);   fscanf(input,"%lf", &k2);
    fscanf(input,"%lf", &r1);   fscanf(input,"%lf", &r2);

    fscanf(input,"%lf", &x1[0]);    fscanf(input,"%lf", &x2[0]);
    fscanf(input,"%lf", &v1[0]);    fscanf(input,"%lf", &v2[0]);

    fclose(input);
}

else if(inmethod == 1)
{
    printf("system properties: \n");

    printf("please enter m1 then m2, separated by a space and followed by enter\n");
    scanf("%lf %lf", &m1, &m2);

    if(m1 == 0)
    {
        flappy = 0;

        while(flappy == 0)
        {
            printf("enter a non zero value for m1.\n");
            scanf("%lf ", &m1);

            if(m1 != 0)
            {
                flappy = 1;
            }
        }
    }

// why do things happen twice?

    if(m2 == 0)
    {
        flappy = 0;

        while(flappy == 0)
        {
            printf("enter a non zero value for m2.\n");
            scanf("%lf ", &m2);

            if(m2 != 0)
            {
                flappy = 1;
            }
        }
    }

    printf("please enter k1 then k2, separated by a space and followed by enter \n");
    scanf("%lf %lf", &k1, &k2);

    printf("please enter r1 then r2, separated by a space and followed by enter \n");
    scanf("%lf %lf", &r1, &r2);

    //needs a validation condition

    printf("initial conditions: \n");

    printf("please enter x1[0] then x2[0], separated by a space and followed by enter \n");
    scanf("%lf %lf", &x1[0], &x2[0]);

    if(x1[0] < 0)
    {
        flappy = 0;

        while(flappy == 0)
        {
            printf("x1[0] must be positive, please enter it again.\n");
            scanf("%lf ", &x1[0]);
            if(x1[0] != 0)
            {
                flappy = 1;
            }
        }
    }

    if((x2[0] - x1[0] - w) < error)
    {
        flappy = 0;

        while(flappy == 0)
        {
            printf("x2[0] must be to the right of x1[0], please enter it again. \n");
            scanf("%lf ", &x2[0]);

            if((x2[0] - x1[0] - w) > error)
            {
                flappy = 1;
            }
        }
    }

    printf("please enter v1[0] then v2[0], separated by a space and followed by enter \n");
    scanf("%lf %lf", &v1[0], &v2[0]);

Upvotes: 2

Views: 154

Answers (1)

chux
chux

Reputation: 153478

Change scanf("%lf ", &m2); to scanf("%lf", &m2); in at least 3 places.

The extra space tells scanf() to scanf for following white space (which is OK as this is usually the \n), but to keep scanning until non-white space is entered. That is the reasons for the need to enter 2 numbers.

The first char of the 2nd number, not being a white space, is pushed back for the following scanf() to consume.


Side notes:

Consider collecting your fscanf() and test the result.

// fscanf(input,"%lf", &m1);   fscanf(input,"%lf", &m2); ...  8 more
if (10 != fscanf(input,"%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &m1, &m2, ... &v2[0])) {
  Handle_InputError();
}

The space in scanf("%lf %lf", &r1, &r2); looks fine, but its functionality is the same as scanf("%lf%lf", &r1, &r2);. "%lf" scans through leading white space with or without a leading " ". Here too and in other places, suggest testing the result.

if (2 != scanf("%lf%lf", &r1, &r2)) Handle_InputError();

Upvotes: 2

Related Questions