3 Replies - 257 Views - Last Post: 05 July 2013 - 04:31 PM Rate Topic: -----

#1 Redeyery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-July 13

Question about organization and efficiency

Posted 05 July 2013 - 11:07 AM

Hello everyone! I'm new to this forum as well as c++.

I'm creating a program that takes an integer input by the user and outputs the equivalent in words (i.e. input = 201, output = two hundred one)

So far the program works up to an input of 9999. But I feel that it's becoming unorganized and inefficient. I was hoping that I could have some advice about a more efficient way to code this program (any type of advice would be appreciated). Otherwise, thank you for your time.

#include <iostream>
#include <string>

using namespace std;

string ones[10] =
{
    "zero ",       "one ",       "two ",       "three ",        "four ",
    "five ",       "six ",       "seven ",     "eight ",        "nine "
};

string teens[10] =
{
    "ten",         "eleven",     "twelve",     "thirteen",      "fourteen",
    "fifteen",     "sixteen",    "seventeen",  "eighteen",      "nineteen"
};

string tens[8] =
{
    "twenty ",     "thirty ",    "fourty ",    "fifty ",        "sixty ",
    "seventy ",    "eighty ",    "ninety "
};

string magnitude[3] =
{
    "hundred ",    "thousand "
};

int main()
{
    while ( true )
    {
        int number;
        cout << "Enter number: ";
        cin >> number;

        if ( number < 10 )
        {
            cout << ones[number];
            cout << endl;
        }
        else if ( number > 9 && number < 20 )
        {
            cout << teens[number % 10];
            cout << endl;
        }
        else if ( number > 20 && number < 100)
        {
            cout << tens[(number / 10) - 2];
            if ( (number % 10) != 0 )
            {
                cout << ones[number % 10];
            }
            cout << endl;
        }
        else if ( number > 99 && number < 1000 )
        {
            int reverseNumber[3];

            for ( int i = 0; i < 3; i++ )
            {
                reverseNumber[i] = (number % 10);
                number = (number / 10);
            }
            cout << ones[reverseNumber[2]];
            cout << magnitude[0];
            if ( reverseNumber[1] > 1 )
            {
                cout << tens[reverseNumber[1] - 2];
            }
            if ( reverseNumber[1] < 2 && reverseNumber[1] != 0 )
            {
                cout << teens[reverseNumber[0]];
            }
            else if ( reverseNumber[0] != 0 )
            {
                cout << ones[reverseNumber[0]];
            }
            cout << endl;
        }
        else if ( number > 999 && number < 10000 )
        {
            int reverseNumber[6];

            for ( int i = 0; i < 6; i++ )
            {
                reverseNumber[i] = (number % 10);
                number = (number / 10);
            }
            if ( reverseNumber[5] == 0 && reverseNumber[4] == 0 )
            {
                cout << ones[reverseNumber[3]];
                cout << magnitude[1];

                if ( reverseNumber[2] != 0 )
                {
                    cout << ones[reverseNumber[2]];
                    cout << magnitude[0];
                }
                if ( reverseNumber[1] > 1 )
                {
                    cout << tens[reverseNumber[1] - 2];
                }
                if ( reverseNumber[1] < 2 && reverseNumber[1] != 0 )
                {
                    cout << teens[reverseNumber[0]];
                }
                else if ( reverseNumber[0] != 0 )
                {
                    cout << ones[reverseNumber[0]];
                }
            }
            cout << endl;
        }
        else
        {
            cout << "Derp" << endl;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Question about organization and efficiency

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Question about organization and efficiency

Posted 05 July 2013 - 11:14 AM

Quote

So far the program works up to an input of 9999.

Not really, try 20.

Quote

I was hoping that I could have some advice about a more efficient way to code this program (any type of advice would be appreciated).

You may want to consider adding some functions. And your global strings should really be const.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Redeyery  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-July 13

Re: Question about organization and efficiency

Posted 05 July 2013 - 12:06 PM

Quote

Not really, try 20.


Hmmm.. That seems to be the only number that doesn't work between 0-9999. I'll have to figure that out.

Thanks for the advice.

Ryan
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,659
  • Joined: 19-February 09

Re: Question about organization and efficiency

Posted 05 July 2013 - 04:31 PM

If you start at the high order digits first and work down it is easier.


int num = 259;
int digits;

// calculate hundreds
digits = num / 100;

// adjust number
num = num - (digits * 100);

cout << digits << ", " << num << endl;




Also, you probably want to start think about creating your own functions, because you can have one thousand, twenty thousand, three hundred thousand etc.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1