C++字符串的循环

发布于 2025-02-12 17:08:07 字数 1100 浏览 2 评论 0原文

我正在努力创建一个从用户那里获取输入的循环。输入必须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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

几度春秋 2025-02-19 17:08:07

您的for循环缺少声明和(迭代)表达式部分:

for (declaration-or-expression; declaration-or-expression; expression)

因此,它应该看起来像这样:

for (;x != '1';) {

这通常

while (x != '1') {
  • 会引起问题,因为当用户输入<时它不会直接停止代码> 1 。
  • 您还将intchar> char'1')进行比较,因此,为了退出循环,用户必须必须输入491的ASCII值),而不是1
  • 您还将格式的输入(cin&gt; x)与未形式的输入(getline)混合。我建议您只坚持一个。

例子:

while(cout << "Enter 1 to stop\n", getline(cin, input) && input != "1") {
    bookQ.push_back(input);
}                                                

Your for loop is missing the declaration and (iteration) expression parts:

for (declaration-or-expression; declaration-or-expression; expression)

so it should have looked like this:

for (;x != '1';) {

which is generally written as

while (x != '1') {
  • That would cause problems though since it would not stop directly when the user entered 1.
  • You are also comparing an int with a char ('1'), so in order to exit the loop, the user would have had to enter 49 (the ASCII value for 1), not 1.
  • You are also mixing formatted input (cin >> x) with unformatted input (getline). I suggest that you stick to one only.

Example:

while(cout << "Enter 1 to stop\n", getline(cin, input) && input != "1") {
    bookQ.push_back(input);
}                                                
前事休说 2025-02-19 17:08:07

假设您意味着输入是一个字符串,那么您就犯了一些类型的错误。首先,您已将错误的类型用于变量X,您使用的是INT,即INTEGER类型,并且需要类型字符串。其次,在将X与“ 1”进行比较时,您使用了单引号,将变量的类型定义为char,而不是字符串。要使1个字符串,您应该使用双引号,例如“ 1”。除此之外,您还将用于(条件),这是不正确的语法。您应该在(条件)时使用。另外,当循环迭代时,X变量是输入簿名称,输入变量始终是一个空字符串,因此我建议用x替换x无处不在。工作代码在下面。

ps我不确定您是否要“ 1”在最终矢量中,所以我没有更改

#include <iostream>
#include <vector>
#include <string>

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;
    string x;
    while (x != "1") {
        cout << "Enter 1 to stop" << endl;
        cin >> x;

        bookQ.push_back(x);
    }

    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}

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 use while(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

#include <iostream>
#include <vector>
#include <string>

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;
    string x;
    while (x != "1") {
        cout << "Enter 1 to stop" << endl;
        cin >> x;

        bookQ.push_back(x);
    }

    for (int i = 0; i < bookQ.size(); i++) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}
情定在深秋 2025-02-19 17:08:07

只需检查输入是否每次输入1时,当用户输入= 1时,只需打破循环即可。

string x;
while (true) {                                 // require a loop to input string and  end when user prompts                      
    cout << "Enter 1 to stop" << endl;
    cin >> x;
    if (x == "1"){
       break;
    }
    getline(cin, x);
    bookQ.push_back(x);
}
}    

simply check if input is 1 everytime the user enters somthing, and when it does = 1, simply break loop.

string x;
while (true) {                                 // require a loop to input string and  end when user prompts                      
    cout << "Enter 1 to stop" << endl;
    cin >> x;
    if (x == "1"){
       break;
    }
    getline(cin, x);
    bookQ.push_back(x);
}
}    
生活了然无味 2025-02-19 17:08:07

首先,您的语法是错误的。您需要一个循环,或者在这种情况下,do..whore循环会更有意义。另外,在验证输入实际是什么之前,您将将用户的输入推入向量。

其次,x是一个整数,但是'1'是一个字符,其ASCII值为数字49。您的循环永远不会结束,因为!=将永远是真实的。由于您希望用户输入数字1来停止循环,因此需要删除引号:

第三,预填充bookq的点是什么?只需声明bookQ而无需任何初始数据,然后cout将整个问题作为普通字符串。这样,在用户完成输入后,向量将包含用户的输入,而无需其他。

尝试更多这样的东西:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std; 

int main()
{
    vector <string> bookQ;
    string input;

    cout << "what book is that you are reading" << endl;

    do {
        cout << "Enter a book, or 1 to stop" << endl;
        getline(cin >> ws, input);
        if (input == "1") break;
        bookQ.push_back(input);
    }
    while (true);

    for (size_t i = 0; i < bookQ.size(); ++i) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}

First, your for syntax is wrong. You want a while loop instead, or in this case a do..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 the bookQ without any initial data, and then cout 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:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std; 

int main()
{
    vector <string> bookQ;
    string input;

    cout << "what book is that you are reading" << endl;

    do {
        cout << "Enter a book, or 1 to stop" << endl;
        getline(cin >> ws, input);
        if (input == "1") break;
        bookQ.push_back(input);
    }
    while (true);

    for (size_t i = 0; i < bookQ.size(); ++i) {
        cout << bookQ[i] << ' ';
    }
    cout << endl;

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文