我是C++的新手,我一直在编写这个简短的程序。我似乎找不到它出了什么问题。它可以很好地输出first_name字符串,但是当我尝试输出我调用的向量的内容时,什么也没有发生。如果能帮上忙我会很感激的。毫无疑问,有一个简单的解释,比如我遗漏了一些语法。谢谢!
int main()
{
cout<< "Please enter the first name of the person you are writing to\n";
string first_name = "??";
cin >> first_name;
vector<string>w;
cout << "Enter your message to "<<first_name<<"?\n";
for(string word; cin>>word;)
w.push_back(word);
for(int i=0; i<w.size();++i)
cout<<w[i]<<'\n';
}发布于 2020-06-08 00:05:58
那是因为你没有阻止你的循环
int main()
{
cout<< "Please enter the first name of the person you are writing to\n";
string first_name = "??";
cin >> first_name;
vector<string>w;
cout << "Enter your message to "<<first_name<<"?\n";
for(string word; cin>>word && word != "q";)
w.push_back(word);
for(int i=0; i<w.size();++i)
cout<<w[i]<<'\n';
}这对我很管用。它将只是一个无限循环,但如果你传递给它q,它就会停止
https://stackoverflow.com/questions/62247922
复制相似问题