C++字符串的循环
我正在努力创建一个从用户那里获取输入的循环。输入必须push_back()
每个实例。
#include <iostream>
#include <array>
#include <cstring>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
int main()
{
vector <string> bookQ = { "what","book","is","that","you","are","reading" };
for (int i = 0; i < bookQ.size(); i++) {
cout << bookQ[i] << ' ';
}
cout << endl;
string input;
int x = 0;
for (x != '1') { // require a loop to input string and end when user prompts
cout << "Enter 1 to stop" << endl; //
cin >> x; //
getline(cin, input); //
bookQ.push_back(input); //
} //
for (int i = 0; i < bookQ.size(); i++) {
cout << bookQ[i] << ' ';
}
cout << endl;
return 0;
}
I am struggling to create a loop for getting input from user. The input must push_back()
each instance.
#include <iostream>
#include <array>
#include <cstring>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
int main()
{
vector <string> bookQ = { "what","book","is","that","you","are","reading" };
for (int i = 0; i < bookQ.size(); i++) {
cout << bookQ[i] << ' ';
}
cout << endl;
string input;
int x = 0;
for (x != '1') { // require a loop to input string and end when user prompts
cout << "Enter 1 to stop" << endl; //
cin >> x; //
getline(cin, input); //
bookQ.push_back(input); //
} //
for (int i = 0; i < bookQ.size(); i++) {
cout << bookQ[i] << ' ';
}
cout << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
for
循环缺少声明和(迭代)表达式部分:因此,它应该看起来像这样:
这通常
int
与char> char
('1'
)进行比较,因此,为了退出循环,用户必须必须输入49
(1
的ASCII值),而不是1
。cin&gt; x
)与未形式的输入(getline
)混合。我建议您只坚持一个。例子:
Your
for
loop is missing the declaration and (iteration) expression parts:so it should have looked like this:
which is generally written as
1
.int
with achar
('1'
), so in order to exit the loop, the user would have had to enter49
(the ASCII value for1
), not1
.cin >> x
) with unformatted input (getline
). I suggest that you stick to one only.Example:
假设您意味着输入是一个字符串,那么您就犯了一些类型的错误。首先,您已将错误的类型用于变量X,您使用的是INT,即INTEGER类型,并且需要类型字符串。其次,在将X与“ 1”进行比较时,您使用了单引号,将变量的类型定义为char,而不是字符串。要使1个字符串,您应该使用双引号,例如“ 1”。除此之外,您还将
用于(条件)
,这是不正确的语法。您应该在(条件)时使用。另外,当循环迭代时,X变量是输入簿名称,输入变量始终是一个空字符串,因此我建议用x替换x无处不在。工作代码在下面。
ps我不确定您是否要“ 1”在最终矢量中,所以我没有更改
Assuming you meant that input is a string, then you've made a few mistakes with types. First of all, you've used wrong type for variable x, you used int which is integer type, and the type string is required. Secondly, when comparing x with '1' you used single quotes, which define the type of variable as char, not string. To make 1 a string you should use double quotes, like so "1". Besides that, you have used
for(condition)
, which is incorrect syntax. You should usewhile(condition)
. Also, when your loop iterates, the x variable is the input book name, and input variable is always an empty string, so I would suggest replace input with x everywhere. The working code is below.P.S. I am not sure whether you want "1" to be in the final vector, so I haven't changed that
只需检查输入是否每次输入1时,当用户输入= 1时,只需打破循环即可。
simply check if input is 1 everytime the user enters somthing, and when it does = 1, simply break loop.
首先,您的语法是错误的。您需要一个
而
循环,或者在这种情况下,do..whore
循环会更有意义。另外,在验证输入实际是什么之前,您将将用户的输入推入向量。其次,
x
是一个整数,但是'1'
是一个字符,其ASCII值为数字49。您的循环永远不会结束,因为!=
将永远是真实的。由于您希望用户输入数字1来停止循环,因此需要删除引号:第三,预填充
bookq
的点是什么?只需声明bookQ
而无需任何初始数据,然后cout
将整个问题作为普通字符串。这样,在用户完成输入后,向量将包含用户的输入,而无需其他。尝试更多这样的东西:
First, your
for
syntax is wrong. You want awhile
loop instead, or in this case ado..while
loop would make more sense. Also, you are pushing the user's input into the vector before validating what the input actually is.Second,
x
is an integer, but'1'
is a character whose ASCII value is number 49. Your loop will never end, because!=
will always be true. Since you want the user to enter number 1 to stop the loop, you need to drop the quotes:Third, what is the point of pre-populating
bookQ
? Just declare thebookQ
without any initial data, and thencout
the entire question as a normal string. This way, after the user is done entering input, the vector will contain only the user's input and nothing else.Try something more like this: