why does my compiler say that I'm converting from a char to a constabt char?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

double convertb10(char number[],int base)
{
    double z = 0;
    double x = 0;
    char conversion[16] = {'0','1','2','3','4','5','6',
    '7','8','9','A','B','C','D','E','F'};
    double convert[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 
     for (int y = 0; y <= 20; y++)
     {
         for (int i = 0; i < 16; i++)
         {
             if (!(strcmp(number[y],conversion[i])))
             {
                x += (convert[i] + pow(10,z));
                z++;
             }
         }
     }
     return x;
}
int main()
{
    char number[20];
    int base, convert;
    cout << "Please enter the number, the base, and the base to be converted: ";
    cin >> number >> base >> convert;
    double b10num = convertb10(number,base);
    
    cout << b10num;

    system("PAUSE");
    return EXIT_SUCCESS;
}



my compiler says that on line 18 there is an invalid conversion from char to const char. what does this mean? I've compared char arrays like this before, I don't know why it's not working now.
Last edited on
closed account (S6k9GNh0)
Change the definition of conversion.
I don't understand, what do you mea, I change conversion to a one dimentional array and it's still the same. what do you mean by change the definition?
I think the actual error is:
invalid conversion from 'char' to 'const char*'
Last edited on
yes, that's it, but what does that mean and what am I doing wrong?
strcmp function takes a pointer (s) to a char (and is used for comparing C style strings). You are passing actual char values.

You don't need to use strcmp to do what you are trying to do.

Instead of using strcmp do

1
2
3
4
5
if ( number[y] == conversion[i] )
{
     x += (convert[i] + pow(10,z));
     z++;
}


Converting char to const char is implicit (meaning you don't have to cast a char to a const char) so in this example const has nothing to do with the problem.

As guestgulkan says, strcmp takes a CHAR POINTER and you are providing a CHAR.
Topic archived. No new replies allowed.