hi:
If i can use getchar() function to input some numbers from keyboard only, how i can convert or use any method to sum those numbers up? Thanks!!!
hi:
If i can use getchar() function to input some numbers from keyboard only, how i can convert or use any method to sum those numbers up? Thanks!!!
also i cannot use array to hold the number..
x now takes the value of whatever you enter.Code:int x; printf("Enter a number:"); scanf("%d", &x);
Here, c must be between 0 and 9 inclusive. This is obviously no good.Code:int c; c = getchar(); int x = c - '0';
Please read this
Last edited by Tommo; 09-01-2007 at 04:34 PM.
Well i'm not sure to understand what you are trying to do but you sure can recompose a number by reading one character at a time.
Somehow like this
But you should make a call to scanf since scanf is good for reading number. Except in the case this was some sort of homework... whatever..Code:#include <stdio.h> #include <ctype.h> int main() { int carac; int nombreEntree = 0; printf("Enter a non negative number: "); carac = getchar(); while (isdigit(carac)) { nombreEntree = nombreEntree * 10 + (carac - '0'); carac = getchar(); } // Flush the input buffer if it isn't empty if (carac != '\n') { while (getchar() != '\n'); } printf("\nThe number you entered is: %d", nombreEntree); getchar(); return 0; }
Code:... nombreEntree = nombreEntree * 10 + (carac - '0');
Is this (and the others " - '0'"s) because getchar stores an ASCII?