Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,283 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,245 people online right now. Registration is fast and FREE... Join Now!




Off By One Error

 
Reply to this topicStart new topic

Off By One Error, I have an error in my functions which are giving an off by one error i

Steffan
post 2 Apr, 2008 - 11:09 AM
Post #1


New D.I.C Head

*
Joined: 14 Jan, 2008
Posts: 28

I am writing a program to give words if the user enters text, for example:

The User enters 123
The program output:
"one hundred twenty three dollars and no cents.

Another example
The User enters 1.01
The Program outputs
1 dollar and one cents.

Here is my code that i have written so far

CODE
#include<iostream>
#include<cmath>
#include<string>
using namespace std;

int AnalyzeAmount (double amt)
{
    int divisor, amt_value;


    amt_value = (int) amt;
    divisor = 1;

    while(amt_value / divisor >= 10)
        divisor = divisor*10;

    if (amt_value == 10)
        divisor = 10;

    return divisor;
}

void  TakeDifferenceToGetMultipleOfTen(double & amt, string & words)
{
    double amt_dif, floored_amt;
    static int function_count = 0;
    static int count = 0;
    int  amt_temp;

    function_count++;

    amt_temp = (int)floor(amt);
    floored_amt = floor(amt);


    while(amt_temp % 10 != 0)
    {    
        amt_temp--;
        count++;
    }

    amt_dif =  floored_amt - amt_temp;
    amt -= amt_dif;

    if(function_count >= 2)
    {    
        amt = count;
        function_count = 0;
        count = 0;
    }


}

void GetCentsAndDollars (double amt, double & cents, string & words)
{
    double floor_amount = floor(amt);
    double floor_cents, rounding_factor;

    cents = amt - floor_amount;
    cents *= 100;

    floor_cents = floor(cents);
    rounding_factor = cents - floor_cents;

    rounding_factor *= 10;

    if(rounding_factor >= 5)    
    {
        floor_cents += 1;
        cents = floor_cents;
    }
    if (rounding_factor <= 5)
        cents = floor_cents;

    if (cents==0)
        words += " no cents ";
    else if(cents == 1)
        words+= " cent ";

    else if (cents > 1)
        words += " cents ";

    
}


void AddDollarsAndCentsToString(double & amt, string & words)
{
    double cents = 0;


    if( amt > 1)
        words+= "dollars and ";
    else
        words += "dollar and ";

    GetCentsAndDollars(amt,cents,words);
    

    
}
void AddNumericWordsLessThanTen (double amt, string & words)
{

    amt = floor(amt);

    if (amt < 1)
        words += "no dollars";
    else if (amt == 1)
        words += "one ";

    else if (amt == 2)
        words += "two ";

    else if (amt == 3)
        words += "three ";

    else if (amt == 4)
        words += "four ";

    else if (amt == 5)
        words += "five ";

    else if (amt == 6)
        words += "six ";

    else if (amt == 7)
        words += "seven ";

    else if (amt == 8)
        words += "eight ";

    else if (amt == 9)
        words += "nine ";

}


void NumbersBetweenTenAndTwenty(double amt, string & words)
{
    amt = floor(amt);

    if (amt == 11)
        words += "eleven ";

    else if (amt == 12)
        words += "twelve ";

    else if (amt == 13)
        words += "thirteen ";

    else if (amt == 14)
        words += "fourteen ";

    else if (amt == 15)
        words += "fifteen ";

    else if (amt == 16)
        words += "sixteen ";

    else if (amt == 17)
        words += "seventeen ";

    else if (amt == 18)
        words += "eighteen ";

    else if (amt == 19)
        words += "nineteen ";

}

void MultiplesOfTenBetweenOneHundred(double amt, string & words)
{
    amt = floor(amt);

    if (amt == 10)
        words += "ten ";

    else if (amt == 20)
        words += "twenty ";

    else if (amt == 30)
        words += "thirty ";

    else if (amt == 40)
        words += "fourty ";

    else if (amt == 50)
        words += "fifty ";

    else if (amt == 60)
        words += "sixty ";

    else if (amt == 70)
        words += "seventy ";

    else if (amt == 80)
        words += "eighty ";

    else if (amt == 90)
        words += "ninety ";

}

