#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
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 <iostream>
#include <string>
#include <sstream>
int main()
{
std::string sentence;
int inputInt = 0;
//read line by line
while(std::cin >> inputInt && std::getline(std::cin, sentence))
{
std::cout<< inputInt <<"-----"<<sentence<<std::endl;
//do the check here
}
}
发布评论
评论(1)
您可以使用
std::getline
和std::istringstream
,如下所示。特别是在给定的程序中,std::getline
用于逐行读取,std::istringstream
用于首先读取整数,然后读取剩余的句子。演示
You can make use of
std::getline
andstd::istringstream
as shown below. In particular in the given program,std::getline
is used to read line by line andstd::istringstream
is used to read first the integer and then the remaining sentence.Demo