简体   繁体   中英

C++ Loop back beginning if the result is false

I am very new to C++ and still studying. The below scenario just came to my mind and I was trying to figure it out how to do this.

Scenario is as below:-

  • User inputs a number
  • then I store it in x
  • next is to check whether the input number is an int or float
  • if int , then pop up a message "Entered Number is not a Decimal Number" and go back to the beginning and inform the user to re-enter a number
  • if the entered number is float then I round it to the nearest int and pop up a message cout<<"Nearst Rounded Number is: "<<round(x)<<endl;

I assume this can be done with a loop, but I cannot figure it out.

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

It should work. Else you can keep one bool variable in if else part and validate them below and break the while loop until your condition satisfy.

I believe this is what you wanted:

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

Bye!

Edit: Changed the code a bit and removed a bug.

I have changed your code a bit to take input continuously. while(cin>>x) means the program is taking input constiniously until EOF. Then when you find a decimal number, it breaks.

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

By the way I will advise you to spend a bit more time to find out the solution on your own before posting here.

first of all Why is “using namespace std;” considered bad practice? second - use a loop with a boolean flag to indicate when you want the exit the loop

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

Your code needs improvements like indentations and the use of loop conditions otherwise your program will not rerun again.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM