C string input

0 votes

According to my book, if I want to read a string with a maximum length of 40 characters, I must declare a variable with a length of 41, but it does not explain why.

char name[41];
cin >> name;
cout << name << endl;

I realise this is a novice question, and I am a C++ rookie.

I'm hoping you can assist me. 

Thanks.
 

EDIT:
 

Thank you for your responses; I didn't expect to learn so much in such a short period of time.

Who is responsible for appending the NULL terminator at the end of the char array?

What happens to the "n" once I press the Enter key?

Thank you one again.

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
540 views

1 answer to this question.

0 votes

To read from cin, use a std::string. 

Then you don't have to guess how big your buffer should be.

std::string input;
cin >> input;
cout << intput;

If you need a C-style array you can do:

const char* cstyle = input.c_str();

To signify the conclusion of a C-style string, the last character is invariably the null terminator '0'. 

It is critical to understand when your character sequence stops.

Some example:

char* text = "hello"; // the compiler puts an extra '\0' at the end
std::string str("hello"); // does not have a null terminator! (before C++11)
str.c_str(); // this returns "hello\0" with a null terminator

answered Jul 11, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
439 views
0 votes
1 answer

C++ string in classes

The std namespace contains the string class. You need change the class to something like this: class Language { public: ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,236 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
5,658 views
0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
469 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
704 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,431 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,465 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,889 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,424 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,109 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP