My code returns the ASCII value of string input - Unable to find the error


  • 0
    L
     bool checkNumeric(char input)
      {
       //cout<<"CheckNum: "<<input; 
       return (input>= '0' && input<= '9') ? true : false;
    
      }
      
      int Solution::myAtoi(char * input)
      {
        int num = 0;
        for(int i=0; input[i]!= '\0'; i++)            //Check if Numeric
        {
          cout<<"\n current numeric:"<<input[i]<<"  prev Num: "<<num;
          if(checkNumeric(input[i]))
          {
            cout<<"\n"<<num<<"*"<<10<<"+"<<input[i];
            num = num*10 + input[i];
            cout<<"\nNum is"<<num;
          }
        }
         
      if(input[0] == '-') num = num* -1;              //Handle negative numbers
      
      return num;
      }
    

    OUTPUT:

     current numeric:-  prev Num: 0
     current numeric:4  prev Num: 0
    0*10+4
    Num is52
     current numeric:5  prev Num: 52
    52*10+5
    Num is573
     current numeric:6  prev Num: 573
    573*10+6
    Num is5784
     current numeric:7  prev Num: 5784
    5784*10+7
    Num is57895
     current numeric:3  prev Num: 57895
    57895*10+3
    Num is579001
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.