Discuss the problem of array and string input (C + + Edition) _c language

Source: Internet
Author: User
Tags arrays first string

For the string problem, the original understanding is not deep enough, now discuss some questions about string input

1.strlen () returns the length of the string in the array, not the length of the array itself.
2.strlen () evaluates only the visible characters, not the null characters.

So more interesting in the back:

Char name[16] = "ABCDEFG";
How much is the output result?
cout << name << Endl;
NAME[3] = ' the ';
How much is the output result?
cout << name << Endl; 

Let's guess.

# include <iostream>
# include <cstring>
# define SIZE
using namespace std;
int main (void)
{
  char name_cin[size];
  Char name[size] = "C++owboy"; Initialized array
  cout << "Hello I ' m" << name;
  cout << "! What is your name? ";
  Cin >> Name_cin;
  cout << "OK" << name_cin << ", your name has";
  cout << strlen (name_cin) << "letters and is stored" << Endl;
  cout << "In an array of" << sizeof (name_cin) << "bytes." << Endl;
  cout << "Your initial is" << Name_cin[0] << "." << Endl;
  NAME[3] = ' the ';
  cout << "Here are the 3 characters ' my name:";
  cout << name << Endl;
  return 0;
}

Let's guess what the result is?

The name string was truncated ...

Interpretation:
Arrays can use indexes to access individual characters of an array, such as name[0] to find the first character of an array, name[3] = '; Set to a null character so that the string ends after the third character, even if there are other characters in the array.

There is a flaw in Cin, however, that with whitespace as the end sign, if you encounter a space and enter the string, you will need to use a string to enter the method to solve, but first look at this problem:

# include <iostream>
using namespace std;
int main (void)
{
 const int arsize =;
 Char name[arsize];
 Char dessert[arsize];
 cout << "Enter Your Name:" << Endl;
 CIN >> name; Enter the name
 cout << "Enter your favorite dessert:" << Endl;
 CIN >> Dessert; Enter the name of the dessert
 cout << "I have some delicious" << dessert;
 cout << "For You," << name << "." << Endl;
 return 0;
}


Interpretation:

CIN uses whitespace (spaces, tabs, line breaks) to set the bounds of the string, CIN reads only the first word when it gets the character array input, and after reading the word, CIN places the string in the array and automatically adds the null character ' yes ' at the end.
CIN takes Meng as the first string and puts it in an array, Liang the input queue Liang when it is placed in the input queue for the second time, because the CIN reads the Liang and places it in the dessert array

If you can enter a row of data, this problem is not resolved?
Getline (), get () can implement ...

 # include <iostream>
using namespace std;
int main (void)
{
  const int arsize =;
  Char name[arsize];
  Char dessert[arsize];
  cout << "Enter you name:" << Endl;
  Cin.getline (name,arsize);
  cout << "Enter you favorite dessert:" << Endl;
  Cin.getline (dessert,arsize);
  cout << "I have some delicious" << dessert;
  cout << "For You," << name << Endl;
  return 0;
}

Interpretation:

CIN is a word as input, and sometimes we need to take a line as input, such as I love C + +
The class in iostream provides a number of line-oriented class member functions, such as getline () and get (), both of which read one row of input until the line break ends, and the difference is that getline () will discard line breaks
Get () to keep line breaks in the input sequence
Line-oriented input: getline (char* cha,int num)
Getline () reads the entire line, and the end is determined by a newline character, which can be used using Cin.getline (char* cha,int num), a member function, the first parameter is the name of the array to store the input rows, and the second is the number of characters to read. If the argument for this character number is 30, it reads up to 29 characters, and the remainder is used to store the empty characters that are automatically added at the end.
Get () ends with a null character when the string is stored.

What if this situation is met?

# include <iostream>
using namespace std;
int main (void)
{
 cout << "What year is your house built" << Endl;
 int year;
 CIN >> Year;
 char ch;
 Cin.get (CH); Receives line breaks (CIN >> year). get ();
 cout << "What is it street address?" << Endl;
 Char address[80];
 Cin.getline (address,);
 cout << "Year built:" << year << Endl;
 cout << "Address:" << address << Endl;
 cout << "Done!" << Endl;
 return 0;
}
 

The address has not been entered, it is over ...
Remove the above note, add a character, to receive line breaks ...
Note: C + + programs often use pointers rather than arrays to handle strings

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.