-1
#include <stdio.h>
#include <ctype.h>

int addi (int n, int m) {
    int x;
    x = n + m;
    printf("%d", x);
    return 0;
}

int main () {
    int t, u, i, j, k;
    printf("please enter two integers to be added");
    while (1) {
        scanf("%d%d",&u,&t);
        if (isdigit(t)==0 && isdigit(u)==0) {
            break;
        } else {
            if(isdigit(t)==1 or isdigit(u)==1)
            printf("invalid input");
        }
     }

    i = addi(u, t);
    return 0;
}

The above is the code, that I am having a slight issue with. I keep getting a wrong output each time I input a character instead of getting an invalid input output on the screen.

  • 1
    1. you don't print the result of `addi`. 2. you don't return anything in `addi` – royhowie Jun 10 '15 at 18:06
  • You should check the return value of `scanf` rather than checking whether an `int` is digit, which is obviously not working as you think it is.. – Eugene Sh. Jun 10 '15 at 18:07
  • 3
    what is this "or" in your code. In C programming "or" operator is written like this "||" not "or". Did this code got compiled. – mSatyam Jun 10 '15 at 18:14
  • possible duplicate of [Check if a value from scanf is a number?](http://stackoverflow.com/questions/2023079/check-if-a-value-from-scanf-is-a-number) – royhowie Jun 10 '15 at 18:23

3 Answers3

0

The C library function isdigit checks if the passed character is a decimal digit. You need to use isdigit properly in your code. Here you can find usage of isdigit  

royhowie
  • 11,075
  • 14
  • 50
  • 67
Amol Saindane
  • 1,568
  • 10
  • 19
0

First of all you have some pretty basic issue's in your code. I will try to explain those.

#include<stdio.h>
#include<ctype.h>

int addi(int n, int m)
{
   // 1
   int x;
   x=n+m;
   printf("%d",x);
   return 0;
}

In your code Number:1 point, you always printing the value of the addition and returning 0, that's not a good approach, you should return the addition value and print it from the main function. you could replace the whole code by only this line return n+m;

int main()
{
   int t,u,i,j,k;
   printf("please enter two integers to be added");
   while(1){
      scanf("%d%d", &u, &t);
      if(isdigit(t)==0 && isdigit(u)==0)
         break;
      else
      {
         if(isdigit(t)==1 or isdigit(u)==1) // 2
            printf("invalid input");
      }
   }

   i=addi(u,t); // 3
   return 0;
}

In Number:2 point you have 2 mistakes, 1: or is not any keyword in c you should use || this. and 2: isdigit() return the value of the character if its a digit else it return 0, so you will see invalid input if either of the value is 1

In Number:3 you should not call addi function all the time, just call this function if its a digit.

NULL
  • 1,848
  • 1
  • 21
  • 23
0

Your function 'addi' dint return anything so why 'return 0'?

You may want to make it void !

Again another thing is why you catch the return in variable i

Please check what you want your code to do.

Valid C syntax use || in place of or and && in place of and

And isdigit() return the value of the digit itself if its a digit else it return 0

So use it accordingly you should get the issue corected

Sagar Kar
  • 177
  • 1
  • 2
  • 10