store a full length of name in C++

In the program given here, we will discuss how to store a complete name in C++.

In this program, we will ask the user to input two names. Both names are stored differently in the program.

Here is the program,

#include<iostream.h> 
#include<conio.h>
#include<stdio.h>    // for gets() function
void main()
{
   char name1[20],name2[20];  // declaration of variable name1,and name2;

   cout<<"Enter Name: "; cin>>name1;

   cout<<"Enter Name: ";
   gets(name2);
     // print values of these variables
   cout<<"Name: "<<name1;
   cout<<"Name: "<<name2;
 getch();
}
OUTPUT:-
 Enter name: Rahul_sherma 
 Enter name: Rahul sherma

 Name: Rahul_sherma
 Name: Rahul sherma

explanation:-

Two variable name1 and name2 have been declared in the program. In which the name entered by the user will be stored.

suppose both given name is rahul sherm, so

name1 = rahul sherma
name2 = rahul sherma

But here the variable name1 is used with the cin statement,

cin>>name1;

and because, the cin statement does not read the space. Therefore,variable name1 will only store Rahul,

While variable name2 is used with gets() functon,

gets(name2);

so variable name will store the complete name(with space).

such as,

store-name-in-cpp

Remember, where we have to include the header file iostream.h in the program to use the cin statement. To use the gets() function, we have to include its header file stdio.h in the program.

Instead of the gets() function we can also use the getline() function.

We will discuss these functions in string