Help with converting decimal to corresponding integer.

Hello. I am new to C++ and need a little bit of help.

Question:

Write a program that prompts the user to input a decimal number. The program should output the decimal number and the corresponding integer. The number I will be entering is 4.123.

I am not sure how to convert it.

Not looking for someone to do it for me. I just need help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    double num;

    cout << "Enter a decimal number: "<< endl;
    cin >> num;

    
    return 0;

}
Have you ever done this on paper before? Or know of any techniques? (There are only really two that I know of).

P.S.
Don't ask people here to do your programs for you.

EDIT:
Also, you're assignment probably means decimal in the sense of base 10, not floating point. Converting a floating point base 10 number takes a little bit more effort than just converting an int.
Last edited on
1) I did not ask anyone to do it for me...read my post. I put the instructions so you would know what exactly I am trying to do.

2) No, this is my first time programming. Only chapter 2.
I could go into type-casting, but as this is a beginners' forum, this is the easiest way to do it:

Create a new variable with type 'int', and after the user has input the decimal number, assign that variable to the value of 'num'. It will be automatically converted into an integer (by rounding down).

Here's an example:

1
2
double DoubleNum = 4.356; //this is equal to 4.356
int IntNum = DoubleNum; //this is equal to 4 
I did not ask anyone to do it for me...read my post. I put the instructions so you would know what exactly I am trying to do.


Ah you're right. I apologize, I guess I can't read today.


Alright well, I like to use the modulus and divide method of doing this. Basically it follows this on paper.

1)Get number, n
2)Divide n by base you are going to
3)Record the remainder
4)Record the quotient, store in n
5)Go to step 2 while n > 0
6)Your answer is the remainders backwards.

For example: 10 to binary

10/2 = 5 remainder 0
5/2 = 2 remainder 1
2/2 = 1 remainder 0
1/2 = 0 remainder 1

Answer = 1010

The method is the same for numbers after a radix(decimal) point, but you dont reverse the remainders.
Isn't my method a bit easier for a simple conversion between data types?
Wait, I am a bit confused now. What is the difference between the two ways you guys did?
I have no idea what ResidentBiscuit did, but I simply converted the 'double' variable into an integer ('int').
Ok, I see what he did now.

So, I would only convert the 4 in 4.123?

My answer would be 100, right or can it be just 4?

4/2 = 2 remainder 0
2/2 = 1 remainder 0
1/2 = 0 remainder 1

Last edited on
What is a corresponding integer? Just round it, or something along the lines of what ResidentBiscut did? Perhaps display the hex value of a float?
I came up with this code but I do not know how to reverse it.

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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    int num;
    int total = 0;

    cout << "Enter a decimal number: "<< endl;
    cin >> num;

    while( num > 0)
    {
        total = num % 2;
        num = num / 2;
        cout << total;
    }
     cout << endl;

    _getch();
    return 0;

}
Last edited on
Are you lookng for an output of:
4.123 - 4


If you are, I would simple use Archimaredes' method. It's very simply, albeit the wrong way, but it produces the results you want without too much code.

If you're just starting out, just understand that the int datatype is only able to store integers and any decimals following the integer is truncated.
What I showed you is how to convert an int to binary. I also told you how to handle digits after the radix point. Did I misunderstand your question?
Topic archived. No new replies allowed.