taking input of a string word by word

后端 未结 3 1251
小鲜肉
小鲜肉 2020-12-02 13:53

I just started learning C++. I was just playing around with it and came across a problem which involved taking input of a string word by word, each word separated by a white

3条回答
  •  情书的邮戳
    2020-12-02 14:20

    getline is storing the entire line at once, which is not what you want. A simple fix is to have three variables and use cin to get them all. C++ will parse automatically at the spaces.

    #include 
    using namespace std;
    
    int main() {
        string a, b, c;
        cin >> a >> b >> c;
        //now you have your three words
        return 0;
    }
    

    I don't know what particular "operation" you're talking about, so I can't help you there, but if it's changing characters, read up on string and indices. The C++ documentation is great. As for using namespace std; versus std:: and other libraries, there's already been a lot said. Try these questions on StackOverflow to start.

提交回复
热议问题