473,509 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'atoi' : cannot convert parameter 1 from 'char' to 'const char *'

What's wrong with the use of atoi in the following code? Why do I
get the error message:

'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
char cBuffer[9];
void PushUnique(int);

for(y = 0; y < 9; y++)
{
isF.getline(cBuffer,9);
for(x = 0; x < 9; x++)
bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
}
Oct 31 '05 #1
6 16024
the prototype for atoi is
int atoi(const char *str);
meaning it takes a pointer to an entire character string like "12345".

if you wanna get the value of each digit and pass it to PushUnique just
subtract 48 from each character. Hackish, but should work.

bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));

Chinchilla

Oct 31 '05 #2
John Smith wrote:
What's wrong with the use of atoi in the following code? Why do I
get the error message:

'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
char cBuffer[9];
void PushUnique(int);

for(y = 0; y < 9; y++)
{
isF.getline(cBuffer,9);
Are you sure the buffer of 9 chars is enough to read a whole line?
for(x = 0; x < 9; x++)
bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
In this statement, the expression inside 'PushUnique's parentheses is

atoi(cBuffer[x])

'cBuffer[x]' is a _char_. 'atoi' expects a pointer to char (actually
a pointer to a first element of an array of char, ending with a null
character, IOW a C string. What are you trying to accomplish here?
Depending on your answer, we can suggest a solution or two. The very
first one that comes to mind is for you to drop the loop here and just
do

bd.Cell[x][y].PushUnique(atoi(cBuffer));

it will compile, but it may not be what you need...
}


V
Oct 31 '05 #3
"John Smith" <js****@company.com> wrote in message
news:zK******************@twister.southeast.rr.com ...
: What's wrong with the use of atoi in the following code? Why do I
: get the error message:
:
: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
:
:
: char cBuffer[9];
: void PushUnique(int);
:
: for(y = 0; y < 9; y++)
: {
: isF.getline(cBuffer,9);

What do you intend cBuffer to contain here?
I would think it is to contain the ascii representation
of a single number -- such as: "16384" .
When that is the case, a single call to atoi will extract
that number:
int i = atoi(cBuffer); // atoi takes a string, and
// returns the value (e.g. i=16384)
Note that strtoi supports better error reporting,
and shall usually be preferred to atoi.

: for(x = 0; x < 9; x++)
: bd.Cell[x][y].PushUnique(atoi(cBuffer[x]));
: }

Are you trying to convert each digit into an integer?
In this case, PushUnique( cBuffer[x] - '0' ); will do.
But then you need to do the following:
- make sure the read line is at least 9 characters
long, or handle shorter strings appropriately.
- BTW, if you need 9 characters then cBuffer must
be at least 10 chars long (null-termination of C strings).
- error handling: first check that each digit-character
c is actually a decimal digit: ( (c>='0') && (c<='9') )

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Oct 31 '05 #4
Chinchilla wrote:
the prototype for atoi is
int atoi(const char *str);
meaning it takes a pointer to an entire character string like "12345".

if you wanna get the value of each digit and pass it to PushUnique just
subtract 48 from each character. Hackish, but should work.

bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));


Why would you write 48 instead of '0' ?? (Really -- I'm interested,
it seems to be a fairly common practice). It is less portable
and less readable and appears to have absolutely nothing in its
favour, as far as I can see.

Oct 31 '05 #5
force of habit.

Oct 31 '05 #6

"Chinchilla" <ch********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Chinchilla:

Please don't omit context (I've restored it below)
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Chinchilla wrote:

bd.Cell[x][y].PushUnique((int)(cBuffer[x] - 48));


Why would you write 48 instead of '0' ?? (Really -- I'm interested,
it seems to be a fairly common practice). It is less portable
and less readable and appears to have absolutely nothing in its
favour, as far as I can see.

force of habit.


All the world is not ASCII.

-Mike
Oct 31 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8049
by: Magig Boatman | last post by:
I am completely stumped after many hours pouring through my books, but I need to write fractions. I have a method to convert a string expression representing a fraction as "n/d" into a float and...
6
3441
by: Henry Jordon | last post by:
This is a piece of code that I have left to complete my project. I have hopefully one small error that needs to be fixed. This portion of the code evaluates the postfix notation that is passed to...
5
22007
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
2
6658
by: Sona | last post by:
Hi, I have a char* that holds an ascii character in its first element (at least I think that's what it holds because when I print it, it prints weird characters). I need to convert this into an...
47
46130
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
3
13896
by: bg_ie | last post by:
Hi, I am using a API I downloaded from the internet programmed in C. I need the function below which works with this api in my c++ file - void StoreNoteCallback(void *context, int arglen,...
3
4167
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...
50
5169
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
4154
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
7237
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
7137
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
7416
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
7506
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
5656
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,...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4732
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
779
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.