Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcmp conversion from ‘char’ to ‘const char*’

Tags:

c++

char

strcmp

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]
like image 746
Daniel Del Core Avatar asked Dec 12 '25 01:12

Daniel Del Core


1 Answers

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)
like image 106
hmjd Avatar answered Dec 14 '25 18:12

hmjd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!