2

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]
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
  • 4
    If you want to compare two `char`just use `==` or `!=`, if you want to compare two strings(`char *`) then use `strcmp`. It doesn't make any sense to compare a single character to a character string, which is what you seems are doing. – Alok Save Mar 13 '12 at 11:05
  • what's the datatype for wood..?? –  Mar 13 '12 at 11:05

2 Answers2

12

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)
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • This answer is correct, @Daniel D C, add to that what my comment says under the Q for better understanding. – Alok Save Mar 13 '12 at 11:09
  • @DanielDC I think take a look at [char and string literals](http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc02ccon.htm) in addition too the answer :) – T I Mar 13 '12 at 11:11
  • Thanks guys ! appreciate the quick response ! – Daniel Del Core Mar 13 '12 at 11:12
  • char compare only use "==" and with single char include with " ' " – zszen Nov 16 '15 at 09:27
0

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 );

Sanish
  • 1,699
  • 1
  • 12
  • 21