Advertisement
Functions | Description |
---|---|
get() | Reads a single character from a user at the console and returns it. |
get(char& s) | Reads a single character entered by a user at the console and assigns it to a char variable s. |
get(char* array, streamsize n)) | Reads a character or a multi-word string value entered by a user at the console and assigns it to a char array. |
getline(char* array, int length) | Reads a character or a multi-word string value entered by a user at the console and assigns it to a char array. |
getline(istream& is, string& str) | Reads a character or a multi-word string value entered by a user at the console by using the istream object, and assigns it to a string. |
//C++ Using the cin object to read a string
#include<iostream>
using namespace std;
int main()
{
//A char array
char season[20];
//Enter a string
cout<<"Enter your favorite season : ";
//Using the cin object to read a single-word string, which is assigned to a char array
cin>> season;
//Printing the value of char array
cout<<"Your favorite season is : " << season;
return 0;
}
Enter your favorite season : Spring
Your favorite season is : Spring
Advertisement
//C++ By using a cin object we cannot read multi-word strings
#include<iostream>
using namespace std;
int main()
{
//A char array
char country[20];
//Enter a string
cout<<"Enter a country you want to visit : ";
//Using the cin object to read a single-word string, which is assigned to a char array
cin>>country; // cin object can't read a multi-word string
////Printing the value of char array
cout<<"The country you want to visit is : " << country;
return 0;
}
Enter a country you want to visit : United Kingdom
The country you want to visit is : United
get(char* array, streamsize n);
//C++ Using the cin object to read a string
#include<iostream>
using namespace std;
int main()
{
//A char array
char country[30];
//Enter a string
cout<<"Enter a country you want to visit: ";
//Using the cin object with a variant of get function to read a multi-word string, which is assigned to a char array
cin.get(country,30);
//Printing the value of char array
cout<<"The country you want to visit is : " << country;
return 0;
}
Enter a country you want to visit: : New Zealand
The country you want to visit is : New Zealand
get(char& s);
//C++ Calling the get(char& s) function to read a character
#include<iostream>
using namespace std;
int main()
{
//First char
char c;
//Enter a char value
cout<<"Please enter a character : ";
//Calling the get() function to read a character
cin.get(c);
//Printing the char value
cout<< "The value of char is : " << c << "\n";
//Second char
char d;
//Enter a char value
cout<<"\n Please enter a character : ";
//Calling the get() function to read a character
cin.get(d);
//Printing the char value
cout<< "The value in char is : " << d;
//Third char
char e;
//Enter a char value
cout<<"Please enter a character : ";
//Calling the get() function to read a character
cin.get(e);
//Printing the char value
cout<< "The value in char is : " << e;
}
Please enter a character : x
The value of char is : x
Please enter a character : The value in char is :
Please enter a character : y
The value in char is : y
get(void);
//C++ Calling the get(void) function to read a character
#include<iostream>
using namespace std;
int main()
{
//Fist char
char c;
//Enter a char value
cout<<"Please enter a character : ";
//Calling the get() function to read a character
c = cin.get();
//Printing the char value
cout<< "The value in char is : " << c <<"\n";
//Second char
char d;
//Enter a char value
cout<<"Please enter a character : ";
//Calling the get() function to read a character
d = cin.get();
//Printing the char value
cout << "The value in char is : "<< d;
//Third char
char e;
//Enter a char value
cout<<"Please enter a character : ";
//Calling the get() function to read a character
e = cin.get();
//Printing the char value
cout<< "The value in char is : " << e;
}
Please enter a character : h
The value of char is : h
Please enter a character : The value in char is :
Please enter a character : p
The value in char is : p
getline (char* arr, int length);
//C++ Callig the getline(char* arr, int length) function to read a char/string
#include<iostream>
using namespace std;
int main()
{
//A char array
char country[20];
//Enter a character/string value
cout<<"Enter a country you want to visit : ";
//Calling the getline() function to read a character/string value entered at the console
cin.getline(country,20);
//Printing the char/string value
cout<<"The country you want to visit is : " << country;
return 0;
}
Enter a country you want to visit : New Zealand
The country you want to visit is : New Zealand
getline (istream& is, string& str);
//C++ Calling the getline(istream& is, string& str) function to read a char/string
#include<iostream>
using namespace std;
int main()
{
//A string
string country;
//Enter a char/string value
cout<<"Enter a country you want to visit : ";
//Calling the getline() function to read a character/string value entered at the console
//which is assigned to a string
getline(cin, country);
//Priting the char/string value
cout<<"The country you want to visit is : " << country;
return 0;
}
Enter a country you want to visit : South Africa
The country you want to visit is : South Africa
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement