2
votes

I'm creating a program which should validate my input on correct values, but unfortunately I'm doing something wrong. This program needs to check an input value 10 times if the answer is 1 or 0. Else it has to ask for an answer again. When the input is done it is supposed to show the correct answer by a printf, but it doesn't.

My guess is that something is wrong in the 'switch case' part. Help would be appreciated!

My code:

int main()

{
char a0, a1, a2, a3, a4, a5, a6, a7, a8, a9;
char c = '0';
int a = 0, OK = 0, check = 0, valid_input = 0, a_ok;

printf("Fill in a value 1 or 0.\n\n");

while (a < 10)
{
while (valid_input == 0)
{
    printf("Fill in a%d: ", a);
    a_ok = scanf("%d", &OK);
    if (a_ok != 1)
    {
        scanf("%s", &c);
    }
    else if (OK <0 | OK >1)
    {
        do
        {
            while (check == 0)
            {
            printf("Fill in a%d: ", a);
            check = check +1;
            }
        c = getchar();
        }
        while (!isdigit(c));
        ungetc(c, stdin);
    }
    else
    valid_input = 1;
}
switch (a)
    {
    case 0:
    OK = a0;
    case 1:
    OK = a1;
    case 2:
    OK = a2;
    case 3:
    OK = a3;
    case 4:
    OK = a4;
    case 5:
    OK = a5;
    case 6:
    OK = a6;
    case 7:
    OK = a7;
    case 8:
    OK = a8;
    case 9:
    OK = a9;
    }
a = a +1;
}

/*
printf("Fill in a1: ");      **<--- WANT TO REMOVE THIS PART BY LOOP**
scanf("%d", &a1);
printf("\nFill in a2: ");
scanf("%d", &a2);
printf("\nFill in a3: ");
scanf("%d", &a3);
printf("Fill in a4: ");
scanf("%d", &a4);
printf("\nFill in a5: ");
scanf("%d", &a5);
printf("Fill in a6: ");
scanf("%d", &a6);
printf("\nFill in a7: ");
scanf("%d", &a7);
printf("\nFill in a8: ");
scanf("%d", &a8);
printf("Fill in a9: ");
scanf("%d", &a9);
*/

printf("\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d", a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);

getch();
return(0);
}
1
i would recommend to use an array for aX - thumbmunkeys

1 Answers

7
votes

You forgot the break statement in each case of your switch:

case 0:
    OK = a0;
    break;
case 1:
    OK = a1;
    break;
/* etc. */

Also, this if statement is wrong:

if (OK <0 | OK >1)

You probably meant to use ||, the logical OR operator, not |, the bitwise OR operator.

Furthermore, your aN variables (a0, a1, ... a9) aren't initialized anywhere. Their initial values are undefined.

Additionally (I'm running out of continuation words here :-P), this:

while (!isdigit(c));

just looks wrong. It's equivalent to this:

while (!isdigit(c))
{
}

Which means it's an infinite loop when isdigit(c) == 0 and does nothing when isdigit(c) != 0. Maybe you meant to write this instead:

while (!isdigit(c)) {
    ungetc(c, stdin);
}

But even then, you'll be pushing the same character back to stdin in an infinite loop.

This is also wrong:

scanf("%s", &c);

c is a char variable, but you're telling scanf() to read a string.

Not an error, but instead of 10 individual variables, you should probably use an array instead:

char val[10];

So that instead of:

a0 a1 ... a9

you'll have:

val[0] val[1] ... val[9]

Anyway, this program is extremely broken. If you just began to learn C, I'd recommend starting from scratch and paying more attention.