   Decimal to number/Number to decimal converter
  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.   
  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   ;  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.   ;  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'.RegardsAdmiral   ;  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.   ;  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;}};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 <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;}}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 use2. C++-style casting3. Removal of the unnecessary cast (float)valueAlso, 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.   ;  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?   ;Quote:Original post by VidikiHere's my new code:<snip>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   ;  Well, if the "choice" is above 3, I would like it to erase all previously written text, and run the code again.   