如果结果为假,则 C++ 循环开始

问题描述

我对 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) {
    //(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;
}

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

,

我相信这就是您想要的:

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

再见!

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

,

我对您的代码进行了一些更改以连续输入。 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;
        }
    }
}

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

,

首先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;
            }
        } 
        
    }
 }
,

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

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