Reputation: 105
I want to append a char-variable containing the password to a string saying "Your password is: ". When I use the append function for this I get an error saying I can't use this function with string. (I can only use it with unsigned int and char.)
char pass[64];
std::cout << "Enter a password: "; fgets(pass, 64, stdin);
std::string ssifre = "Your password is: "; ssifre.append(sizeof(pass), pass);
If you know another way to solve this, please comment. I don't need to use this function.
Upvotes: 3
Views: 282
Reputation: 598134
ssifre.append(sizeof(pass), pass)
is trying to call the append(size_type count, CharT ch)
overloaded version, which appends ch
count times. That does not work since pass[]
is not a single char
, it is an array of chars.
You need to use the append(const CharT* s)
overloaded version instead (especially since fgets()
ensures that pass[]
will be null-terminated, and append(const CharT* s)
expects a null-terminated char*
string). A fixed char[]
array decays into a char*
pointer to the 1st char
element:
ssifre.append(pass);
That being said, you really shouldn't be using a char[]
array in this situation to begin with. You should be reading in a std::string
instead:
std::string pass;
std::cout << "Enter a password: ";
std::getline(std::cin, pass);
std::string ssifre = "Your password is: " + pass;
Upvotes: 0
Reputation: 1322
This should work:
ssifre += pass;
It's using the += operator
(note that as @MichaelDoubez mentionned in the comments, ssifre.append(pass)
would work, too.)
Upvotes: 4