Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:09:40+00:00 2026-06-14T17:09:40+00:00

I’m creating a program which should validate my input on correct values, but unfortunately

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:09:40+00:00Added an answer on June 14, 2026 at 5:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.