繁体   English   中英

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

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

所以我正在尝试制作一个文本乘法器,这是代码

#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;
}

一切都很好,直到我知道它没有用空格保存文本输入我搜索了如何用空格存储字符串输入然后更改了代码但它没有用。 更改后的代码是

#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;
}

请帮忙

  • 项目清单

如果我明白你想做什么,你需要读取 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;
}

注意:使用isdigit(y)是多余的。 如果您正确验证输入,则只需在读取后检查流状态即可确定在读取时是否输入了有效的 integer。 如果设置了失败位,则用户没有输入有效的failbit

虽然对于测试代码来说很好,但您需要查看为什么“使用命名空间标准;” 被认为是不好的做法?

示例使用/输出

$ ./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

如果我误解了您的目标,请告诉我,我很乐意进一步提供帮助。

这个答案可以看出,您正在混合>>运算符和getline()这会导致同步问题,因为getline不等待输入刷新。

你可以打电话

cin.ignore();

或者

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

就在getline()之前。


补丁代码:

#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