Dec 12, 2010 at 10:03am UTC
I have this bit of code here:
char answer1[11];
int answer2[10];
...
for(initializer=0;initializer<10;initializer++)
answer2[initializer]=atoi(answer1[initializer2]);
...
and it comes up with the following errors:
C:/C++/Projects/Base_converter_calculator/Calculation.cpp:27: error: invalid conversion from 'char' to 'const char*'
C:/C++/Projects/Base_converter_calculator/Calculation.cpp:27: error: initializing argument 1 of 'int atoi(const char*)'
Can somebody help me?
Last edited on Dec 12, 2010 at 12:00pm UTC
Dec 12, 2010 at 1:26pm UTC
why dont you just use something like
int answer[10];
for(int x = 0; x<=10;x++){
cin.get(answer[x]); //afaik this should return just one value, could be wrong though.
}
you can also use
#include <conio.h>
int answer[10];
for(int x = 0; x<=10;x++){
answer[x] = getch() - '0';
}
both of these examples would just get the integers straight from the input of the keyboard.
Or do you need it for some other purpose?
Dec 14, 2010 at 5:55am UTC
do not use function atoi(), because this function takes a const char * as parameter, but you give a char in your program. You can change the statement answer2[initializer]=atoi(answer1[initializer2]);
to
answer2[initializer]= answer1[initializer2] - '0';
I think it will work fine.
Dec 24, 2010 at 10:56pm UTC
quick, your code worked perfectly first time but when I use it like this:
int answer1[10];
int answer3[10];
char answer2[11];
char answer4[11];
for(i1=0;i1<10;i1++)
answer1[i1] = 0;
for(i2=0;i2<10;i2++)
answer2[i2] = 0;
for(i3=0;i3<10;i3++)
answer3[i3] = 0;
for(i4=0;i4<10;i4++)
answer4[i4] = 0;
...
answer1[i1]= answer2[i2] - '0';
answer3[i3]= answer4[i4] - '0';
...
answer2 and answer4 are numbers in a character state. answer1 and answer 3 would be a random number near answer2 and answer4.
Can somebody help?
Last edited on Dec 25, 2010 at 6:42pm UTC
Jan 29, 2011 at 11:55pm UTC
First of all, you are converting 1 byte to 4 byte array. So try this....
char a1[11];
int a2[10];
for (int i=0;i<10;i++)
{
memcpy(a2+i,a1+i,1);
};
OR
memcpy(a2,a1,10);
atoi for strings only!!