Thread: something is wrong with the program.please help

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    41

    something is wrong with the program.please help

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    string text(int c);
    string numInWords(int num);
    int main()
    {
        int num;cout<<"enter the number : ";cin>>num;
        cout<<"In words :\n"<<numInWords(num)<<endl;
        return 0;
    }
    string numInWords(int num)
    {
        double a=num;
        int c1=0,c2=0,c3=0;
        int c4;
        if(a>=1000000000)
        {
    	a/=1000000000;
    	c1=a;a=(a-c1)*1000000000;
        }
        if(a>=1000000)
        {
    	a/=1000000;
    	c2=a;a=(a-c2)*1000000;
        }
        if(a>=1000)
        {
    	a/=1000;
    	c3=a;a=(a-c3)*1000;
        }
        c4=a;
        string s1=text(c1);string s2=text(c2);string s3=text(c3);string s4=text(c4);
        string m1="",m2="",m3="",temp="";
        if(s1!=temp)m1=" billion ";if(s2!=temp)m2=" million ";if(s3!=temp)m3=" thousand ";
        return s1+m1+s2+m2+s3+m3+s4;
    }
    string text(int c)
    {
        string h="",t="",o="";
        bool odd=false;
        if(c>=900)
    	h="nine hundred ";
        else if(c>=800)
    	h="eight hundred ";
        else if(c>=700)
    	h="seven hundred ";
        else if(c>=600)
    	h="six hundred ";
        else if(c>=500)
    	h="five hundred ";
        else if(c>=400)
    	h="four hundred ";
        else if(c>=300)
    	h="three hundred ";
        else if(c>=200)
    	h="two hundred ";
        else if(c>=100)
    	h="one hundred ";
        else{}
        double tens=c/100.0;tens=(tens-(int)(tens))*100;
        if(tens>=90)
            t="ninety ";
        else if(tens>=80)
            t="eighty ";
        else if(tens>=70)
            t="seventy ";
        else if(tens>=60)
            t="sixty ";
        else if(tens>=50)
            t="fifty ";
        else if(tens>=40)
            t="forty ";
        else if(tens>=30)
            t="thirty ";
        else if(tens>=20)
            t="twenty ";
        else if(tens==19){
            t="nineteen";odd=true;
        }
        else if(tens==18){
            t="eighteen";odd=true;
        }
        else if(tens==17){
            t="seventeen";odd=true;
        }
        else if(tens==16){
            t="sixteen";odd=true;
        }
        else if(tens==15){
            t="fifteen";odd=true;
        }
        else if(tens==14){
            t="fourteen";odd=true;
        }
        else if(tens==13){
            t="thirteen";odd=true;
        }
        else if(tens==12){
            t="twelve";odd=true;
        }
        else if(tens==11){
            t="eleven";odd=true;
        }
        else if(tens==10){
            t="ten";odd=true;
        }
        else{}
        if(!odd){
            tens/=10;tens=(tens-(int)(tens))*10;
            if(tens==9)o="nine";
            if(tens==8)o="eight";
            if(tens==7)o="seven";
            if(tens==6)o="six";
            if(tens==5)o="five";
            if(tens==4)o="four";
            if(tens==3)o="three";
            if(tens==2)o="two";
            if(tens==1)o="one";
        }
        return h+t+o;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What is wrong with it? Compiler errors/warnings?

    Does it not run correctly? If this is the case, what input are you giving it, what output do you expect, and what output are you getting?

    How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    No, there is no compiler error/warnings. if i input 777 , i get output as seven hundred seventy instead of seven hundred seventy seven.
    On line #72 even if tens is>=40, line #73 is not executed. i don't know why that it is happening.
    The if..else..if statements are not working as they should . please help...

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    41
    i know there is an easier version for this program in cprogramming.com . But i expected my program to work correctly.I just don't know where it is wrong.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    i know there is an easier version for this program in cprogramming.com . But i expected my program to work correctly.I just don't know where it is wrong.
    Kudos to you for working it out yourself! I'm not really a C++ guy, but I should be able to give you advice with your logic.

    The line numbers you reference don't seem to be part of the problem you're seeing - if you're entering 777, line 72 shouldn't play into it.

    I'd focus on line 110:

    Code:
    tens/=10;tens=(tens-(int)(tens))*10;
    It appears you have an issue with floating point inaccuracy, which you've seemed to attempt to work around (albeit unsuccessfully). Here's what I got when I printed out the value of "tens" after the line I posted above:

    Code:
    /*
    enter the number : 777
    > tens: 0.0000000000000000
    > tens: 0.0000000000000000
    > tens: 0.0000000000000000
    > tens: 6.9999999999999574
    In words :
    seven hundred seventy
    */
    6.9999999999999574 is not equal to 7, so line 113 does not get executed.

    You need to find a better way to get an integer out of your floating point calculations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please what is wrong in this program.......
    By Kira Patricio in forum C Programming
    Replies: 5
    Last Post: 02-05-2013, 06:03 AM
  2. What's wrong with this program?
    By lijr07 in forum C Programming
    Replies: 4
    Last Post: 06-24-2011, 07:08 AM
  3. what's wrong with this program?
    By fares jajeh in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 10:39 AM
  4. What's wrong with my program?
    By Infuriate in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 04:43 PM
  5. do you see anything wrong with this program?
    By seal in forum C Programming
    Replies: 3
    Last Post: 09-22-2005, 07:43 AM