【问题标题】:C++, cin until no more input on line using a while loopC ++,cin,直到不再使用while循环在线输入
【发布时间】:2015-04-01 01:09:55
【问题描述】:

我的 C++ 程序有问题。我重新格式化并显示用户输入控制台的单词。如果用户输入:您好,我是 bob。用户将 bob 输入控制台后按 Enter。我将重新格式化并以新格式重新打印。问题是在输入该控制台行上的所有单词之前,我不想显示更多输入的消息。我当前的循环要么在每个单词之后显示输入请求,要么根本不显示它。这取决于我是否包含提示。我需要做一个while循环处理每个单词并将其输出并在最后一个单词之后停止。什么是布尔参数?我包含我的代码以供参考。

int _tmain(int argc, _TCHAR* argv[])
{

    int b;
    string input;
    string output ;
    int check = 1;

    while (check){
        cout << "Enter in one or more words to be output in ROT13: " << endl;
        cin >> input;
        while(my issue is here){

            const char *word = input.c_str();

            for (int i = 0; i < input.length(); i++){

                b = (int)word[i];
                if (b > 96){
                    if (b >= 110){
                        b = b - 13;
                    }
                    else {
                        b = b + 13;
                    }

                    output += ((char)b);
                }

                else
                {
                    if (b >= 78){
                        b = b - 13;
                    }
                    else {
                        b = b + 13;
                    }

                    output += ((char)b);
                }







            } 
            cout << output << endl;
            output = "";
            cin >> input;

        }

            check = 0;

    }

    return 0;
}

【问题讨论】:

    标签: c++ input console cin


    【解决方案1】:

    如果没有更多行输入,cin 函数将返回 false。您可以执行以下操作以读取直到输入结束,或者如果您将 cin 重定向到从文件中读取,则可以执行 eof。

    int a;
    while(cin >> a){
        //Your loop body
    }
    

    【讨论】:

    • @NeilKirk 我从没说过 while(eof)
    • 我说“直到输入结束”或“eof”
    • 你可以做 freopen("filename", "r",m stdin);重定向输入。
    • 好吧抱歉我误会了。
    【解决方案2】:

    你可以用这一行替换整个while循环:

    std::getline(std::cin, input);  // where input is a std::string
    

    然后在这一行之后重新格式化。

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2021-03-04
      相关资源
      最近更新 更多