Mark44
Mentor
- 38,034
- 10,507
Out of curiosity, what's the title of the book you're working out of, and who is the author?I hope it's not Herb Schildt.
Assuming that you use '\0' as the initializer, all the other 19 array elements also get this value.
I changed your code to the following:
The first parameter is the source of the copy, the second is the max. number of characters to copy, and the third is the destination of the copy.
Edit: My code did what it was supposed to, but my explanation was wrong. The first parameter is the destination string, second is the max. number of characters, and the third is the source of the copy.
c_str() is a function that converts a C++-style string to a C-style null-terminated array of type char. One of the very confusing things about C++ and its standard template library is that both types of string are still present -- the C-type array of char, and the C++ string class. The program you showed mixes both types, plus it uses a C function (strcpy) that Microsoft doesn't support any longer.
Here's my version of the program, one that builds with no warnings or errors.
Notice that I don't have using namespace std;
The strcpy_s() function is documented here: https://docs.microsoft.com/en-us/cp...rence/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019
The null character (ASCII code 0) should be '\0', not '/0'. All of the special control characters are represented with a backslash; e.g, '\n' (new line), '\t' (tab), and so on.yungman said:Also I have questions:
1) Line 11 is: char copyInput[20] = { '/0' }. Does this mean it fills all 20 elements with {'/0'} null character?
Assuming that you use '\0' as the initializer, all the other 19 array elements also get this value.
First off, since you're using VS, you shouldn't use strcpy(), since that has been deprecated for the past two or three versions of VS. Instead, use strcpy_s(), the secure version of this function.yungman said:2) Line 14 is: strcpy(copyInput, userInput.c_str()). Why having .c_str()? what does this mean?
I changed your code to the following:
C++:
strcpy_s(copyInput, 20, userInput.c_str());
Edit: My code did what it was supposed to, but my explanation was wrong. The first parameter is the destination string, second is the max. number of characters, and the third is the source of the copy.
c_str() is a function that converts a C++-style string to a C-style null-terminated array of type char. One of the very confusing things about C++ and its standard template library is that both types of string are still present -- the C-type array of char, and the C++ string class. The program you showed mixes both types, plus it uses a C function (strcpy) that Microsoft doesn't support any longer.
Here's my version of the program, one that builds with no warnings or errors.
C++:
#include <iostream>
#include <string>
#include <string.h>
using std::cout; using std::cin; using std::endl;
using std::string;
int main()
{
cout << " Enter a line of text: ";
string userInput;
getline(cin, userInput);
char copyInput[20] = { '\0' };
if (userInput.length() < 20)// check bound.
{
strcpy_s(copyInput, 20, userInput.c_str());
cout << " CopyInput contains: " << copyInput << endl;
}
else
cout << " Bound exceed, won't copy!" << endl;
}
The strcpy_s() function is documented here: https://docs.microsoft.com/en-us/cp...rence/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019
Last edited: