×

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the user to store a collection of characters, like "World" or "January 1st is the new year".

As with the other data types, a string must first be declared before a value can be stored in it.

Strings in C++

Declaring strings in C++ programming starts by giving them a starting value and utilizing them in a variety of input and output tasks. A string is a group of characters or an array of characters. The C++ programming language typically uses two types of strings:

  • Strings that are objects of the string class (The string class is available in the Standard C++ Library).
  • The C-Strings (C-style Strings)

The sequence of characters is kept in array form in C language. Programming in C++ also offers this. Therefore, it is known as C-strings.

C-strings are arrays of type char that end with the character "\0" or the null character.

(The null character's ASCII value is 0).

Declaring a C-string

char s[] = “String” ;

The string s in the preceding code holds seven characters.

Despite the fact that "String" has six characters, the null character \0 is automatically appended to the end of the string.

Different approaches to declaring a string

char s[7] = “String” ;


char s[] = {'S', 't', 'r', 'i', 'n', 'g', '\0'} ;


char s[7] = {'S', 't', 'r', 'i', 'n', 'g', '\0'} ;

It's not required to fill the whole space allotted for the string, just like with arrays.

Example

char s[100] = “String” ;

Reading a name using C++ String

A C++ program that displays a user-entered string. By using this program, the user can enter a name.

Program

#include <iostream>
using namespace std ;


int main()
{
    char s[100] ;


    cout << "Enter the name: " ;
    cin >> s ;
    cout << "Entered name is " << s << endl ;


    cout << "\nEnter another name: " ;
    cin >> s ;
    cout << "Entered name is "<<s<<endl ;


    return 0 ;
}
How to enter a name in C++

Output 2

How to enter a name in C++

It is worth noting that in the second output, only "rohit" is displayed rather than "rohit sharma."

This is because the extraction operator >> behaves similarly to scanf() in C and regards a space " " as a terminating character.

C++ String for reading the full name (a line of text)

C++ program reads and displays an entire line of text entered by the user.

Program:

#include <iostream>
using namespace std ;


int main()
{
    char s[100] ;
    cout << "Enter a name: " ;
    cin.get(s, 100) ;


    cout << "The entered name is " << s << endl ;
    return 0 ;
}

The cin.get() function can be used to read text that contains blank spaces. This function accepts two arguments.

The first argument is the string's variable name (the address of the first element of the string), and the second argument is the maximum size of the array.

In the preceding program, s is the string's variable name, and 100 is the array's maximum size.

How to enter a name in C++

String Object

A string object can also be made in C++ to hold strings. Unlike char arrays with a set length, string objects can be extended as needed.

Program

C++ string using the string data type to accept user input.

#include <iostream>
using namespace std ;


int main()
{
    // Declaring a string object
    string s ;
    cout << "Enter a string: " ;
    getline(cin, s) ;


    cout << "You entered: " << s << endl ;
    return 0 ;
}
How to enter a name in C++

The string s is declared in this code. The user is then prompted for the string. You can obtain the entered line of text using getline() rather than cin>> or the cin.get() function.

The first parameter of the getline() function, cin, is the input stream, and s is the line's location that must be stored.

Running a function with a string (String is passed)

Similar to how arrays are supplied to a function, strings are also given to functions.

Program:

#include <iostream>
using namespace std ;


void display(char *) ;
void display(string) ;


int main()
{
    string s1 ;
    char s[100] ;


    cout << "Enter a name: " ;
    getline(cin, s1) ;


    cout << "Enter another name: " ;
    cin.get(s, 100, '\n') ;


    display(s1) ;
    display(s) ;
    return 0 ;
}


void display(char a[])
{
    cout << "Entered name using char array is " << a << endl ;
}


void display(string a)
{
    cout << "Entered name using string is " << a << endl ;
}

In the code mentioned above, two strings must be entered. These are kept in the variables s, and s1, where s is a char array and s1 is a string object, respectively.

The string is then displayed using two procedures called display().

The parameter is the only distinction between the two functions. The first display() function accepts a parameter of the type char array, while the second takes a parameter of the type string.

Overloading functions is the term used to describe the activity.

How to enter a name in C++

Related Topics

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Vector Size in C++

What are Vectors?  In the C++ programming language, vectors are run-time sequence containers representing arrays with variable sizes, which are contained within STL (Standard Template Library). They utilize contiguous storage spaces...

6 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

How to improve programming skills in C++

Before getting started, one should know why to improve their programming skills. To become a good software developer or programmer, one must be skilled in at least one programming language. Many...

4 minutes read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.