Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a constant result of 689 in the output even if i change the string values input into ascii_to_int:

C
#include<stdio.h>
#include<conio.h>

int ascii_to_int(unsigned char *s);

void main()
{
    clrscr();
    ascii_to_int("ATOI");
    printf("%d",ascii_to_int);
    getch();
}

int ascii_to_int(unsigned char *s)
{
     int value=0;
     while(*s>'0' && *s<'9')
     {
       value= value*10 + (*s-0);
       s++;
     }
     return value;
}


What am I doing incorrectly?

[Modified: just fixed a < and >. The & in &lt; had been changed to &amp;lt;]
Posted
Updated 1-Jul-10 5:50am
v6

In addition to copying CPalini's answer go and buy two books:

- "The C Programming Language" by Kernighan and Richie

- "Expert C Programming" by Peter van der Linden

Read them and inwardly digest both. The second one is one of the best treatments of how to use C once you've got the syntax down pat, and, surprisingly for a technical book it's a really entertaining read.

Cheers,

Ash
 
Share this answer
 
Hi,

I am not sure what your code is meant to do as you are passing your function ASCII characters (which are letters) then checking for values between 0 and 9? But anyway I did notice that you have declared the parameter which you are passing to 'ascii_to_int' as an 'unsigned char' and yet you are passing it a 'const char'.
Try to single step through it to understand it fully,

Hope that helps
 
Share this answer
 
v2
Comments
CPallini 1-Jul-10 7:33am    
He is (excluding the (*s-0) bug) correctly updating the value of 'value'.
LittleYellowBird 1-Jul-10 8:18am    
Oops - you are right, what made me think that? Apologies, I will remove it from my Answer, thank you for pointing it out. :)
CPallini 1-Jul-10 8:32am    
I suppose a '+=' was just expected by your (like mine) eyes. :-)
LittleYellowBird 1-Jul-10 8:48am    
Hehe - I think you right, to my eyes, 'loop' + '=' = ALARM ALARM ALARM :-)
You got the wrong result because there are a lot of errors in you code.
Try:

C
#include <stdio.h>

int ascii_to_int(const char *s);

void main()
{
  printf("%d",ascii_to_int( "0101"));
  getchar();
}

int ascii_to_int(const char *s)
{
  int value=0;
  while(*s>='0' && *s<='9')
  {
    value = value * 10 + (*s-'0');
    s++;
  }
  return value;
}
 
Share this answer
 
Comments
LittleYellowBird 1-Jul-10 8:22am    
Just wanted to thank you for pointing out the error in my Answer to this Question - I don't know what I was thinking about (rushing due to wanting my lunch actually :) ), anyway thanks again, I shal remove that part of my Answer. :)
Richard MacCutchan 1-Jul-10 10:26am    
> and < ... yup, I missed them; probably because that line 'looked' right. :laugh:
I'm not sure how you get any answer as this code will not even compile.

You start by making a redundant call to ascii_to_int("ATOI"); which is wrong as the characters "ATOI" do not represent a number.

You then do printf("%d",ascii_to_int); which is an invalid statement; you have a function call without any input parameter. I suspect the value printed is part of the address of the function.

Also within your ascii_to_int function the statement value= value*10 + (*s-0); is incorrect, you should be subtracting '0' from *s.

It's probably a good moment to reread your study guides.
 
Share this answer
 
Comments
CPallini 1-Jul-10 7:34am    
You overlooked the '>' and '<' little bugs... :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900