简体   繁体   English

如何在 c++ 中保存带空格的字符串输入(我正在使用 if else 语句)

[英]How can I save a string input with blank space in c++ (I am using if else statement)

So I am trying to make a text multiplier, here is the code所以我正在尝试制作一个文本乘法器,这是代码

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    string x;
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin >> x;
        for (int a = 1; a <= y; ++a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}

Everything was fine until I came to know that it was not saving the text input with a blank space I searched how to store string input with blank space and then changed the code but it didn't work.一切都很好,直到我知道它没有用空格保存文本输入我搜索了如何用空格存储字符串输入然后更改了代码但它没有用。 The changed code was更改后的代码是

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    char x[100];
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin.getline(x, 100);
        for (int a = 1; a <= y; ++a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}

Please help请帮忙

  • List item项目清单

If I understand what you want to do, you need to read the integer value, clear the remaining '\n' that is left in stdin by operator>> , and then use getline() to read the text you want to multiply, eg如果我明白你想做什么,你需要读取 integer 值,清除operator>>留在stdin中的剩余'\n' ,然后使用getline()读取你想要乘以的文本,例如

#include <iostream>
#include <limits>

using namespace std;

int main()
{
    string x;
    int y;
    
    cout << "enter how many times you want to multiply the text : ";
    
    if (!(cin >> y)) {  /* validate stream-state after EVERY input */
      std::cerr << "error: invalid integer input.\n";
      return 1;
    }
    /* clear remaining '\n' from stdin (and any other characters) */
    std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    
    cout << "enter the text you want to multiply : ";
    
    if (!getline (cin, x)) {  /* validate stream state */
      std::cout << "user canceled input.\n";
      return 0;
    }
    
    for (int a = 1; a <= y; ++a)
        cout << x << endl;
    
    return 0;
}

Note: the use of isdigit(y) is superfluous.注意:使用isdigit(y)是多余的。 If you validate the input correctly, you determine whether a valid integer was entered at the time of the read simply by checking the stream-state after the read.如果您正确验证输入,则只需在读取后检查流状态即可确定在读取时是否输入了有效的 integer。 If failbit is set, the user did not enter a valid integer.如果设置了失败位,则用户没有输入有效的failbit

While fine for test code, you will want to look at Why is “using namespace std;”虽然对于测试代码来说很好,但您需要查看为什么“使用命名空间标准;” considered bad practice?被认为是不好的做法?

Example Use/Output示例使用/输出

$ ./bin/multiplytext
enter how many times you want to multiply the text : 3
enter the text you want to multiply : my dog has fleas
my dog has fleas
my dog has fleas
my dog has fleas

If I misinterpreted your goal, let me know and I'm happy to help further.如果我误解了您的目标,请告诉我,我很乐意进一步提供帮助。

As seen from this answer , you are mixing the >> operator and getline() which causes syncing issues as getline is not waiting for the input to flush.这个答案可以看出,您正在混合>>运算符和getline()这会导致同步问题,因为getline不等待输入刷新。

You can call either你可以打电话

cin.ignore();

or或者

cin.clear();
cin.sync();

just before getline() .就在getline()之前。


Patched code:补丁代码:

#include <iostream>
using namespace std;
int main()
{
    bool m, n;
    char x[100];
    int y;
    cout << "enter how many times you want to multiply the text : ";
    cin >> y;
    isdigit(y);
    if (y)
    {
        cout << "enter the text you want to multiply : ";
        cin.ignore();
        cin.getline(x, 100);
        for (int a = 1; a <= y; ++a)
            cout << x << endl;
    }
    else
    {
        cout << "you did not entered a number , try again";
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM