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.
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.
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.
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.