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*'
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;
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
@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.