std::string has a overloaded operator==, so you can use it for comparison. You can't do the same with raw arrays.
slackPLUSPLUS wrote:
isn't that equivalent to a string? using the double quotes?
No. "bob" is a string literal, i.e. a constant, null-terminated char array. When you assign that to a char pointer, it will point to the first character of the string literal. That's all d is - a primitive pointer.
Not quite, you need to dereference the pointer first the get the char value it's pointing to: if (*name == 'b')
But yeah, that would compare the first character.
slackPLUSPLUS wrote:
How would I make a conversion from char type to string?
std::string has a contructor that takes a const char*. So you can do the following:
That's not the proper usage for the results you want, since now "bill" or "b" or anything that starts with a b is a valid input. Use strings as suggested:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
usingnamespace std;
int main()
{ string name;
cout << "Please input your name: ";
getline(cin, name); // Use getline for inputting strings
if (name == "bob")
{ // do stuff
}
// Pause your program here
return 0;}
Eww, no. strcmpi is not even part of the C++ (or C) standard. strcmp is, but there is no place for that in a C++ program.
Also, don't confuse the terms "same" and "equal".