Decimal to number/Number to decimal converter

Started by
14 comments, last by Vidiki 17 years, 4 months ago
Quote:Original post by Vidiki
Well, 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.
Advertisement
Code update.

Quote:
#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;
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." << 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;


}


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.
Quote:Original post by Vidiki
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.


Have you tried the "+ 0.5" solution posted earlier in the thread?
Use [ source ] [ /source ] tags to get pretty code boxes instead of ugly quote boxes [smile]
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. ;)
And I just thought of one! XD

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

This topic is closed to new replies.

Advertisement