It is time to protect against just focusing on numbers. In this section, we will cover functions that exist for working v strings and also characters.

You are watching: The cin object must be followed by

String and also character assignment

string exampleString = "Some Pig"; // wire assignmentchar exampleChar = "A"; // char assignment Note: remember #include

cout and strings and characters

Just ar the variable or literal in ~ the common cout statement.


Inputting a string

Using cin

You deserve to use cin however the cin object will certainly skip any kind of leading white space (spaces, tabs, line breaks), then begin reading when it comes to the very first non-whitespace character and then avoid reading as soon as it pertains to the following white space. In other words, it just reads in one word at a time.

string exampleString;cout > exampleString; // entrust input to exampleStringcout


The getline duty reads in whole line including all leading and trailing whitespace as much as the point where return is entered by the user. The result is returned as a wire object.


getline(cin, stringVariable);


string exampleString;cout


The cin object permits easy intake of a solitary character. However, keep in mind that cin will certainly not enable you come input a white room (blank, tab, newline) character.

char exampleChar;cout > exampleChar;cout


The expression cin.get method call the get role of the cin object. You will certainly learn much more about features of objects in thing 7 so for now, just follow the syntax instructions.

The cin.get function when used in a regime will entry the following character, which can be any kind of character, white space or non-whitespace.


There are two ways to speak to or use cin.get.

Return char way

char exampleChar;exampleChar = cin.get(); char as duty argument waychar exampleChar;cin.get(exampleChar);
periodically you will uncover that you should skip or ignore characters that room in the input keyboard buffer. The cin.ignore role allows you to skip one or a collection of characters.


cin.ignore() // skip a solitary character

cin.ignore(n, c) where n is an integer and c is a character. In this form, characters are skipped until the number skipped is same to n or until the personality c is encountered, whichever come first. If the personality c is encountered, it itself is not skipped. It just triggers the skipping come stop.

Using cin.ignore in a program

Let’s consider the following case. We desire the user to go into a number and then select an option specified by a letter or to push to quit.

#include #include using namespace std;int main(){ int number; char optionCharacter; cout > number; cin.ignore(); // clear get in remaining from cin explain cout


examination of the keyboard buffer while this routine runs

After cout keyboard buffer 1 0 0 \n

After cin >> number;

key-board buffer

After cin.ignore();

key-board buffer

After cout key-board buffer \n

After cin.get(optionCharacter);

keyboard buffer
Test yourself
Comment out the line: cin.ignore() in the regimen above. What happens?
Answer: the regimen did not prevent to enable me to get in the a, b, c, or option. It simply said “You quit”. Why walk this happen? the happened because the \n character stayed in the key-board buffer ~ the cin explain to gain the number therefore the cin.get statement entry the \n. It did not have to wait because that my input.

Useful wire member functions and also operators

C++ string objects additionally have a variety of member functions. These attributes are accessed by keying in the string variable name followed by a duration and followed by the role name.

The size function

This role returns the length of the string.

string exampleString = "CS 161";cout

The assign function

The assign function allows you to initialize a string v a specified number of a given character.

string lotsOfAs;lotsOfAs.assign(10, "A"); // collection lotsOfAs to 10 A"scout


Concatenation utilizing + and +=

You deserve to concatenate or join strings using + and +=.

string part1 = "It"s ";string part2 = "Friday";string combinedString = part1 + part2;part1 += part2; // equivalent to part1 = part1 + part2

Using C strings

prior to the introduction of the string course in C++, strings were controlled with what are dubbed C-strings.

C-strings are based on a collection on contiguous 1-byte memory cells referred to as an array. We will comment on arrays in full in chapter 8 so the focus on this discussion is to provide you with a general understanding that this alternate approach to taking care of strings.

Let’s begin with an example.


Remembering the a char data kind requires 1 byte every character on most systems, this statement create a 10 byte sequence (an array) in memory and places the personalities of “Hello” into that room as presented below.

key-board buffer
H e l l o \0

C-strings use a null character, \0, to signify the end of the string. The null personality is instantly appended the finish of a C-string. Thus, our statement sString<10> means we can store up to 9 characters because 1 byte will constantly be offered by the null character.

Assigning a value to a C-string

As presented above, you have the right to assign a value to a C-string with the initialization statement.

char myString<6> = "howdy"; // 5 personalities in howdy so require room for 6 If you specify the char array and also do not provide it an initial value, you must use the strcpy duty to copy personalities into the array.

char anotherString<10>; // carry out not initializestrcpy(anotherString, "bonjour"); // set value through strcpy Note: use of strcpy in Microsoft intuitive Studio outcomes in a compiler error. You should use strcpy_s if you use Microsoft visual Studio.

See more: All Of This Information Is Part Of A ____ In A Database, Microsoft Access Lesson 1

Using cout v C-style strings

To calculation a C-string v cout, just place the name of the selection in the cout statement.