×

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

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 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.

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.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 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++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Swap numbers in C++

Swap numbers Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.