-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
  • 12,860
  • 3
  • 14
  • 29
  • THANK YOU SIR/MADAM YOU SOLVED MY 1st ERROR. THANK YOU I CORRECTED MY MISTAKE. – RB Editz Dec 21 '20 at 06:42
  • @RBEditz: If one of the answers solved your problem, then please consider accepting one of the answers. See this official help page for more information: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Andreas Wenzel Dec 21 '20 at 06:52
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
  • 11,844
  • 4
  • 24
  • 32
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
  • 368
  • 1
  • 10
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
  • 8,033
  • 10
  • 17
  • 34
omar KDB
  • 11
  • 1