# Decimal to number/Number to decimal converter

## Recommended Posts

Vidiki    122
Hello, I was wondering if this code could be perfected in any way:
#include <iostream>
using namespace std;

int main()
{

int choice;
int value;
float value2;

cout << "Which way do you want to convert?" << endl;
cout << "1. Decimal to Number" << endl;
cout << "2. Number to Decimal" << endl;
cin>>choice;

if (choice == 1)
{
cout << "Input the decimal you wish to convert > ";
cin>>value2;
cout << endl;
cout << "The decimal " << value2 << " was converted to " << (int)value2;
cin.ignore();
cin.get();
}

else if (choice == 2)
{
cout << "Input the number you wish to convert > ";
cin>>value;
cout << endl;
cout << "The number " << value << " was converted to " << (float)value << ".0";
cin.ignore();
cin.get();
}

}
And secondly, how would I make it so when I type in a decimal, let's say 2.6, convert to 3 instead of 2 in decimal to number conversion? Thanks.

##### Share on other sites
Illco    928
You could check if the input actually is 1 or 2 and repeat the question if it is not (be sure to allow a way of exiting the program; for example by choosing 0). You could also check out stream formatting operators to format the number correctly (instead of adding .0 in the output yourself). See iomanip online.

Regarding the conversion: casting a floating-point number to an integer will result in having the largest integer that is smaller than or equal to the floating point number. A way to fix it is as follows:
int integer = (int)(decimal + 0.5)

Check: if the number is exactly 3.0, casting 3.5 will yield the integer 3. If the number is 3.5, casting 4.0 will yield the integer 4.

Greetz,

Illco

##### Share on other sites
jyk    2094
I'll just point out a couple of things:

1. Instead of dumping the entirety of namespace std into the global namespace, just 'use' what you need, where you need it. In this case, you would remove the line 'using namespace std;' and add the following lines at the beginning of main:
using std::cout;using std::cin;using std::endl;
2. In C++ it is not required that local variables be declared at the top of the function, so you can (and should) declare them near their first use. For example, your 'value' and 'value2' variables should be declared within the code blocks associated with the two conditionals (in which case they can also share the same name).

3. You're using C-style casting, e.g. (int)value. Since you're learning C++, you should also learn about C++-style casting. In this case, you would use static_cast.

##### Share on other sites
A point on terminology.
Decimals are a subset of numbers. While your descriptions aren't strictly false, they're a little misleading. Perhaps you should exchange 'number' for 'integer' or 'whole number'.

Regards

##### Share on other sites
Zahlman    1682
Also, floating-point numbers are not decimals. This *will* bite you eventually. You need to understand that in general, decimal values can't be represented exactly with a float (note *in general* - a few can). This is for the same reason, effectively, that you can't express "one third" as a decimal (in a finite number of digits, anyway; and a float certainly has a finite number of bits to work with).

Other notes:

- Converting from int to float doesn't do anything that the user can see (notice how you still have to append the .0 manually). So don't bother; just output the .0.

- Try to factor common stuff out of if-conditions etc. Avoid repetition.

- Don't pause your programs artificially at the end. Learn to run them properly, instead.

##### Share on other sites
Vidiki    122
Here's an update on my code, I can't remember if I did much, because this was a few days ago. But anyway, bump.

#include <iostream>using std::cout;using std::cin;using std::endl;int main() {int choice;int value;float value2;mylabel:cout << "Which way do you want to convert?" << endl;cout << "1. Decimal to Whole Number" << endl;cout << "2. Whole Number to Decimal" << endl;cout << "3. Quit" << endl;cin>>choice;if (choice == 1){cout << "Input the decimal you wish to convert > ";cin>>value2;cout << endl;cout << "The decimal " << value2 << " was converted to " << (int)value2;cin.ignore();cin.get();}else if (choice == 2){cout << "Input the whole number you wish to convert > ";cin>>value;cout << endl;cout << "The number " << value << " was converted to " << (float)value << ".0";cin.ignore();cin.get();}else if (choice == 3){	return 0;}else{	cout << "That's not an option." << endl;	goto mylabel;}}

