14

I'm using gcc on codeblocks and I'd like to write a function that uses an array of records.

However I keep getting the error:

invalid conversion from 'int' to 'const char*'

The code:

#include <iostream>
#include <string>

using namespace std;

struct rendeles {
    string nev;
int mennyiseg;
};

struct teaceg {
string nev;
int mennyiseg;
};

int szam; 
struct rendeles rendelt [100];      
struct teaceg cegek [100];          
int h;

int hanyadikceg (string cegnev)
{                            
    for (int i=0;i<szam;i++)
    {
        if (cegek[i].nev==cegnev)
            {
                return i;
            }
    }
    return -1;
}

int main()
{
    cout << "Hány db rendelés lesz összesen?";
    cin >> szam;
    if (szam > 100)
    {
        cout << "Hiba: túl nagy a rendelések száma! (100 a maximum)";
        return -1;
    }

    for (int i=0;i<szam;i++)        
    {
        cout << "A(z) " << i+1 <<". cég neve:";
        cin >> rendelt[i].nev;                                 
        cout << "A(z) " << i+1 <<". rendelés mennyisége:";
        cin >> rendelt[i].mennyiseg;                           
    }
    cout << endl;

    h = hanyadikceg('Lipton');              //the problem is in this line
    cout << "Hanyadik cég a xyz:" << h;

    for (int i=0;i<szam;i++)          
    {
        cout << "A(z) " << i+1 << ". rendelés: " << rendelt[i].nev << " " <<     rendelt[i].mennyiseg << endl;
    }

    return 0;
}

What causes this error?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
GregT
  • 1,039
  • 4
  • 14
  • 34
  • 2
    btw: having function and variable names in the lingua franca of programming makes it far easier to get help, since far more people are able to understand the code. – PlasmaHH Mar 14 '12 at 13:05
  • Just curious, what language is that (I can't tell)? – Jesse Good Mar 14 '12 at 13:09
  • 1
    @Jesse, I believe it is Hungarian. – Moo-Juice Mar 14 '12 at 13:10
  • @Jesse: google translate autodetection claims it is hungarian. – PlasmaHH Mar 14 '12 at 13:11
  • Yes, it's Hungarian. I wrote it in this language, because it was a school assignment, and we used all variables in Hungarian. But I know it's better to use English for most purposes. Btw, I don't know how this became a "famous question"? – GregT Sep 04 '16 at 20:09

4 Answers4

20

You need to use double-quotes (") for string literals, not single-quotes (').

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
4

You are using single quotes ('Lipton'). Single quotes are for char-literals.

Use "Lipton", for a const char* literal.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
4

Change 'Lipton' to "Lipton" in the problem line and the compilation error will go away.

In C/C++, double-quoted expressions are strings (or, technically, string literals) and resolve to char * or const char * type, while single-quoted expressions are characters (or character literals) and resolve to char type (which can be implicitly casted to int). That explains your error: the compiler cannot convert the char integer type to the const char * that the function signature requires.

George Skoptsov
  • 3,831
  • 1
  • 26
  • 44
  • Just a note. String literals are *always* resolved to `const`. – Moo-Juice Mar 14 '12 at 13:58
  • 1
    String literals in C++ have type `const char[N]`, where N is the length of the string including nul terminator. There's an implicit conversion to `const char*`, and in C++03 there's a deprecated conversion to `char*` for compatibility with C. IIRC, the deprecated conversion is removed in C++11. The function `hanyadikceg` calls for a `std::string`, and there's another implicit conversion from `const char*` to `std::string`. – Steve Jessop Mar 14 '12 at 14:05
2
h = hanyadikceg('Lipton');

should be

h = hanyadikceg("Lipton");
Lou
  • 1,955
  • 14
  • 16