Advertisement
// Using cin function to read a string in C++
#include<iostream>
using namespace std;
int main()
{
char season[20];
cout<<"Enter your favorite season : ";
cin>> season;
cout<<"Your favorite season is : " << season;
return 0;
}
Enter your favorite season : Spring
Your favorite season is : Spring
//cin object cannot read multi-word strings in C++
#include<iostream>
using namespace std;
int main()
{
char country[20];
cout<<"Enter a country you want to visit : ";
cin>>country; // cin object can't read a multi-word string
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
Advertisement
istream& getline (char* arr, int length);
// Using gets() function to read a string in C++
#include<iostream8>
using namespace std;
int main()
{
char country[20];
cout<<"Enter a country you want to visit : ";
cin.getline(country,20);
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
istream& getline (istream& is, string& str);
// Using another variant of getline() function to read a string in C++
#include<iostream>
using namespace std;
int main()
{
string country;
cout<<"Enter a country you want to visit : ";
getline(cin, country);
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