atoi issue, cannot get integer value for a character

I am hoping someone can help me. I am reading the book C++ Programming Fundamentals (CyberRookies) and I have encountered an issue and I can't figure it out!
In Chpt 4, example 4.7, it talks about built-in functions, specifically atoi, which is supposed to take a character and return the integer equivalent. The problem is whenever I execute the program, the character is displayed, but the equivalent integer value always comes up as 0 (zero), no matter what character I input. Here is the program:
//example 04-07 - using built-in functions
#include <iostream>
using namespace std;
int main()
{
char a,b,u,l;
int x;
cout << "Please enter a character. \n";
cin >> a;
x = atoi(&a);
cout << "The character " << a << " is converted to the integer " << x << "\n";
cout << "Please enter a character. \n";
cin >> b;
u = toupper(b);
l = tolower(b);
cout << b << " in upper case is " << u << " in lower case is " << l;
cout << "\n Press any key to continue ";
return 0;
}

I have tried everything, including looking this forum's archives, but anything I have seen just doesn't help me to fix this problem - probably because I am a beginner! So, I am hoping someone can help me, so that I can execute this program and get the integer value for a character value that is entered.
Any help is much appreciated!

Margie
I'm pretty sure atoi only accepts null-terminated strings, not characters.

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main(){
	char buffer[16];
	char b,u,l;
	int x;
	cout << "Please enter a character. \n";
	cin.getline(buffer,16);
	x = atoi(buffer);
	cout << "The string " << buffer << " is converted to the integer " << x << "\n";
	cout << "Please enter a character. \n";
	cin >> b;
	u = toupper(b);
	l = tolower(b);
	cout << b << " in upper case is " << u << " in lower case is " << l;
	cout << "\n Press any key to continue ";
	return 0;}
The solution on this website is not in the archives. http://cplusplus.com/reference/clibrary/cstdlib/atoi/

For future reference: If you are having trouble with a specific command, I would check cpluplus.com/*******, where ******* is the command you need help on. I always do that first ;)

Anyways best of luck to ya.
Hello PiMaster,
Thank you for your response. I tried the program you designed above, however I still get an integer value of zero no matter what character or string I enter. Is there something else that is missing?
Topic archived. No new replies allowed.