problem while reading numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meenasupari
    New Member
    • Jun 2007
    • 3

    problem while reading numbers

    Hi!

    I have a problem with the output of the following code.

    Code:
     #include<stdio.h>
                #include<string.h>
          
           main(){
           int Number=0;
           char c,*s;
          
          int base=10;
          while((c=getchar())!='\n')
          {
          if(atoi(&c)>0)
                  Number=Number*base+atoi(&c);
         }
         
         sprintf(s,"%i", Number);
         printf("%s\n",s);
         }
    it works fine till I input a number starting with a '5'
    Can anybody please tell me what exactly is the problem?

    Thanks in advance
    Mee.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    getchar returns an int. You are assigning that return to a char, which is 8 bits instead of 32.

    You might try changing c to an int.

    Comment

    Working...