Beth
0
Q:

how to take input from string in c++

// C++ program to demonstrate 
// anomaly of delimitation of 
// getline() function 
#include <iostream> 
#include <string> 
using namespace std; 
  
int main() 
{ 
    string name; 
    int id; 
  
    // Taking id as input 
    cout << "Please enter your id: \n"; 
    cin >> id; 
  
    // Takes the empty character as input 
    cout << "Please enter your name: \n"; 
    getline(cin, name); 
  
    // Prints id 
    cout << "Your id : " << id << "\n"; 
  
    // Prints nothing in name field 
    // as "\n" is considered a valid string 
    cout << "Hello, " << name 
         << " welcome to GfG !\n"; 
  
    // Again Taking string as input 
    getline(cin, name); 
  
    // This actually prints the name 
    cout << "Hello, " << name 
         << " welcome to GfG !\n"; 
  
    return 0; 
} 
4
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
10
// C++ program to understand the use of getline() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    string S, T; 
  
    getline(cin, S); 
  
    stringstream X(S); 
  
    while (getline(X, T, ' ')) { 
        cout << T << endl; 
    } 
  
    return 0; 
} 
1

  string firstName; //unlike array, no need to mention the size of the sting
cout << "Type your first name: "; 
cin >> 
  firstName; //this is important
  // get user input from the keyboard
cout << "Your name is: " << 
  firstName;

// Type your first name: John
// Your name is: John 
0

New to Communities?

Join the community