-1

I'm trying to create a program in which the user enters three integers, and another function checks to see that their input is valid. If the input is not valid, then the user must input three new numbers.

#include <stdio.h>
int sanitizedInput(int a, int b, int c)
{
     if(scanf("%d", &a)==0)
     {
         printf("Not a number\n");
         return 1;
     }
     else if(scanf("%d", &b)==0)
     {
         printf("Not a number\n");
         return 1;
     }
     else if(scanf("%d", &c) == 0)
     {
          printf("Not a number\n");
          return 1;
     }                 
     else
         return 0;
}
int main()
{
     int a;
     int b;
     int c;
     int check = 1;

     do
     {
         check = 0;

          printf("Enter a number:");
          scanf("%d",&a);
          printf("Enter a number:");
          scanf("%d",&b);
          printf("Enter a number:");
          scanf("%d",&c);

          check = sanitizedInput(a,b,c);
     }while(check);
}

However when I run this code, after entering three valid integers nothing shows up in the terminal and the code only terminates after entering 6 integers. (There are other functions and code in the main function, if that code is necessary to find the problem tell me and I will post it.)

Ludwig
  • 99
  • 1
  • 2
  • 11

4 Answers4

1

Your code and your writing part is not matching.....

  1. You should check the three numbers are valid or not firstly.
int sanitizedInput(int a, int b, int c)
{
    if(a==0 || b==0 || c==0)
    {
        return 1;
    }
    else
    {
        printf("They are valid.....\n");
        return 0;
    }
}

Then if one of them are invalid, you will be able to take another three input for the returning value of 1. Because while(1) is a true condition.

Mr. Perfectionist
  • 2,605
  • 2
  • 24
  • 35
0

remove

      printf("Enter a number:");
      scanf("%d",&a);
      printf("Enter a number:");
      scanf("%d",&b);
      printf("Enter a number:");
      scanf("%d",&c);

and stay with check = sanitizedInput(a,b,c);, and add printf("something\n") to the

else return 0; block

and see what happens

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

In main() you are taking input for three numbers a,b,c and passing these variables as arguments for sanitizedInput().

Here, instead of checking the variables you are again using scanf() which will take new input.

if(scanf("%d", &a)==0)

The above if condition will not check the value of 'a', it will check the return value of scanf() with '0'.

if statement should be like this

    if(a==0)
        scanf("%d",&a);

this is same for all three variables.

In main function you are passing variables to sanitizedInput(), there you are checking variables and if not valid you are taking input again, so the variable which you changed are local to that function, which will not reflect in main(). So take care about that. Hope this will help you.

Ash
  • 176
  • 1
  • 11
0

In your while loop,you actually call scanf twice each variable(a,b,c),so you input number for 6 times.When sanitizedInput(a,b,c) finished,it return 0;so check is 0,the loop is over.I think you can do with in your main:

int main
{
     int a;
     int b;
     int c;
     int check = 0;
     do
     {
        check = sanitizedInput(a,b,c);
        printf("check = %d\n",check);
     }while(!check);
     return 0;
}
BobWanghz
  • 58
  • 3