【问题标题】:C++ Loop back beginning if the result is false如果结果为假,C++ 循环返回开始
【发布时间】:2021-06-05 02:57:28
【问题描述】:

我对 C++ 很陌生,还在学习。下面的场景突然出现在我的脑海中,我试图弄清楚如何做到这一点。

场景如下:-

  • 用户输入数字
  • 然后我将它存储在x
  • 接下来是检查输入的数字是int还是float
  • 如果int,则弹出消息“输入的数字不是十进制数”并回到开头并通知用户重新输入一个数字
  • 如果输入的数字是float,那么我将它四舍五入到最接近的int并弹出一条消息cout<<"Nearst Rounded Number is : "<<round(x)<<endl;

我认为这可以通过循环来完成,但我无法弄清楚。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float x,y;
    cout<<"Enter Number Here : ";
    cin>>x;
    {
        if ( (x- int(x) == 0))
            cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
        else
            cout<<endl;
        cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
    } 
}

【问题讨论】:

  • 循环是一个好的开始。我的建议是你从一个无限循环开始(例如while (true) 或类似的),然后在输入正确的情况下找出一种打破循环的方法。
  • “十进制数”是什么意思?

标签: c++ visual-c++ c++14 c++17


【解决方案1】:
while(true) {
    //(your existing code goes here)

    cout << "Do you want to run the program again either types yes or no" << endl;

    cin >> answer;

    if (answer != 'yes' && answer != 'Yes')
        break;
}

它应该工作。否则,您可以在 if else 部分中保留一个 bool 变量并在下面验证它们并中断 while 循环,直到满足您的条件。

【讨论】:

  • 如何解决OP识别输入是否为“十进制数”的问题?
【解决方案2】:

我相信这就是你想要的:

int main() {
    string input_str;
    float input_float = 0;
    while (true) {
        cout << "Enter number here: ";
        cin >> input_str;
        input_float = stof(input_str); //stof converts string to float
        if (input_float != 0) {
            if (input_float == int(input_float)) cout << "Entered number is not a decimal, please try again." << endl;
            else break;
        }
        //TODO: Catch any exceptions thrown by stof (Ex: User inputs a letter)
    }
    cout << "Nearest Rounded number is: " << round(input_float)<<endl;
    return 0;
}

再见!

编辑:稍微更改了代码并删除了一个错误。

【讨论】:

    【解决方案3】:

    我已经稍微更改了您的代码以连续接收输入。 while(cin>>x) 表示程序一直在接收输入,直到 EOF。然后,当你找到一个十进制数时,它就会中断。

    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
        float x,y;
        cout<<"Enter Number Here : ";
        while(cin>>x)
        {
            if ( (x- int(x) == 0))
                cout<<"Entered Number is not a Decimal Number"<<endl<<"Enter Number Here : ";
            else
            {
                cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
                break;
            }
        }
    }
    

    顺便说一句,我建议你在发帖之前多花点时间自己找出解决方案。

    【讨论】:

      【解决方案4】:

      首先Why is “using namespace std;” considered bad practice? 第二 - 使用带有布尔标志的循环来指示何时退出循环

      #include <iostream>
      #include <cmath>
      
      int main()
      {
          float x,y;
          bool flag = true;
          while(flag)
          {
              std::cout<<"Enter Number Here : ";
              std::cin>>x;
              {
                  if ( (x- int(x) == 0))
                     std::cout<<"Entered Number is not a Decimal Number"<<std::endl;
                  else{
                      std::cout<<std::endl;
                      std::cout<<"Nearst Rounded Number is : "<<round(x)<<std::endl;
                      flag = false;
                  }
              } 
              
          }
       }
      

      【讨论】:

        【解决方案5】:

        您的代码需要改进,例如缩进和循环条件的使用,否则您的程序将无法再次运行。

         #include <iostream>
         #include <cmath>
         using namespace std;
         int main()
            {
                float x,y;
                bool correctinputFlag=false;
                do()
                {
                    cout<<"Enter Number Here : ";
                    cin>>x;
                    if ( (x- int(x) == 0))
                    {
                        cout<<"Entered Number is not a Decimal Number"<<endl<<endl;
                    }
                    else
                    { 
                        cout<<endl;
                        cout<<"Nearst Rounded Number is : "<<round(x)<<endl;
                        correctinputFlag=true; 
                    }
                }while(correctinputFlag==false);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-18
          • 2017-07-23
          • 2019-01-07
          • 2017-03-10
          • 1970-01-01
          • 1970-01-01
          • 2014-12-15
          • 1970-01-01
          相关资源
          最近更新 更多