strcmp conversion from ‘char’ to ‘const char*’ - c++

I'm having a problem with strcmp.
This is my code.
while (strcmp("m",wood) !=0 || strcmp("j",wood) !=0 || strcmp("o",wood) !=0){
cout << "(m for mahogany, o for oak, or p for pine): ";
cin >> wood;
}
And this is my error:
dining.cpp: In member function ‘void DiningSet::woodType()’:
dining.cpp:76:24: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
dining.cpp:76:48: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
dining.cpp:76:72: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
/usr/include/string.h:143:12: error: initialising argument 2 of ‘int strcmp(const char*, const char*)’ [-fpermissive]

wood is of type char: it must be a string, ie, char*, to be used in strcmp().
Change to:
while ('m' != wood && 'j' != wood && 'o' != wood)

The error shows problem with second argument. It should be of a const char *
The signature of strcmp is:
int strcmp ( const char * str1, const char * str2 );

Related

error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

I get the following error:
2 smartcard.c: In member function ‘virtual bool cSmartCards::ParseLine(const char*, bool)’:
3 smartcard.c:1187:25: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
4 char *r=index(line,':');
5 ^
The code is:
1184
1185 bool cSmartCards::ParseLine(const char *line, bool fromCache)
1186 {
1187 char *r=index(line,':');
1188 if(!r) return false;
I included "string.h"
How can I rewrite line 1187?
index() can be found in string.h.
Either or both of the following:
index returns const char*, not a char*. So, make r a const char*, not a char*.
The function index is written to expect a char*, not a const char*. I cannot safely suggest a fix for this without knowing what index is and does.
The answer is the following:
Function index is found in string.h.
Change line 1187 to: const char *r=index(line,':');

error: conversion from ‘int’ to non-scalar type ‘BIGNUM {aka bignum_st}’ requested

I am trying to use the following openssl function with g++ under Linux
int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
as described here. My example program is as follows:
RSA *rsa;
BIGNUM e = 7;
if (!RSA_generate_key_ex(rsa, 1024, e)) {
cout << "error" << endl;
}
But the error I get during compilation is
src/test.cpp: In function ‘int main(int, char**)’:
src/test.cpp:29:15: error: conversion from ‘int’ to non-scalar type ‘BIGNUM {aka bignum_st}’ requested
src/test.cpp:30:41: error: cannot convert ‘BIGNUM {aka bignum_st}’ to ‘BIGNUM* {aka bignum_st*}’ for argument ‘3’ to ‘int RSA_generate_key_ex(RSA*, int, BIGNUM*, BN_GENCB*)’
make: *** [build/test.o] Error 1
I have no idea what BIGNUM is, nor is it described on the linked page.
You need to supply it with a BIGNUM*.
BIGNUM *e = BN_new();
BN_set_word(e, 7);
if (!RSA_generate_key_ex(rsa, 1024, e, cb)) { /* ... */ }
BN_clear_free(e);
If you have no idea on how cb should be defined, replace it with NULL. That is,
RSA_generate_key_ex(rsa, 1024, e, NULL)

Not sure what Compiler is complaining about, and how to find the solution

I have a huge list of errors that come up when i try to compile source code under cygwin.. My best approach to learning programming is hitting it hard, and trail and error. So even if my C++ knowledge is very basic, I am still really new, so please when you explain can I please ask that you use baby talk for a lack of a better word lol. When I type in 'make' under the source directory is gives me these errors. A friend of mine, we are friends on a MUD, he has been a programmer for 35 years and he says to me that the compiler is not liking that the function is returning a pointer and to change all the "return ''''" to return strdup('''')
Please let me know what you guys think. Thanks
Underneath is only a very smalllll part of the syntax that was given to me after I typed in make in Cygwin. I hope someone has the time to explain this to me, thank you.
$ make
make -s smaug
-Compiling o/imc.o....
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
};
^
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write--strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c: In function ‘char* color_itom(const char*, CHAR_DATA*)’:
imc.c:393:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* color_mtoi(const char*)’:
imc.c:414:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* imccapitalize(const char*)’:
imc.c:525:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[i] = tolower( str[i] );
^
imc.c:527:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[0] = toupper( strcap[0] );
^
imc.c: In function ‘void imc_new_channel(const char*, const char*, const char*, const char*, const char*, bool, int, const char*)’:
imc.c:1089:13: error: conversion to ‘short int’ from ‘int’ may alter its value [-Werror=conversion]
c->level = perm;
^
^
cc1plus: all warnings being treated as errors
Makefile:101: recipe for target 'o/imc.o' failed
make[1]: *** [o/imc.o] Error 1
Makefile:46: recipe for target 'all' failed
make: *** [all] Error 2
Ok below is the code where it shows error for line 106: 1 and 393: Its a very lonnng .c file I am sure you guys don't want to upload the whole thing, but here is the portion of it, and according to Visual 2013 here is starting point line 106 and 393: i am not sure when cygwin says the line number where the error took place if that doesn't include white space and comments, but here is 106 and 393 according to VS:
line 106
SITEINFO *this_imcmud;
line 393
if( IMCIS_SET( IMCFLAG( ch ), IMC_COLORFLAG ) )
You should show your code, but your problems are:
Somewhere you're doing something like:
char *x = "hello";
It should be:
const char *x = "hello";
Similarly, char* color_itom(const char*, CHAR_DATA*) should return const char * if you want to return string literals from it.
strcap is defined as a char array, but you're putting the int values returned by tolower and toupper in there somewhere. Either change the type or put in an explicit cast.
Same for c->level = perm. Either add an explicit cast or change the type of c->level to match the type of perm.
The error:
deprecated conversion from string constant to ‘char*’
is caused by using string literal to initiaize char*, e.g.
char* str = "something";
This should read:
const char* str = "something";

strcmp[c++] error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

Where am doing wrong in this code? I need only in char types, please don't suggest to use std::string.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *mystring="C:/windows";
char last_char;
last_char = mystring[strlen(mystring)-1];
cout<<"Input: " <<mystring<<endl;
if(strcmp(last_char,";")!=0)
{
strcat(mystring,";");
}
cout<<"Output: "<<mystring<<endl;
return 0;
}
Output:
Compilation error time: 0 memory: 3340 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *mystring="C:/windows";
^
prog.cpp:11:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
if(strcmp(last_char,";")!=0)
^
In file included from prog.cpp:2:0:
/usr/include/string.h:140:12: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
extern int strcmp (const char *__s1, const char *__s2)
Don't use strcmp, it expects a null terminated characters sequence. Instead, use direct comparison:
if (last_char == ';') ...
Also, your code invokes undefined behavior in the strcat() call. my_string was initialized with a string literal, thus, you are not allowed to modify it, since the implementation is free to place it in read-only memory (and typically will do so).
You can declare it like this instead:
char mystring[12] = "C:/windows"; // space for one more char
last_char is not a string. It is a character. You can't compare a char with string.
Try this instead
if (last_char == ';') {...}
Statement
strcat(mystring,";");
invokes undefined behavior. You can't modify a string literal as it resides in read only section of the memory.

invalid conversion from 'const char*' to 'std::basic_ostream<char>::char_type {aka char}'

Can't get around this problem
ofstream out;
out.open("o");
string a[5][5];
//fill array with letters from 'in.get(ch)'...and then i've tryed:
//1
out.put(a[row[0]][col[1]].c_str()); //=>invalid conversion from 'const char*' to 'std::basic_ostream<char>::char_type {aka char}'
//2:
out.put(const_cast<char *>(a[row[0]][col[1]].c_str())); //=>invalid conversion from 'char*' to 'std::basic_ostream<char>::char_type {aka char}'
//3
char x=const_cast<char *>(a[row[0]][col[1]].c_str());
out.put(x); //=>invalid conversion from 'char*' to 'char'
Nothin seems to be working. Can you help me? What should i do?
ofstream::put() is for putting individual characters, not strings, to the stream.
If you want to print the string to the file stream, why not simply use <<:
out << a[row[0]][col[1]];