void AmountLessThanOne(double amt, string words)
{
}
void AmountIsOneDigit(double amt, string & words)
{


    AddNumericWordsLessThanTen(amt,words);

    if ( amt < 10 && amt >= 1 )
        AddDollarsAndCentsToString( amt, words);



}
void AmountIsTwoDigits(double amt, string & words)
{
    
    if (amt < 10)
        AddNumericWordsLessThanTen(amt, words);

    if ( amt >= 10 && amt < 20 )
    {
        NumbersBetweenTenAndTwenty(amt, words);
        MultiplesOfTenBetweenOneHundred(amt,words);

        AddDollarsAndCentsToString( amt, words);
    }

    if(amt >= 20)
    {
        TakeDifferenceToGetMultipleOfTen(amt, words);
        MultiplesOfTenBetweenOneHundred( amt, words);
        TakeDifferenceToGetMultipleOfTen(amt, words);
        
        if(amt != 0)
            AmountIsOneDigit(amt, words);
        else
            AddDollarsAndCentsToString(amt, words);


    


    
    }


}
void AmountIsThreeDigits(double amt, string & words)
{
    double floor_amt, new_amt;

    amt/= 100;
    AddNumericWordsLessThanTen(amt,words);
    words += "hundred ";

    floor_amt = floor(amt);

    if( amt - floor_amt != 0)
    {
        new_amt = amt - floor_amt;
        new_amt *= 100;
    
        

        AmountIsTwoDigits(new_amt, words);
    }



    


    


    
    }





void AmountIsFourDigits(double amt, string & words)
{
    double floor_amt, new_amt;

    amt/= 1000;
    AddNumericWordsLessThanTen(amt,words);
    words += "thousand ";

    floor_amt = floor(amt);
    
    if( amt - floor_amt != 0)
    {
        new_amt = amt - floor_amt;
        new_amt *= 1000;
        new_amt = ceil(new_amt);

        AmountIsThreeDigits(new_amt, words);
    }


}
void AmoutIsFiveDigits(double amt, string & words)
{
double floor_amt, new_amt;

    amt/= 10000;
    AmountIsTwoDigits(amt,words);
    

    floor_amt = floor(amt);
    
    if( amt - floor_amt != 0)
    {
        new_amt = amt - floor_amt;
        new_amt *= 10000;

        new_amt = ceil(new_amt);

        AmountIsFourDigits(new_amt, words);
    }

}
void AmoutIsSixDigits(double amt, string words)
{

}
void AmoutIsSevenDigits(double amt, string words)
{
}
void AmountIsEightDigits(double amt, string words)
{
}

void AmountIsNieDigits (double amt, string words)
{
}










void SendDataToFunctions(double amt, string & words)
{
    int check = 0;

    check = AnalyzeAmount(amt);

    if (check == 1)
        AmountIsOneDigit(amt, words);
    else if (check == 10)
        AmountIsTwoDigits(amt, words);

    else if(check == 100)
        AmountIsThreeDigits(amt, words);

    else if (check == 1000)
        AmountIsFourDigits(amt, words);

    else if (check == 10000)
        AmoutIsFiveDigits(amt, words);



}


void ConvertAmountToWords (double amt, string & words)
    {
    words.clear();
    SendDataToFunctions(amt, words);
}



void main()
{
    double amt;
    string words;

    cout << "Amount? ";
    cin >> amt;
    while (amt > 0)
    {
        ConvertAmountToWords(amt, words);
        cout << words<<"\n\n";
        cout << "Amount? ";
        cin >> amt;
    }
}



The error are in the digits functions three, four, and five, where the ceiling function is used.
User is online!Profile CardPM

Go to the top of the page

KYA
post 2 Apr, 2008 - 03:01 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


Here is what appears to be the problem:

CODE

   if( amt - floor_amt != 0)
    {
        new_amt = amt - floor_amt;
        new_amt *= 1000;
        new_amt = ceil(new_amt);

        AmountIsThreeDigits(new_amt, words);
    }


You don't have enough paraentheses... Try this:

CODE

   if( (amt - floor_amt) != 0)
    {
        new_amt = amt - floor_amt;
        new_amt *= 1000;
        new_amt = ceil(new_amt);

        AmountIsThreeDigits(new_amt, words);
    }
User is offlineProfile CardPM

Go to the top of the page

Steffan
post 2 Apr, 2008 - 05:32 PM
Post #3


New D.I.C Head

*
Joined: 14 Jan, 2008
Posts: 28

thanks that worked
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 12:54AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month