##### Share on other sites
jyk    2094
Quote:
 Original post by VidikiHere's an update on my code, I can't remember if I did much, because this was a few days ago. But anyway, bump.#include using std::cout;using std::cin;using std::endl;int main() {int choice;int value;float value2;mylabel:cout << "Which way do you want to convert?" << endl;cout << "1. Decimal to Whole Number" << endl;cout << "2. Whole Number to Decimal" << endl;cout << "3. Quit" << endl;cin>>choice;if (choice == 1){cout << "Input the decimal you wish to convert > ";cin>>value2;cout << endl;cout << "The decimal " << value2 << " was converted to " << (int)value2;cin.ignore();cin.get();}else if (choice == 2){cout << "Input the whole number you wish to convert > ";cin>>value;cout << endl;cout << "The number " << value << " was converted to " << (float)value << ".0";cin.ignore();cin.get();}else if (choice == 3){ return 0;}else{ cout << "That's not an option." << endl; goto mylabel;}}
I gather you're still open to input, so here are a couple of things that have been mentioned but don't appear to have been incorporated into your code:

1. Local variable declaration near first use
2. C++-style casting
3. Removal of the unnecessary cast (float)value

Also, without getting into a big goto discussion, it's probably safe to say that the use of that keyword is unnecessary here - just use a while loop with an appropriate conditional.

##### Share on other sites
Vidiki    122
Here's my new code:

#include <iostream>using std::cout;using std::cin;using std::endl;int main() {int choice;while (choice < 3){cout << "Which way do you want to convert?" << endl;cout << "1. Decimal to Whole Number" << endl;cout << "2. Whole Number to Decimal" << endl;cout << "3. Quit" << endl;cin>>choice;if (choice == 1){cout << "Input the decimal you wish to convert > ";float value2;cin>>value2;cout << endl;cout << "The decimal " << value2 << " was converted to " << static_cast<int>(value2) << endl;cout << "Thank you for using the decimal to whole number converter.";cin.ignore();cin.get();}else if (choice == 2){cout << "Input the whole number you wish to convert > ";int value;cin>>value;cout << endl;cout << "The number " << value << " was converted to " << static_cast<float> (value) << ".0." << endl;cout << "Thank you for using the whole number to decimal converter.";cin.ignore();cin.get();}else if (choice == 3){	return 0;}else{	cout << "That's not an option." << endl;}};}

But I have a question and a problem.
First of all, I was just curious if there was a code that could clear the screen of text.

Now in my while loop, "choice" hasn't been initialized yet, so I can't place it there.
How could I fix this?

##### Share on other sites
jouley    805
Quote:
 Original post by VidikiHere's my new code:Now in my while loop, "choice" hasn't been initialized yet, so I can't place it there.How could I fix this?

Not meaning to be overly simple about it, but... why not initialize it to something that will let the loop get run at least once before it exits? Say...
int choice = 0;
Also, what do you want to happen if the input is 4? Or 5? What will happen?
And to make sure I'm definitely not making this overly simple, this is a good candidate for a do-while loop. You'll still want to declare (and initialize) the variable (choice) near its first use, though.
As far as logic inside of the loop, a switch statement could clean it up a bit and make expanding your choices easier. If google doesn't help you with the referenced terms (do-while and switch), post back and I'll explain them up nice and purty.

Other than those small nits, lookin' nice!

-jouley

##### Share on other sites
Vidiki    122
Well, if the "choice" is above 3, I would like it to erase all previously written text, and run the code again.

##### Share on other sites
jouley    805
Quote:
 Original post by VidikiWell, if the "choice" is above 3, I would like it to erase all previously written text, and run the code again.

Well, here's a brief discussion on that topic. (Short answer -- it varies from system to system, but if you're sticking with just one platform then there's probably a way to do it.)

My original point was regarding your else statements. Think about what would happen if you enter '3'. Is the else if (choice == 3) really necessary? In general, it is preferable for a function (even main) to have only one exit point. That is, don't return if you can just fall out of a loop and return at the end of the function.

