473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

atoi conversion problem

2 New Member
Following code is not working properly
it give answer
c=5
and
d=75

Expand|Select|Wrap|Line Numbers
  1. char a='5',b='7';
  2. int c,d;
  3. c=atoi(&a);
  4. cout<<c;
  5. d=atoi(&b);
  6. cout<<d;
Apr 20 '10 #1
4 3142
jkmyoung
2,057 Recognized Expert Top Contributor
Seems like it's looking at the address and expecting a string; however you have only defined space for characters.
In memory, it must be stored as
75\0.

Use strings instead of single chars, or you could also try c=(int)(a - '0');
since you know both a and b are chars.
Apr 20 '10 #2
donbock
2,426 Recognized Expert Top Contributor
Refer to the man page for atoi. As jkmyoung said, atoi expects its argument to point to a null-terminated string. That's not what you passed. It is amazing the result bears any resemblance to what you expect.
Apr 20 '10 #3
whodgson
542 Contributor
The following may shed some further light on it.
Expand|Select|Wrap|Line Numbers
  1. const char* s="  21xy 3";
  2.     const char*s1= " 74.7xyz ";
  3.     cout<<"the first string is \n"<<s<<endl;
  4.     cout<<"the second string is \n"<<s1<<endl;
  5.     n=atoi(s);
  6.     m=atoi(s1);
  7.     cout<<"after converting strings to integers ";
  8.     cout<<"s= "<<n<<" and s1="<<m<<endl;
  9.     cout<<"the product of s and s1 is "<<n*m<<endl; 
//prints:-
/*demonstrating the atoi() function
the first string is
21xy 3
the second string is
74.7xyz
after converting strings to integers s= 21 and s1=74
the product of s and s1 is 1554
press any key to continue...*/
Apr 21 '10 #4
whodgson
542 Contributor
The following convert() function I think does the equivalent of atoi ().
Expand|Select|Wrap|Line Numbers
  1. double convert(string a)
  2. {double sum_s=0.0;
  3.     double xx=0.0;
  4.     len=a.length();
  5.     n=len;
  6.     xx=pow(10,n-1);
  7.     for(int i=0;i<len;i++)
  8.     {
  9.     sum_s+=(a[i]-'0')*xx;
  10.     xx/=10;
  11.     }
  12.     return sum_s;
  13. }
Apr 22 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1589
by: senthil kumar | last post by:
Hi We've been noticing a problem with atoi conversion on PowerPC. When we supply value > 2^31-1 to utmost 2^32-1 it doesn't work properly. What i mean is the converted value when printed using...
15
5120
by: Anonymousgoogledeja | last post by:
Hi all, since the function atof, atoi, _atoi64, atol returned value are Return Values Each function returns the double, int, __int64 or long value produced by interpreting the input...
47
46131
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
2
2218
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; ...
5
5119
by: David Ching | last post by:
Hello, I'm a .NET newbie but have 12 years of C++/MFC. In C++, atoi() stops converting at the first non-digit, so in this code, octet will be 192: int octet = atoi("192."); // <-- note...
4
4785
by: sachinahuja82 | last post by:
Hi I am using atoi and strtoul in my code. I have read from MSDN that both functions return 0 if they are unsuccessful. i.e strtoul returns 0 if no conversion can be performed. Now the string...
3
4169
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
11
4227
by: Nezhate | last post by:
Hi all, Can you help me? Why this warning appears in the next simple code ? warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. #include <stdio.h> #include...
50
5171
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was...
10
4156
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.