我想从我输入的字符串(很长)中连续用cin.getline()分给两个string 变量,然后打印出来,但是第二个变量打印不出来, 有人知道为什么吗
#include <iostream>
using namespace std;
int main() {
char* string1;
char* string2;
cout<<"please enter a string: "<<endl;
//cin.get(string1,5).get(string2,2);
cin.getline(string1,5);
cin.getline(string2,5);
cout<<string1<<endl;
cout<<string2<<endl;
return 0;
}
/Users/taoxiang/CLionProjects/test/cmake-build-debug/test
please enter a string:
sdfsdfsssss
sdfs
Process finished with exit code 11
#include <iostream>
using namespace std;
int main() {
char* string1;
char* string2;
cout<<"please enter a string: "<<endl;
//cin.get(string1,5).get(string2,2);
cin.getline(string1,5);
cin.getline(string2,5);
cout<<string1<<endl;
cout<<string2<<endl;
return 0;
}
/Users/taoxiang/CLionProjects/test/cmake-build-debug/test
please enter a string:
sdfsdfsssss
sdfs
Process finished with exit code 11