std::cin input with spaces?

前端 未结 8 1238
再見小時候
再見小時候 2020-11-21 05:41
#include 

std::string input;
std::cin >> input;

The user wants to enter \"Hello World\". But cin fails at the spa

8条回答
  •  鱼传尺愫
    2020-11-21 06:22

    THE C WAY

    You can use gets function found in cstdio(stdio.h in c):

    #include
    int main(){
    
    char name[256];
    gets(name); // for input
    puts(name);// for printing 
    }
    

    THE C++ WAY

    gets is removed in c++11.

    [Recommended]:You can use getline(cin,name) which is in string.h or cin.getline(name,256) which is in iostream itself.

    #include
    #include
    using namespace std;
    int main(){
    
    char name1[256];
    string name2;
    cin.getline(name1,256); // for input
    getline(cin,name2); // for input
    cout<

提交回复
热议问题