c - I am running a simple addition program, but I am having an issue with the output, when I check for an integer? -


#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 2 integers 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");         }      }      = addi(u, t);     return 0; } 

the above code, having slight issue with. keep getting wrong output each time input character instead of getting invalid input output on screen.

first of have pretty basic issue's in code. try 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 code number:1 point, printing value of addition , returning 0, that's not approach, should return addition value , print main function. replace whole code line return n+m;

int main() {    int t,u,i,j,k;    printf("please enter 2 integers 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 have 2 mistakes, 1: or not keyword in c should use || this. , 2: isdigit() return value of character if digit else return 0, see invalid input if either of value 1

in number:3 should not call addi function time, call function if digit.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -