×

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

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

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.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

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.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

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

Octal to Decimal in C++

We need to write a system that converts octal number into equal decimal number when octal number is given as input. Let us look at an example of a program in...

2 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Difference between exit() and _Exit() in C++

Before understanding the difference between the exit() and _Exit(), one must know about exit() and _Exit() functions. The exit() function in C/C++ The exit() method in the C language kills the calling...

3 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

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++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.