String input in C and C++ -
String input in C and C++ -
i want write programme in c or c++ asks user come in string @ run time of different lengths @ different times given user (either space separated or non space separated) , store array. please give me sample code in c , c++ it.
e.g.
1st run: come in string input: foo now char array[]="foo";
2nd run: come in string input: pool of now char array[]="pool of";
i have tried:
#include<iostream> using namespace std; int main() { int n; cout<<"enter no. of chars in string"; cin>>n; char *p=new char[n+1]; cout<<"enter string"<<endl; cin>>p; cout<<p<<endl; cout<<p; homecoming 0; } but it's not working when string space separated.
i have tried too, it's not working either.
#include <iostream> using namespace std; int main() { int n; cout<<"enter no. of chars in string"; cin>>n; char *p=new char[n+1]; cout<<"enter string"<<endl; cin.getline(p,n); cout<<p<<endl; cout<<p; homecoming 0; }
use getline.
take example: http://www.cplusplus.com/reference/iostream/istream/getline/
c++
Comments
Post a Comment