If you want to take whole line as an input than use getline() function.
If you want to input a string from user "character by character" than use getche()
If you want to input a string from user "character by character without echoing to screen" than use getch(). This method is useful when we want user to enter password, because as user enter the string it will not be seen on screen.
Method 1 : C++ program to input a string using cin
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[20];
cout<<"Enter the string : ";
cin>>str;
cout<<"You have entered : "<<str;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
char str[20];
cout<<"Enter the string : ";
cin>>str;
cout<<"You have entered : "<<str;
return 0;
}
Method 2 : C++ program to input a string using gets() function
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
cout<<"Enter the string : ";
gets(str);
cout<<"You have entered : "<<str;
return 0;
}
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
cout<<"Enter the string : ";
gets(str);
cout<<"You have entered : "<<str;
return 0;
}
Method 3 : C++ program to input a string character by character from user
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[20];
int i=0;
cout<<"Enter the string : ";
for(i=0;int(str[i]=getche())!=13;i++);
str[i]='\0';
cout<<endl<<"You have entered : "<<str;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
char str[20];
int i=0;
cout<<"Enter the string : ";
for(i=0;int(str[i]=getche())!=13;i++);
str[i]='\0';
cout<<endl<<"You have entered : "<<str;
return 0;
}
Method 4 : C++ program to input a string character by character from user without echoing to screen
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[20];
int i=0;
cout<<"Enter the string : ";
for(i=0;int(str[i]=getch())!=13;i++);
str[i]='\0';
cout<<endl<<"You have entered : "<<str;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
char str[20];
int i=0;
cout<<"Enter the string : ";
for(i=0;int(str[i]=getch())!=13;i++);
str[i]='\0';
cout<<endl<<"You have entered : "<<str;
return 0;
}
Method 5 : C++ program to input string with spaces
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[50];
cout<<"Enter Sentence : ";
cin.getline(str,50);
cout<<"You have entered : "<<str;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
char str[50];
cout<<"Enter Sentence : ";
cin.getline(str,50);
cout<<"You have entered : "<<str;
return 0;
}
C++ program to take n names as input from user
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char *str[10];
int n;
cout<<"How many names to be entered : ";
cin>>n;
cout<<"Enter names : ";
for(int i=0;i<n;i++)
{
str[i]=new char[20];
cin>>str[i];
}
cout<<endl<<"You have entered : ";
for(int i=0;i<n;i++)
cout<<endl<<i+1<<".) "<<str[i];
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
char *str[10];
int n;
cout<<"How many names to be entered : ";
cin>>n;
cout<<"Enter names : ";
for(int i=0;i<n;i++)
{
str[i]=new char[20];
cin>>str[i];
}
cout<<endl<<"You have entered : ";
for(int i=0;i<n;i++)
cout<<endl<<i+1<<".) "<<str[i];
return 0;
}
No comments:
Post a Comment