while loop - C compare numbers -
i have write program in c asks user enter range of numbers. ex. 1 4 6 9 5 , finds largest of them. seem simple consistently wrong outputs.
my code have written far is:
#include <stdio.h> #include <stdlib.h> main() { char max; int count = 0; char numbers; printf("enter sequence of numbers\n"); max = getchar(); while((numbers = getchar()) != eof) { printf("max: %d", max); putchar(numbers); }
for example, if input number 3, 3 putchar()
method, max
method, 51. or if put in 7, 55 max
method. confused.
i keeping simple of now, want first integer/char input , assign max
. , once read 2nd integer, compare max
see if greater or lower. thanks!
with getchar() reading char , not number. return value integer corresponds ascii code of char. can see ascii table , veridy (char) '7' corresponds 55, , 3 51.
therefore need either use different function parse numbers, or convert ascii code (51, 55 etc) actual integer number corresponds.
Comments
Post a Comment