簡體   English   中英

C++ 如果結果為假,則循環返回開始

[英]C++ Loop back beginning if the result is false

我對 C++ 很陌生,還在學習。 下面的場景突然出現在我的腦海中,我試圖弄清楚如何做到這一點。

場景如下:-

  • 用戶輸入一個數字
  • 然后我將它存儲在x
  • 接下來是檢查輸入數字是int還是float
  • 如果是int ,則彈出消息“輸入的數字不是十進制數”,並 go 回到開頭並通知用戶重新輸入數字
  • 如果輸入的數字是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;
}

它應該工作。 否則,您可以在if else部分中保留一個bool變量並在下面驗證它們並中斷 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;
        }
    }
}

順便說一句,我建議您在此處發布之前花更多時間自行找出解決方案。

首先為什么是“使用命名空間標准;” 被認為是不好的做法? 第二 - 使用帶有 boolean 標志的循環來指示您何時想要退出循環

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM