#include<iostream.h>
#include<stdlib.h>
int main(void)
{
char a,q;
cout<<"Enter a char\n";
cin>>a;
q=a;
cout<<atoi(&a)<<",";
cout<<atoi(&q);
return 0;
}

*************************************

output for this code for input of (2 ,3,5):
2,22
3,33
5,55
**************************************...
I think answers should be:
2,2
3,3
5,5

First read this, then you read this.

After that you change your code to use new-style headers, and then we're ready to help you :)

Change

#include<iostream.h>
#include<stdlib.h>

to

#include<iostream>
#include<cstdlib>

(if your compiler supports it, if your compiler applies to the C++ standard, then this shouldn't give problems)

Can you also please explain us what the point of your program is?
If it's your homework, can you describe the exercise then? (but in a better way)

Probably your compiler because it worked ok for me after correcting the header files as previously explained.

Now this is an interesting case of ignorance.
But first, I must give OP the answer:
Look at the definition of atoi. It wants a cstring. Which is a null-terminated character array. Tell me, are you giving it a null terminated character array? No you aren't. You are giving him just a pointer to char. The output would be machine dependent. It was just your luck that the address just next to q and a have the value zero in your memory. So change your program as this:

#include<iostream>
#include<cstdlib>//<stdio.h> for old compiler
#include<cstring>//<string.h> for old compiler
using namespace std; //remove this line for old compiler
int main()
{
char a[5],q[5];//this will support only upto 4 digits
cout<<"Enter a char\n";
cin>>a;
strcpy(q,a);
cout<<atoi(a)<<",";
cout<<atoi(q);
return 0;
}

---------------------------------------------------------
Now. The funny part. I was pretty much sure that Ancient got the output correct because his complier had laid the memory in a different way. While the OP's Compiler used heap storage, q had a greater address than a (greater means had more value). Hence, when OP said cout<<atoi(&a), he got the output 2 (if the input was 2, say) because the next block of memory had zero value. But when he said cout<<atoi(&q), he got the value 22 because the next block was not zero but had the value 2 ( this block was the variable a itself). The memory representation was like this:

.
.
q:   |  2  |  <-- 0x000015 
a:   |  2  |  <-- 0x000014 (say)
     |  0  |  <-- 0x000013

I checked if I was correct by changing 5 the line to be char q,a; that is: declaring q before a. Thus the memory would now be laid as:

.
.
a:   |  2  |  <-- 0x000015 
q:   |  2  |  <-- 0x000014 (say)
     |  0  |  <-- 0x000013

And consequently the output came: 22,2

commented: Great explanaton :) +36
commented: Agreed with Ancient Dragon :) +7

The atoi() function converts the string pointed to by ptr to an int.

#include<iostream>
#include<cstdlib>
using namespace std;

int main(void)
{
	char q = '2';
	cout << atoi("2") << endl;
	cout << atoi(&q) << endl;

	return 0;
}

result:
2
20

commented: Did you read siddhant3s' post? No! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.