Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 05-21-2009, 04:33 PM
Raziel Raziel is offline
Count
 
Join Date: Apr 2009
Thanks: 3
Thanked 136 Times in 103 Posts
Send a message via MSN to Raziel Send a message via Skype™ to Raziel
Default Decimal to English converter (c++)

Got bored, made this in five minutes..
I seriosly need some idea's for projects.

Anyway, have at it.
http://pastebin.com/f5d1f519e
Code:
#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;
}
Reply With Quote
  #2  
Old 05-21-2009, 04:45 PM
Agent 008 Agent 008 is offline
Slightly Grander Duke
 
Join Date: Jan 2009
Thanks: 440
Thanked 525 Times in 324 Posts
Lightbulb Re: Decimal to English converter (c++)

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.
Code:
if (input >= 3) {
   input = input - 3;
   cout << "Three ";
}
or
Code:
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.

Last edited by Agent 008; 05-21-2009 at 06:25 PM.
Reply With Quote
  #3  
Old 05-21-2009, 05:52 PM
Raziel Raziel is offline
Count
 
Join Date: Apr 2009
Thanks: 3
Thanked 136 Times in 103 Posts
Send a message via MSN to Raziel Send a message via Skype™ to Raziel
Default Re: Decimal to English converter (c++)

Thanks Agent 008. I'll do some reseach
Reply With Quote
Reply

Bookmarks

Tags
converter, decimal, english

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WARNING Tv converter boxes devices to spy on us?? WARNING Orinux LOL, Internet 11 03-03-2009 10:16 PM
Help with English WeaselKit Generally Speaking 23 02-23-2009 02:35 AM
converting decimal solutions of polynomials into quadratic irrationals rabbit boy Math, Science, and Engineering 3 02-22-2009 09:59 PM
Non-English rap zuperxtreme Music! 0 02-16-2009 04:30 PM


All times are GMT. The time now is 03:49 PM.


Hot Topics
On IRC
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
Users: 22
Messages/minute: 0.2
Topic: "dangly parts"
Users: 10
Messages/minute: 0
Topic: "vaginaboob"
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.