Thread: float arrays

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Question float arrays

    I am a junior programmer and I am trying to make a program that will recieve a number from the user between 50 and -50 and I want it to be able to say the words back to them on the screen. Example 23.6....twenty three point six. If anyone could help me that would be greatly appreciated.Thanx

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    #include <iostream.h>
    #include <string.h>

    void main()
    {
    char number[20];

    cout << "Enter number: ";
    cin >> number;

    unsigned short int len=strlen(number);

    cout << endl;

    for(unsigned short int a=0;a<len;a++)
    {
    if(number[a]=='0')
    cout << "zero ";
    else if (number[a]=='1')
    cout << "one ";
    else if (number[a]=='2')
    cout << "two ";
    else if (number[a]=='3')
    cout << "tree ";
    else if (number[a]=='4')
    cout << "four ";
    else if (number[a]=='5')
    cout << "five ";
    else if (number[a]=='6')
    cout << "six ";
    else if (number[a]=='7')
    cout << "seven ";
    else if (number[a]=='8')
    cout << "eight ";
    else if (number[a]=='9')
    cout << "nine ";
    else if (number[a]=='.')
    cout << "point ";
    }

    cout << endl;
    }

    but notice that you can hardly make it show you things like "twenty five"... imagine how many checks you must make...
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  3. #3
    Unregistered
    Guest
    one way to do this is to set up a series of switch statements to convert numeric values to words storing each word in a string variable and a series of modulo by 10 and integer division by to to strip of the digits one by one so they can be analyzed

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Talking It wouldn't be...

    IT wouldn't be that big of a pain in the ass to do 50 case statements, simply because you know your input is limited from -50 to 50.
    "Where genius ends, madness begins."
    -Estauns

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    104
    yeah, but it will look bad
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    char array[] = {"","","twenty","thrity","fourty","fifty"};
    char array2[] = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
    
    
    int number;
    
    //get number
    //disect the number say it's 25
    //get the two
    //get the five
    printf("temperature is %s%s degrees.", array[2],array1[5]);
    And you disect the number by dividing by 10 and using modulus %.

  7. #7
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    And you disect the number by dividing by 10 and using modulus %.
    This is complicated by having to output the string representation of a float (you can't modulus a float). However if there is a limit to the amount of decimal places the you can multiply the float by powers of ten and convert to an int to make sure you get all the digits.

    Something like this would work from -10.9 to +10.9 -

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    	string numbers[11]={"zero","one","two","three","four","five","six","seven","eight",
    		"nine","ten"};
    
    	cout << "Enter a number (-10.9 to +10.9): ";
    
    	float fnum;
    	cin >> fnum;
    
    	int inum = (int)fnum;
    	//get decimal point (ensuring postive and correct rounding)
    	int dnum = (fabs(fnum)+0.0001-fabs(inum))*10;
    
    
    	//ensure -10<=num<=10
    	inum %=11;
    
    
    	if (inum<0)
    	{
    		cout << "minus ";
    		inum=-inum;
    	}
    
    	cout << numbers[inum];
    
    	if (dnum>0)
    	cout << " point " << numbers[dnum];
    
    	cout << endl;
    
    	return 0;
    }
    zen

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    This is complicated by having to output the string representation of a float (you can't modulus a float).
    Good point. You need to typecast. You could also multiply by ten to get the decimal over to the integer side than modulus by one hundred instead of fabs. You could also do this right inside the indexes after if(number < 0) number = -number;

    Anyway the main point is to use the array indexes to access the table of unique words.

    cprogramming, what a stinking slow website this is!
    Last edited by Troll_King; 10-17-2001 at 04:10 PM.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    A possible way of doing it:

    First a function which returns the correct word sequence up to 999 is needed. It should also return if there is an 'and' somewhere in there (look below):
    Code:
    enum _ghr { HasAnd, NoAnd } GetHundreds(char* buffer, const char* num)
    {
        _ghr    retval    = NoAnd;
        char*   pbuffer   = buffer;
        char*   Numbers[] =
        {
            "one", "two", "three", "four", "five", "six", "seven",
            "eight", "nine", "ten", "eleven", "twelve", "thirteen",
            "fourteen", "fifteen", "sixteen", "seventeen", "eightteen",
            "nineteen", "twenty", "thirty", "fourty", "fifty", "sixty",
            "seventy", "eighty", "ninety"
        };
        
        if(num[0] != '0')
        {
            // xyz, x != 0 (ie: 100)
            sprintf(pbuffer, "%s hundred ", Numbers[num[0] - '1']);
            pbuffer += strlen(pbuffer);
        }
        
        if(num[1] != '0' || num[2] != '0')
        {
            if(num[0] != '0')
            {
                strcpy(pbuffer, "and ");
                pbuffer += strlen(pbuffer);
                retval = HasAnd;
            }
            if(num[1] == '0')
                // x0z, z != 0 (ie: 101)
                sprintf(pbuffer, "%s ", Numbers[num[2] - '1']);
            else if(num[1] == '1')
                // x1z (ie: 111)
                sprintf(pbuffer, "%s ", Numbers[num[2] - '1' + 10]);
            else if(num[2] != '0')
                // xyz, y != 0, y != 1, z != 0 (ie: 121)
                sprintf(pbuffer, "%s-%s ", Numbers[num[1] - '2' + 19], Numbers[num[2] - '1']);
            else
                // xy0, y != 0, y != 1 (ie: 120)
                sprintf(pbuffer, "%s ", Numbers[num[1] - '2' + 19]);
        }
        
        return retval;
    }
    Now that we have this, all that is left is to add the appropriate 'milliard', 'million' or 'thousand' where it should be. Of course we should also check for the 'and', since we don't want things like 'one thousand one', but 'one thousand and one'. Also, we'll need a little routine to insert strings for us. Better can be written but this one is small and easy to read:
    Code:
    char* strins(char* buffer, const char* str)
    {
        char*   temp = new char[strlen(buffer) + 1];
        
        strcpy(temp, buffer);
        strcpy(buffer, str);
        strcat(buffer, temp);
        
        delete [] temp;
        
        return buffer;
    }
    
    // Writes the word equivalent of Number in buffer.
    // Returns buffer.
    char* UIntToWords(char* buffer, unsigned int Number)
    {
        char    num[13];
        char*   pbuffer = buffer;
        
        if(Number == 0u)
            return strcpy(buffer, "zero");
        
        sprintf(num, "%012u", Number);
        
        bool    milliards   = (num[0] != '0' || num[ 1] != '0' || num[ 2] != '0');
        bool    millions    = (num[3] != '0' || num[ 4] != '0' || num[ 5] != '0');
        bool    thousands   = (num[6] != '0' || num[ 7] != '0' || num[ 8] != '0');
        bool    hundreds    = (num[9] != '0' || num[10] != '0' || num[11] != '0');
        
        if(milliards)
        {
            GetHundreds(pbuffer, &num[0]);
            strcat(pbuffer, "milliard ");
            pbuffer += strlen(pbuffer);
        }
        
        if(millions)
        {
            if(NoAnd == GetHundreds(pbuffer, &num[3]) &&
               milliards && !thousands && !hundreds)
                strins(pbuffer, "and ");
            strcat(pbuffer, "million ");
            pbuffer += strlen(pbuffer);
        }
        
        if(thousands)
        {
            if(NoAnd == GetHundreds(pbuffer, &num[6]) &&
               (milliards || millions) && !hundreds)
                strins(pbuffer, "and ");
            strcat(pbuffer, "thousand ");
            pbuffer += strlen(pbuffer);
        }
        
        if(hundreds)
            if(NoAnd == GetHundreds(pbuffer, &num[9]) &&
               (milliards || millions || thousands))
                strins(pbuffer, "and ");
            
        return buffer;
    }
    Finally, a routine which gets singed ints:
    Code:
    char* IntToWords(char* buffer, int Number)
    {
        char*   pbuffer = buffer;
        
        if(Number < 0)
        {
            strcpy(pbuffer, "minus ");
            pbuffer += strlen(pbuffer);
            Number = -Number;
        }
        
        UIntToWords(pbuffer, (unsigned int)Number);
        
        return buffer;
    }
    Now to test it:
    Code:
    #include <cstring>
    #include <cstdio>
    
    // Insert other functions here
    
    int main()
    {
        char buffer[500];
    
        printf("%d - %s\n", -1234567, IntToWords(buffer, -1234567));
    
        return 0;
    }
    Output is:
    -1234567 - minus one million two hundred and thirty-four thousand five hundred and sixty-seven
    Most probably not the best solution but a working one.
    Last edited by lmov; 10-18-2001 at 01:41 AM.
    - lmov

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    And all of these tons of code for a novice programmer, who doesn't know C++
    Good luck, my friend

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    And all of these tons of code for a novice programmer, who doesn't know C++
    No offence, but you can't possibly call four simple straitforward functions tons of code.
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. Multidimensional String
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 07-03-2004, 12:47 AM
  5. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM