Thread: what is wrong in my code?

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

    what is wrong in my code?

    this is a code which is used for transforming the numbers to the words,i did something here but , this returning ,pointers gives errorcould you help me please?
    i


    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    
    //*************************************************************************************
    char *hane1(int sayi)
    {
    char zero[]="zero\0";
    char one[]="one\0";
    char two[]="two\0";
    char three[]="three\0";
    char four[]="four\0";
    char five[]="five\0";
    char six[]="six\0";
    char seven[]="seven\0";
    char eight[]="eight\0";
    char nine[]="nine\0";
    char *kelime;
    
    
        if (sayi==0) kelime=zero;
        if (sayi==1) kelime=one;
        if (sayi==2) kelime=two;
        if (sayi==3) kelime=three;
        if (sayi==4) kelime=four;
        if (sayi==5) kelime=five;
        if (sayi==6) kelime=six;
        if (sayi==7) kelime=seven;
        if (sayi==8) kelime=eight;
        if (sayi==9) kelime=nine;
    
    
        //printf("**%s**",kelime); //in here it writes the value true but when returning the
    	//main function(kelime) it doesnt work
    
    
        return kelime;
    }
    //*************************************************************************************
    char *hane2(int sayi)
    {
    char zero[]="zero\0";
    
    
    char t0[]="ten\0";
    char t1[]="eleven\0";
    char t2[]="twelve\0";
    char t3[]="thirteen\0";
    char t4[]="fourteen\0";
    char t5[]="fifteen\0";
    char t6[]="sixteen\0";
    char t7[]="seventeen\0";
    char t8[]="eighteen\0";
    char t9[]="nineteen\0";
    
    
    //
    char o2[]="twenty\0";
    char o3[]="thirty\0";
    char o4[]="fourth\0";
    char o5[]="fifty\0";
    char o6[]="sixty\0";
    char o7[]="seventy\0";
    char o8[]="eighty\0";
    char o9[]="ninety\0";
    char *kelime2;
    
    
    
    
    if((sayi<20)&&(sayi>=10))
    {
        if (sayi==10) kelime2=t0;
        if (sayi==11) kelime2=t1;
        if (sayi==12) kelime2=t2;
        if (sayi==13) kelime2=t3;
        if (sayi==14) kelime2=t4;
        if (sayi==15) kelime2=t5;
        if (sayi==16) kelime2=t6;
        if (sayi==17) kelime2=t7;
        if (sayi==18) kelime2=t8;
        if (sayi==19) kelime2=t9;
    }
    
    
    
    
    if((sayi<30)&&(sayi>=20)) kelime2=o2;   
    if((sayi<40)&&(sayi>=30)) kelime2=o3;
    if((sayi<50)&&(sayi>=40)) kelime2=o4;
    if((sayi<60)&&(sayi>=50)) kelime2=o5;
    if((sayi<70)&&(sayi>=60)) kelime2=o6;
    if((sayi<80)&&(sayi>=70)) kelime2=o7;
    if((sayi<90)&&(sayi>=80)) kelime2=o8;
    if((sayi<100)&&(sayi>=90)) kelime2=o9;
    if(sayi==0) kelime2=zero;
    
    
    return kelime2;
    }
    
    
    //************************************************************
    int main()
    {
    int sayi=0;
    char *kelime1;
    char *kelime2;
    
    
    printf("Please enter a number [0-99]:");
    scanf("%d",&sayi);
    printf("\n");
    
    
    
    
    if((sayi<10)&&(sayi>=0))
    {
        kelime1=hane1(sayi);
        printf("-%s-",kelime1);
    }
    
    
    if(((sayi<100)&&(sayi>10))&&(sayi%10!=0))
    {
        kelime1=hane2(sayi);
        kelime2=hane1(sayi);
        printf("-%s%s-",kelime1,kelime2);
    }
    
    
    if((sayi%10==0))
    {
        kelime1=hane2(sayi);
        printf("-%s-",kelime1);
    }
    
    
    getche();
    return 0;
    }
    Last edited by rac1; 12-25-2011 at 08:43 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you do this, you don't need all this code. Way too much.

    You want one array, like numberWords[]. And the strings that it holds, are the strings (words), equal to the number of it's index:
    Code:
    char numberWords[0]={"zero"}; //no \0 on the end of it, C does that for you.
    numberwords[1]={"one"};
    numberwords[2]={"two"};
    .....etc.
    numberwords[50]={"fifty"};
    .....etc.
    However high you want to go.

    You want to associate the word, with a number. You have associated the word with a word (the name of an array). You need to change that association and make it word >> to >> number, not word >> to word >> maybe to number.
    Last edited by Adak; 12-25-2011 at 08:46 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    it doesnt working, it is only possible with structure code.
    i only didnt solved the returning to the main function.
    also i ordered the code again and 0 error.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's an example of what I was describing:

    Code:
    #include <stdio.h> 
    
    int main(void) { // 
       int number;
       char *numWords[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
       printf("\nEnter a number from zero to nine, and I'll print out that word: ");
       scanf("%d", &number);
       printf("The word for %d is %s \n",number, numWords[number]);
    
       printf("\n");
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Quote Originally Posted by Adak View Post
    Here's an example of what I was describing:

    Code:
    #include <stdio.h> 
    
    int main(void) { // 
       int number;
       char *numWords[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
       printf("\nEnter a number from zero to nine, and I'll print out that word: ");
       scanf("%d", &number);
       printf("The word for %d is %s \n",number, numWords[number]);
    
       printf("\n");
       return 0;
    }
    Thanks for code ADAK,
    i havent see this using of pointers before,That is very good,
    thank you very much again.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're welcome, and quite right, that is a nice feature of C. You can do it in a very similar way with regular char arrays

    Code:
    char numWords[]={ {"zero"},{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"}};
    And the rest is the same.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    always i stop somewhere and this steals my time too much, because i am stubborn.Thanks very much to this website owners and the members who helps us to solve our question.

    here is my code.I did it finally.
    My project was , reading a file and if there is a number 0-99, change it with its name(1=one, or 33=thirtythree) and save it.
    at past you said me make the programs in parts, i did this part and i've already did other part of main program, it is time to combine them :P
    this is very dramatic. if it works Yeah i did.
    otherwise wha wha what, where is wrong, lets solve it :P .


    Code:
    #include <stdio.h>
    
    int main()
    {
        int number;
        char rakam[14];
        char *birlik[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
        char *onluk[]={"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        char *onyirmi[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineten"};
        int sayi=0;
        char *kelime1;
        char *kelime2;
    
    
    printf("Please enter a number [0-99]:");
    scanf("%d",&sayi);
    printf("\n");
    
    
    
    
    if((sayi<10)&&(sayi>=0))
    {
        printf("-%s-",birlik[sayi]);
    }
    
    
    if(((sayi<100)&&(sayi>19))&&(sayi%10!=0))
    {
        printf("-%s%s-",onluk[(sayi-sayi%10)/10-1],birlik[sayi%10]);
    }
    
    
    if((sayi%10==0)&&(sayi!=0))
    {
        kelime1=onluk[sayi/10-1];
        printf("-%s-",kelime1);
    }
    if((sayi<20)&&(sayi>10)) printf("-%s-",onyirmi[sayi%10-1]);
    
    
    getche();
    return 0;
    }

    look my first code 140 line ; after help our program is 45 line
    here is the importance of helping
    thanks too much
    Last edited by rac1; 12-25-2011 at 10:05 AM.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Nice job, but you can get rid of a few more things:

    - kelime1 is only used for onluk[sayi/10-1], so why make an entirely new variable?
    - keime2, rakam, and number are unused variables
    - no idea what the getche() at the end does..

    Also, not really necessary, but because the "ty" is repeated across onluk, and the "teen" across onyirmi, it could be manually added in the printf later and not allocated in the variable.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're very welcome, and congrats on completing your program.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Quote Originally Posted by memcpy View Post
    Nice job, but you can get rid of a few more things:

    - kelime1 is only used for onluk[sayi/10-1], so why make an entirely new variable?
    - keime2, rakam, and number are unused variables
    - no idea what the getche() at the end does..

    Also, not really necessary, but because the "ty" is repeated across onluk, and the "teen" across onyirmi, it could be manually added in the printf later and not allocated in the variable.
    yes you are right , but this is a part of main program, which is read file and if you see 0-99 numbers , write ont file as the name of number and save.
    after i will use this function to find number and replace the word.
    that kelimemain will be the replaced word, and i am dealing with other codes to combine with them with this code.

    Code:
    #include <stdio.h>
    
    char kelimemain[14];
    
    
    int main()
    {
        int number;
        char rakam[14];
        char *birlik[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
        char *onluk[]={"ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
        char *onyirmi[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
        int sayi=0,i,r;
        int length=0,length1=0;
        char *kelime1;
        char *kelime2;
        char *kelime3;
    printf("Please enter a number [0-99]:");
    scanf("%d",&sayi);
    printf("\n");
    
    
    
    
    if((sayi<10)&&(sayi>=-1))
    {
        kelime1=birlik[sayi];
        printf("-%s-",kelime1);
    }
    
    
    if(((sayi<100)&&(sayi>19))&&(sayi%10!=0))
    {
        kelime1=onluk[(sayi-sayi%10)/10-1];
        kelime2=birlik[sayi%10];
        //printf("-%s%s-",kelime1,kelime2);
    }
    
    
    if((sayi%10==0)&&(sayi!=0))
    {
        kelime1=onluk[sayi/10-1];
        //printf("-%s-",kelime1);
    }
    if((sayi<20)&&(sayi>10))
    {
        kelime1=onyirmi[sayi%10-1];
        //printf("-%s-",onyirmi[sayi%10-1]);
    }
    
    
    for(i=0;kelime1[i]!='\0';i++) length++;
    length1=length;
    //printf("%d\n",length);
    for(i=0;kelime2[i]!='\0';i++) length++;
    //printf("%d\n",length);
    for(i=0;i<length1+1;i++) kelimemain[i]=kelime1[i];
    r=0;
    for(i=length1;i<length+1;i++,r++) kelimemain[i]=kelime2[r];
    
    
    //printf("%d",length);
    
    
    printf("\nzz--%s--zz",kelimemain);
    return length;
    }
    some compilers are ,when you press f5 execute, it appears and otomaticly close the program.so i put getche(); to see what is there on the screen.
    rakam and others not important , i usually delete and fix the warnings after my program works perfectly.
    Last edited by rac1; 12-25-2011 at 10:43 AM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Work on that indentation Rac1, for your sake as much as your readers. EVERY level of subordination in your code, should be indented 2-5 spaces:
    Code:
    for(i=0;i<10;i++)
       printf("%3d  ",i); //note the indentation of 3 spaces

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would put "zero" to "nineteen" all in the same array, because they are all special cases.
    This simplifies things further.
    The generic cases only start from "Twenty" and up.

    What about hundreds, thousands and millions etc; Do you need to do that?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    0-99 is required in this project.others are not important.
    thanks for posting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code here???
    By kahad in forum C Programming
    Replies: 16
    Last Post: 11-08-2006, 04:28 PM
  2. What would be wrong with this little code...
    By DiGiT in forum C++ Programming
    Replies: 7
    Last Post: 12-24-2004, 09:32 PM
  3. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  4. What is wrong with this code??? Can U help??
    By kickass in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 03:34 AM
  5. what's wrong in this code?
    By HOWY in forum C Programming
    Replies: 2
    Last Post: 06-22-2002, 07:11 PM