PDA

View Full Version : Archived: Decimal to English converter (c++)


Raziel
05-21-2009, 04:33 PM
Got bored, made this in five minutes..
I seriosly need some idea's for projects.

Anyway, have at it;).
http://pastebin.com/f5d1f519e
#include <iostream>

using namespace std;

int main()
{
int input;

cout << "Decimal to English converter (up to 199 for now) ~ By Raziel"<<endl<<"Input:";
cin >> input;

cout <<"Your input was: " << input<<endl<<endl<<"That converts to: ";

if (input >= 200){
cout <<"That was more than 199 :(";
return 0;
}
if (input >= 100){
input = input -100;
cout <<"One hundred and ";
}
if (input >= 90){
input = input -90;
cout <<"Ninety ";
}
if (input >= 80){
input = input -80;
cout <<"Eighty ";
}
if (input >= 70){
input = input -70;
cout <<"Seventy ";
}
if (input >= 60){
input = input -60;
cout <<"Sixty ";
}
if (input >= 50){
input = input -50;
cout <<"Fifty ";
}
if (input >= 40){
input = input -40;
cout <<"Forty ";
}
if (input >= 30){
input = input -30;
cout <<"Thirty ";
}
if (input >= 20){
input = input -20;
cout <<"Twenty ";
}
if (input >= 19){
input = input -19;
cout <<"Nineteen ";
}
if (input >= 18){
input = input -18;
cout <<"Eighteen ";
}
if (input >= 17){
input = input -17;
cout <<"Seventeen ";
}
if (input >= 16){
input = input -16;
cout <<"Sixteen ";
}
if (input >= 15){
input = input -15;
cout <<"Fifteen ";
}

if (input >= 14){
input = input -14;
cout <<"Fourteen ";
}

if (input >= 13){
input = input -13;
cout <<"Thirteen ";
}
if (input >= 12){
input = input -12;
cout <<"Twelve ";
}
if (input >= 11){
input = input -11;
cout <<"Eleven ";
}
if (input >= 10){
input = input -10;
cout <<"Ten ";
}
if (input >= 9){
input = input -9;
cout <<"Nine ";
}
if (input >= 8){
input = input -8;
cout <<"Eight ";
}
if (input >= 7){
input = input -7;
cout <<"Seven ";
return 0;
}
if (input >= 6){
input = input -6;
cout <<"Six ";
}
if (input >= 5){
input = input -5;
cout <<"Five ";
}
if (input >= 4){
input = input -4;
cout <<"Four ";
}
if (input >= 3){
input = input -3;
cout <<"Three ";
}
if (input >= 2){
input = input -2;
cout <<"Two ";
}
if (input >= 1){
input = input -1;
cout <<"One ";
}
cout << endl;
system ("pause");
return 0;
}

Agent 008
05-21-2009, 04:45 PM
I suppose you did it as an exercise, and not because you actually needed the job done, so here are some comments:

1). Code repetition. Most of your code is just 4 lines repeated over and over again with a few changes. This is generally a very, very bad thing. What if you need to change the words (translate to a different language)? What if you want to extend the code? Refactor it?
You could avoid it by keeping the output strings separate, say in an array. Then, you can really shrink your code.
Ideally though, you should separate the interface and the implementation - have an abstract class that provides you the strings, that is implemented in some way. Then you can change the implementation without touching any of the other code.

2). Formatting.
Your code is difficult to read because of strange tabulation.

if (input >= 3) {
input = input - 3;
cout << "Three ";
}

or

if (input >= 3)
{
input = input - 3;
cout << "Three ";
}


is much better.

3). Separation of concerns.
Instead of lumping all the code together, it may be a good idea to separate the routines for presentation with the actual logic of the application. This can greatly improve readability and reduce the number of mistakes.

E.g. in a more "functional" style, you could reduce your main code into a composite of three functions: output( process ( input() )); - and then one could work on improving each separate bit of code without the fear of breaking something else.


Just a few things that you may want to look into.

Raziel
05-21-2009, 05:52 PM
Thanks Agent 008. I'll do some reseach ;)