-1
error: incompatible types in assignment of 'std::__cxx11::string' {aka'std::__cxx11::basic_string<char>'} to 'char [100]'
error: cannot convert 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'const char*'

and I got the above two errors for these lines of codes

    string s1=asctime(localtime(&timetoday));;
    string r=s1.substr(4,6);
    ch=r;                        //char ch[100];(defined aleady)
    //1st error was in line just above this comment
    if(strcmp(r,"Dec 20")==0)
 //2nd error was in line just above this comment
    {
        cout<<"made my first try for creating this program";
    }
    else if(strcmp(r,"Dec 21")==0)
    {
        cout<<"Shortest day of the year";
    }

I'm trying to create a simple remainder program in C++ using code blocks.

RB Editz
  • 1
  • 1

5 Answers5

2

The problem is in the following line:

ch = r;

The variable ch is of type char[100], the variable r is of type std::string. These types are not compatible, you cannot assign an std::string to a char array.

You probably want to write the following instead:

strcpy( ch, r.c_str() );

However, it would be simpler not to use C-style strings at all, and instead use std::string everywhere, like this:

string s1=asctime(localtime(&timetoday));;
string r=s1.substr(4,6);
if( r == "Dec 20" )
{
    cout<<"made my first try for creating this program";
}
else if( r == "Dec 21" )
{
    cout<<"Shortest day of the year";
}

Another mistake in your program seems to be that you are calling strcmp twice, once like this:

strcmp(r,"Dec 20")

And once like this:

strcmp(ch,"Dec 21")

The second one is correct, but the first one is wrong, as you must pass a C-style string (a char array), and not a C++ style string (a std::string). You probably meant to write ch instead of r. Alternatively, if you want to keep using r, you can write r.c_str() instead, which will return a C-style string. But, as already stated above, the best solution would probably be to use std::string everywhere and not use strcmp at all, as it is only intended for C-style strings.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
1

The code is incomplete, i.e. ch is not defined, but you probably want to use r.c_str() in the first call to strcmp, and in the 2nd case r.c_str() instead of ch.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

strcmp compares two cstrings(const char*), you are passing a std::string. try something like ``if(r == "Dec 20") ...```

0

ch = r you are mixing string with char array or c with cpp . Why not make your life easy you can use std::copy in this way

 std::copy(r.begin(),r.end(),ch);

Happy Coding.

Umar Farooq
  • 418
  • 2
  • 14
0

The variable ch is of kind char[100], the variable r is of kind std::string. These sorts are not compatible, you can not assign an std::string to a char array

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
omar KDB
  • 21
  • 2