mardi 2 février 2021

C++: Why is my if-else statement not working?

so I have an assignment and I'm a newbie to programming.

My prof wants to have an output, that when I enter a number, the program will get the square root of that number, then if the answer is in whole number, it will + 1, and if its in a decimal number, it will -1. But the problem is, the if in whole number is working to add 1, but in decimal, its still adding 1. (Sorry for bad english)

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

int main()
{
    double num, result;
    cout << "Enter a number: \n";
    cin >> num;
    result = sqrt(num);
    if (num == 0) {
        cout << "\nThe number is in decimal number: " << --result;
    }
    else {
        cout << "\nThe number is in whole number: " << ++result;
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire