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

You are watching: The cin object must be followed by

String and character assignment

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

cout and strings and also characters

Just place the variable or literal within the normal cout statement.


Inputting a string

Using cin

You deserve to use cin yet the cin object will certainly skip any leading white area (spaces, tabs, line breaks), then begin analysis when it concerns the first non-whitespace character and also then stop reading once it involves the following white area. In various other words, it only reads in one word at a time.

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


The getline attribute reads in an entire line consisting of all leading and trailing whitespace approximately the suggest wbelow rerevolve is entered by the user. The outcome is changed as a string object.


getline(cin, stringVariable);


string exampleString;cout


The cin object allows simple input of a solitary character. However before, note that cin will not permit you to input a white space (empty, tab, newline) character.

char exampleChar;cout > exampleChar;cout


The expression cin.acquire implies speak to the get feature of the cin object. You will learn even more about features of objects in chapter 7 so for now, simply follow the syntax instructions.

The cin.gain attribute once used in a regimen will certainly input the following character, which have the right to be any character, white space or non-whitearea.


Tbelow are 2 ways to contact or use cin.acquire.

Rerevolve char way

char exampleChar;exampleChar = cin.get(); char as attribute debate waychar exampleChar;cin.get(exampleChar);
Sometimes you will certainly find that you have to skip or ignore personalities that are in the input key-board buffer. The cin.ignore function enables you to skip one or a collection of characters.


cin.ignore() // skip a solitary character

cin.ignore(n, c) wright here n is an integer and c is a character. In this form, personalities are skipped till the number skipped is equal to n or till the character c is encountered, whichever before comes first. If the character c is encountered, it itself is not skipped. It just triggers the skipping to stop.

Using cin.ignore in a program

Let’s consider the following instance. We want the user to enter a number and also then select an alternative specified by a letter or to push to quit.

#encompass #include utilizing nameroom std;int main(){ int number; char optionCharacter; cout > number; cin.ignore(); // clear enter remaining from cin statement cout


Examicountry of the key-board buffer while this regime runs

After cout Keyboard buffer 1 0 0

After cin >> number;

Keyboard buffer

After cin.ignore();

Keyboard buffer

After cout Keyboard buffer

After cin.get(optionCharacter);

Keyboard buffer
Test yourself
Comment out the line: cin.ignore() in the regime above. What happens?
Answer: the regime did not stop to allow me to enter the a, b, c, or choice. It just said “You quit”. Why did this happen? It taken place because the character remained in the key-board buffer after the cin statement to get the number so the cin.gain statement input the . It did not must wait for my input.

Useful string member functions and operators

C++ string objects likewise have actually a number of member features. These features are accessed by keying in the string variable name followed by a period and also complied with by the function name.

The size function

This feature returns the size of the string.

string exampleString = "CS 161";cout

The assign function

The assign function permits you to initialize a string with a stated number of a provided character.

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


Concatenation making use of + and +=

You have the right to concatenate or join strings using + and also +=.

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

Using C strings

Prior to the arrival of the string course in C++, strings were controlled via what are called C-strings.

C-strings are based on a series on contiguous 1-byte memory cells referred to as an variety. We will talk about arrays in complete in chapter 8 so the focus on this conversation is to provide you through a basic expertise of this alternate method to managing strings.

Let’s begin through an instance.


Remembering that a char data type calls for 1 byte per character on many units, this statement creates a 10 byte sequence (an array) in memory and also locations the personalities of “Hello” right into that space as displayed listed below.

Keyboard buffer
H e l l o

C-strings usage a null character, , to represent the end of the string. The null character is automatically appfinished the finish of a C-string. Hence, our statement sString<10> suggests we deserve to save as much as 9 personalities because 1 byte will certainly constantly be supplied by the null character.

Assigning a worth to a C-string

As shown over, you deserve to asauthorize a worth to a C-string with the initialization statement.

char myString<6> = "howdy"; // 5 characters in howdy so need room for 6 If you specify the char array and also perform not give it an initial value, you must usage the strcpy function to copy characters right into the selection.

char anotherString<10>; // carry out not initializestrcpy(anotherString, "bonjour"); // set worth via strcpy Note: use of strcpy in Microsoft Visual Studio outcomes in a compiler error. You need to 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 with C-style strings

To output a C-string through cout, simply place the name of the variety in the cout statement.