C++ User Input Strings

User Input Strings

It is possible to use the extraction operator >> on cin to store a string entered by a user:

Example

string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;

// Type your first name: John
// Your name is: John

However, cin considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only store a single word (even if you type many words): Continue reading C++ User Input Strings

C++ Special Characters

Strings – Special Characters

Because strings must be written within quotes, C++ will misunderstand this string, and generate an error:

string txt = “We are the so-called “Vikings” from the north.”;

The solution to avoid this problem, is to use the backslash escape character. Continue reading C++ Special Characters

C++ Access Strings

Access Strings

You can access the characters in a string by referring to its index number inside square brackets [].

This example prints the first character in myString : Continue reading C++ Access Strings

C++ String Length

String Length

To get the length of a string, use the length() function : Continue reading C++ String Length

C++ String Concatenation

String Concatenation

The + operator can be used between strings to add them together to make a new string. This is called concatenation:

Example

string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;

In the example above, we added a space after firstName to create a space between John and Doe on output. However, you could also add a space with quotes (" " or ' '): Continue reading C++ String Concatenation

C++ Strings

C++ Strings

Strings are used for storing text/characters.

For example, “Hello World” is a string.

A string variable contains a collection of characters surrounded by double quotes: Continue reading C++ Strings

C++ Logical Operators

Logical Operators

As with comparison operators, you can also test for true (1) or false (0) values with logical operators.

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example Try it
&& Logical and Returns true if both statements are true x < 5 &&  x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

Continue reading C++ Logical Operators

C++ Comparison Operators

Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either 1 or 0, which means true (1) or false (0). These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else article. Continue reading C++ Comparison Operators

C++ Assignment Operators

Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: Continue reading C++ Assignment Operators