1

Currently, I'm working on a project in C++. I am calculating quotients of Log and I know the rest of the quotients contain decimals. However, when it is printed in the console, it comes up as a whole number. How can I display an accurate number of a further quotient instead of the whole number?

Log Console

 #include <iostream>
 #include <math.h> 

 using namespace std;

 int BinaryLog(int);
 int BinaryLog2(int);

 int main() {





int number = 0;
    int answer = 0;
    

    

    //prompting user for an integer

    cout << "Please enter a positive integer ";

    cin >> number;

    

    answer = BinaryLog(number);
    answer = BinaryLog(number);
    
    // calling function

    cout << "The binary logarithim of " << number
        << " is " << answer << endl;


    ****cout << "Other binary logs of this number " << number
        << " are " << answer << endl;
    // closing scope
    cout << number / 2 << endl;
    cout << number / 2 / 2 << endl;
    cout <<  number / 2 / 2 / 2 << endl;
    cout << number / 2 / 2 / 2 / 2 << endl;
    return 0;****

  **^^^^^^^^^ Calculation for further quotients here ^^^^^^**
 }

 int BinaryLog(int num) {
     int count = 0;

     double quotient = num;
while (quotient >= 2)
{
    count++;
    quotient = quotient / 2;



    }

return count;
}



 int BinaryLog2(int num) {
 int count = 0;

 double quotient = num;
 while (quotient >= 6)
{
    count++;
    quotient = quotient / 6;



}

return count;
 }
monotree
  • 11
  • 1
  • duplicate... lots of similar questions like https://stackoverflow.com/questions/25925290/c-round-a-double-up-to-2-decimal-places – Pedro Ferreira Dec 12 '20 at 14:56

1 Answers1

0

Replace

cout << number / 2 << endl;

with

cout << number / 2.0 << endl;

And the same for all the others.

In C++ if the left and right of / are integers then what you get it integer division and the result of that is always another integer.

john
  • 85,011
  • 4
  • 57
  • 81