-2

I'm a c++ beginner . and im trying to build a program that will check if my float variable is float . then continue the program . but if not you will have to try again . thats what ive done for the moment .

    while (true)
{
    cout << "Okay, pick a  number: " << flush;
    cin >> num1;

    

}

idk how to build an if statement with my needings and need help from you guys.


im trying to make a calculator , by far my code is

    float num1;
char continueQuestion;
float num2;
char op;

cout << "______________________________________________________" << endl;
cout << "                                                      " << endl;
cout << "          Welcome to the amazing calculator!          " << endl;
cout << "______________________________________________________" << endl;


while (true)
{
    cout << "Would you like to continue? <y/n>: " << flush;
    cin >> continueQuestion;

    if (continueQuestion == 'y') {
        break;
    }
    else if (continueQuestion == 'n')
    {
        exit(0);
    }
    else
    {
        cout << "That is not an answer!" << endl;
    }
    
}


while (true)
{
    cout << "Okay, pick a first number: " << flush;
    cin >> num1;

    

}

and if you dont got it im trying to reach for an if statement that will check if my var num1 is not a float so print something . and i cant do that so i request help from you.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
virtual
  • 11
  • 1
  • 2
    Before posting their first question on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question; which gets downvoted, closed, and then deleted. – Sam Varshavchik Nov 21 '20 at 15:45
  • A `float` is always a `float`. Do you want to validate/parse input instead? For that, use these three words plus your programming language as search terms. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 21 '20 at 15:45
  • do you consider an input `3` as invalid, because it has no fractional part? Or do you only want to catch wrong input that is not a number, like `ABC` ? – 463035818_is_not_an_ai Nov 21 '20 at 15:51
  • please also include an example input and expected output. – 463035818_is_not_an_ai Nov 21 '20 at 15:52

2 Answers2

0

You can check with that way;

(int)x = num1 

int(x) == x // if num1 is not float  returns true

---------

(int)x = num1

int(x) == // if number is float x returns false
Yusuf Şengün
  • 284
  • 3
  • 13
0

This code uses a helper function from the following answer.

#include <string>
#include <sstream>
#include <iostream>

bool isFloat(std::string myString) {
    std::istringstream iss(myString);
    float f;
    iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
    // Check the entire string was consumed and if either failbit or badbit is set
    return iss.eof() && !iss.fail(); 
}

int main() {
    float num1;

    while (true) {
        std::string input;

        std::cout << "Okay, pick a number: " << std::flush;
        std::cin >> input;

        if (isFloat(input)) {
            num1 = std::stof(input);
            break;
        }
    }
}
XDarkDev
  • 46
  • 1
  • 8