0
Follow
2
View

how to parse line into number and string? [closed]

dr_heqing 注册会员
2023-01-24 23:50

You can make use of std::getline and std::istringstream as shown below. In particular in the given program, std::getline is used to read line by line and std::istringstream is used to read first the integer and then the remaining sentence.

#include 
#include 
#include 
int main()
{
    std::string  sentence;
    int inputInt = 0;
    //read line by line
    while(std::cin >> inputInt && std::getline(std::cin, sentence))
    {
        std::cout<< inputInt <<"-----"<

Demo

About the Author

Question Info

Publish Time
2023-01-24 23:50
Update Time
2023-01-24 23:50