help..convert string to double

This is a discussion on help..convert string to double within the C Programming forums, part of the General Programming Boards category; i want to mix my two codes, here's part 1: Code: #include<stdio.h> #include<string.h> void print(char str[]); main() { char num[16],copy[16]; ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    help..convert string to double

    i want to mix my two codes, here's part 1:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void print(char str[]);
    
    main()
    {
        char num[16],copy[16];
        int i;
    
        puts("\n AMOUNT: $ ");
        gets(num);
    
        strcpy(copy,num);
    
        while(strlen(copy)<15){
            
            for(i=strlen(copy)+1;i>0;i--)
            copy[i]=copy[i-1];
    
            copy[0]=' ';
        }
    
        print(copy);
    
        printf("\nCheck Protection:\n\n");
    
        while(strlen(num)<15){
            for(i=strlen(num)+1;i>0;i--)
            num[i]=num[i-1];
    
            num[0]='*';
        }
    
        print(num);
        return 0;
    }
    
    void print(char str[])
    {
        int copy;
    
        for(copy=0;copy<strlen(str);copy++)
        {
            printf("%c  ",str[copy]);
        }
    
        printf("\n-  -  -  -  -  -  -  -  -  -  -  -  -  -  -");
        printf("\n1  2  3  4  5  6  7  8  9 10 11 12 13 14 15\n\n");
    }
    part 2:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    main()
    {
        char *nineteen[30]={" ","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN"
        ,"EIGHT","NINE","TEN","ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN"
        ,"SIXTEEN","SEVENTEEN","EIGHTEEN","NINETEEN"};
    
        char *tenths[10]={" "," ","TWENTY","THIRTY","FOURTY","FIFTY","SIXTY",
        "SEVENTY","EIGHTY","NINETY"};
    
        char hundred[]="HUNDRED";
        char thousand[]="THOUSAND";
        char million[]="MILLION";
    
        int ones,ten,hun,thou,num,ten_thou,hun_thou,mil;
        double dec;
    
    
        printf("\nInput ? $");
        scanf("%lf",&dec);
    
        while(dec>9999999.99||dec<0){
            printf("\nINVALID INPUT!!");
            printf("\nInput ? $");
            scanf("%lf",&dec);
        }
    
        num=(int)dec;
        dec=(double)dec-num;
    
        printf("\nIn Words: \n\n ");
    
        if(num>=1&&num<=19){
        printf(" %s",nineteen[num]);
        }
        else if(num==0){
        printf(" Zero");
        }
        else{
            ones=num%10;
            num-=ones;
            ten=num%100;
            num-=ten;
            ten/=10;
            hun=num%1000;
            num-=hun;
            hun/=100;
            thou=num%10000;
            num-=thou;
            thou/=1000;
            ten_thou=num%100000;
            num-=ten_thou;
            ten_thou/=10000;
            hun_thou=num%1000000;
            num-=hun_thou;
            hun_thou/=100000;
            mil=num%10000000;
            num-=mil;
            mil/=1000000;
    
    
    
            if(mil>0)
            printf(" %s %s",nineteen[mil],million);
    
            if(hun_thou>0)
            printf(" %s %s",nineteen[hun_thou],hundred);
    
            if(ten_thou>0)
            printf(" %s",tenths[ten_thou]);
    
            if(thou>0)
            printf(" %s",nineteen[thou]);
    
            if(hun_thou>0||ten_thou>0||thou>0)
            printf(" %s",thousand);
    
            if(hun>0)
            printf(" %s %s",nineteen[hun],hundred);
    
            if(ten>0)
            printf(" %s",tenths[ten]);
    
            if(ones>0)
            printf(" %s",nineteen[ones]);
        }
    
        if(dec!=0)
        printf(" and %.0f/100",dec*100);
    
        printf("\n\n");
        return 0;
    }
    the first code gets num -which is a sting and the second scanf dec -double. can i convert string to double ?
    can any body help me?
    and give some examples?

    sorry for my english.. =)

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yes. Use the "strtod" function.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    for (copy=0;copy<strlen(str);copy++)
    This evaluates strlen(str) every time, which is really wasteful.

    Read these:
    > SourceForge.net: Gets - cpwiki
    > SourceForge.net: Implicit main - cpwiki

    In the first file, what's with all the copying? You manage to make two copies with four loops (why??), and I'm not sure what you're doing with all the -1 offset either. Just get the string (and don't use gets), and then print it.

    In the second file, what if I enter 10 million? Your program can't handle it. If you ever run into this type of issue, don't simply copy/paste the code, instead, use a loop.

    Or just use strtod() like TheBigH said

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    thanks a lot sir memcpy and sir ThebigH .
    im just a newbie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  2. convert string to double
    By xsouldeath in forum C Programming
    Replies: 10
    Last Post: 10-24-2007, 10:18 AM
  3. How to convert from double to string?
    By beon in forum C Programming
    Replies: 10
    Last Post: 10-19-2007, 03:45 AM
  4. convert double to string
    By letdoit in forum C Programming
    Replies: 2
    Last Post: 08-15-2007, 11:08 PM
  5. convert from string to double !
    By Shady in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2006, 07:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21