Alternative to strcpm()

hey guys.... I need some help with the strcmp() function. I need to know an alternative way to write this because strcmp() gives error invalid conversion from 'char' to 'const char*'

Heres the code snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
cout << "Are you sure you want to delete the book? (Y/N)";
    skipBlanks();
    cin >> confirm; 
    
if (strcmp(confirm,"y")==0)
{
            cout << "yes";
            pause();
}

if (strcmp(confirm,"n")==0)
{
            cout <<"no";
            result = false;
            pause();
}
        

if(result == true)
     {
       allTitles[pos] = allTitles[(i-1)];
       allPrices[pos] = allPrices[(i-1)];
       i = i - 1;
       cout << "[Entry has been deleted!]\n\n";
       pause();
      }
    else
     {
       cout << "[The title was not found!]\n\n";
       pause();
      }
       break;


Thanks!!
you can declare it as string and use the member function in string class.
Could you provide an example, I'm not sure what you mean by that.

I understand declaring confirm as a string... what do you mean by use the member function in string class.
Acctually the whole strcmp() function in this case is a little usless. I would just write:

1
2
3
4
5
6
7

if(toupper(compare)=="Y")
{
   //code code code...
}
//...Rest of code


Unless converting it to a pseudo-bool value serves some other purpose?

EDIT: Also do something to only grab the first letter in case ther user types out "yes".
Last edited on
the string class has many member functions and overloaded operators.
in your case ,use a string type to store confirm and then use the operator == to replace strcmp().
like this:
string confirm;
cin>>confirm;
if(confirm=="y")
cout<<"yes"<<endl;
bla bla
is toupper still under string.h lib?

so in this example it would be

1
2
3
4
if (toupper(confirm)=="Y")
{
  blah blah blah
}
Thanks!! both of your methods work!!

Thanks kathy and Computergeek01
Is confirm of type char?

1
2
3
4
if (confirm=='y')
    std::cout <<"Yes.\n";
else if (confirm=='n')
    std::cout <<"No.\n";
@ OP: It should be in the ctype.h header file.

@Kathy L: Welcome to the site, it is always good to have more people here to offer good advice. Please try to use code blocks, as not doing so may encourage users to not use them as well. They are the "<>" box under the format section to the right of the message entry panel.
If all you're going to read is a single character, there's no need for strings. Just use a char and compare like this

if(confirm == 'y')

If you really need a string, std::string has an overloaded operator==, so you could say

if(confirm == "y")

Just a few other things:

There's no need to say something like if(f(x) == 0) because you can say if(!f(x)). Similarly, you don't have to say if(result == true). You can just say if(result).

Finally, when you want to increment (add 1) or decrement (subtract 1) a variable, there's a more direct way of saying that:

--i or ++i

And if you wanted something like i = i - 2 you could write i -= 2.
Yep Wikipedia is your friend, I worked it out using wiki for your method CG..

Thanks filipe... Yes I knew most of those tips, but this was exaggerated for an example.

Thanks all!
Last edited on
Topic archived. No new replies allowed.