And if, as I think is the case, you only want the program to exit when '3' is entered, I'd modify your while conditional -
int choice = 0;while (choice != 3){...}
This will allow inputs like '4' and '5' to be handled like every other "not allowed" input -- go back and start again.

##### Share on other sites
Vidiki    122
Code update.

Quote:
 #include #include using std::cout;using std::cin;using std::endl;int main() {int choice = 0;while (choice != 3){cout << "Which way do you want to convert?" << endl;cout << "1. Decimal to Whole Number" << endl;cout << "2. Whole Number to Decimal" << endl;cout << "3. Quit" << endl;cin>>choice;if (choice == 1){cout << "Input the decimal you wish to convert > ";float value2;cin>>value2;cout << endl;cout << "The decimal " << value2 << " was converted to " << static_cast(value2) << "." << endl;cout << "Thank you for using the decimal to whole number converter." << endl;cout << "Press the return key to continue.";cin.ignore();cin.get();system("command /c cls");}else if (choice == 2){cout << "Input the whole number you wish to convert > ";int value;cin>>value;cout << endl;cout << "The number " << value << " was converted to " << static_cast (value) << ".0." << endl;cout << "Thank you for using the whole number to decimal converter." << endl;cout << "Press the return key to continue.";cin.ignore();cin.get();system("command /c cls");}else if (choice > 3){ cout << "That's not an option." << endl; cout << "Press the return key to continue."; cin.ignore(); cin.get(); system("command /c cls");}}return 0;}

I have another question that may improve my code; Is there a way to have the program wait for a certain amount of time before continuing? I'd like, "Press the return key to continue." to appear three seconds after the "Thank you for using" sign.

Oh! I forgot, I still have the problem with the decimal to whole number converter. I want to have it so when I type 2.5, it comes out as 3, not 2. Or when I type 76.77 it comes out 77, not 76.

##### Share on other sites
Lajnold    226
Quote:
 Original post by VidikiOh! I forgot, I still have the problem with the decimal to whole number converter. I want to have it so when I type 2.5, it comes out as 3, not 2. Or when I type 76.77 it comes out 77, not 76.

Have you tried the "+ 0.5" solution posted earlier in the thread?

##### Share on other sites
rip-off    10976
Use [ source ] [ /source ] tags to get pretty code boxes instead of ugly quote boxes [smile]

##### Share on other sites
Vidiki    122
Yes! I totally forgot about the + 0.5 idea. XD
Near-Final source:

#include <iostream>#include <cstdlib>using std::cout;using std::cin;using std::endl;int main() {int choice = 0;while (choice != 3){cout << "Which way do you want to convert?" << endl;cout << "1. Decimal to Whole Number" << endl;cout << "2. Whole Number to Decimal" << endl;cout << "3. Quit" << endl;cin>>choice;if (choice == 1){cout << "Input the decimal you wish to convert > ";float value2;cin>>value2;float value22;value22 = value2 + 0.5;cout << endl;cout << "The decimal " << value2 << " was converted to " << static_cast<int>(value22) << "." << endl;cout << "Thank you for using the decimal to whole number converter." << endl;cout << "Press the return key to continue.";cin.ignore();cin.get();system("command /c cls");}else if (choice == 2){cout << "Input the whole number you wish to convert > ";int value;cin>>value;cout << endl;cout << "The number " << value << " was converted to " << static_cast<float> (value) << ".0." << endl;cout << "Thank you for using the whole number to decimal converter." << endl;cout << "Press the return key to continue.";cin.ignore();cin.get();system("command /c cls");}else if (choice > 3){	cout << "That's not an option." << endl;    cout << "Press the return key to continue.";	cin.ignore();	cin.get();    system("command /c cls");}}return 0;}

Thank you all for every little bit of your help! ^_^
Of course this wasn't entirely about the program, but was what I learned through making the program, and I thank you all for the knowledge you supplyed me with. :)

But i'm not entirely finished yet; If you know of anything else I could do to improve this little app, please let me know. I would appreciate it very much. ;)

##### Share on other sites
Vidiki    122
And I just thought of one! XD

How would I go about making a decimal to fraction converter?