eddy bader
8
Q:

input a string in c++

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
// CPP program to convert string 
// to char array 
#include <iostream> 
#include <string.h> 
  
using namespace std; 
  
// driver code 
int main() 
{ 
    // assigning value to string s 
    string s("geeksforgeeks"); 
    // declaring character array : p 
    char p[s.length()]; 
  
    int i; 
    for (i = 0; i < sizeof(p); i++) { 
        p[i] = s[i]; 
        cout << p[i]; 
    } 
    return 0; 
} 
0

  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