Dividing numbers to Decimal

How do I make this code spit out decimals? I can't figure out what I have to change exactly, please help.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

float chooseNumber();
float divideNumbers(int, int);

const int MIN_VALUE = 1;
const int MAX_VALUE = 25;
unsigned seed = time(0);



int main()
{
srand(seed);
int firstNumber = chooseNumber();
int secondNumber = chooseNumber();
float total = divideNumbers(firstNumber, secondNumber);
cout << "The first number is " << firstNumber << endl;
cout << "The second number is " << secondNumber << endl;
cout << "The total is " << total;
return 0;
}

float chooseNumber()
{
float number;

number = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
return number;
}

float divideNumbers (int number1, int number2)
{
float number = number1 /number2;
return number;
}
Integer division effectively truncates the result.
You must convert one of the operands to a float.

1
2
3
4
float divideNumbers(int number1, int number2)
{
    return static_cast<float>(number1)/number2;
}
Hello Yeezy6Sosa,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Unless there is a good reason for using "float" "double" is the preferred floating point type.

Going over your code I made a few changes, see what you think:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>  // <--- Added.
#include <cstdlib>
#include <ctime>
using namespace std;

double chooseNumber();
double divideNumbers(int, int);

const int MIN_VALUE = 1;
const int MAX_VALUE = 25;
//unsigned seed = time(0);  // <--- Better to put this in "main", but not needed.

int main()
{
    //srand(seed);
    srand(static_cast<unsigned int>(time(nullptr)));  // <--- Using C++11 standards.
    std::cout << std::fixed << std::setprecision(2);  // <--- Added.

    int firstNumber = chooseNumber();
    int secondNumber = chooseNumber();
    double total = divideNumbers(firstNumber, secondNumber);

    cout
        << "\n The first number is " << firstNumber << '\n'
        << " The second number is " << secondNumber << '\n'
        << " The total is " << total;

    return 0;  // <--- Not required, but makes a good break point for testing.
}

double chooseNumber()
{
    double number;

    number = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

    return number;
}

double divideNumbers(int number1, int number2)
{
    double number = static_cast<double>(number1) / number2;  // <--- As noted by Ganado.

    return number;
}


In your output you do not need a "cout" for every line. That is what the insertation operator, (<<), is for.

In the "chooseNumber" function you are returning a "double" or a "float" and trying to stuff that into an "int". You will loose the decimal portion and only keep the whole number.

In the function "rand" only returns an "int" returning this number as a "double" only gives you (??.0), so you are not really returning a valid "double" that has a decimal value.

Last note: a few blank lines makes the code easier to read.

Andy
Topic archived. No new replies allowed.