Thread: i wonder what the error is

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    i wonder what the error is

    i wrote a program,wanting to type in several sentences and count how many words there are in each one,then output the number of the line where the sentence has most words.


    Code:
    #include<stdio.h>
    main()
    {int str[10][10];
    int a[10]
    int m,c,i,j;
    {for(i=0;i<10;i++)
    for(j=0;j<10;j++)
    (c=getchar())=str[i][j];}
    {a[i]=0;
    for(i=0;i<10;i++)
    for(j=0;j<10;j++)
    while(str[i][j]=='\n'||str[i][j]=='\0')
    a[i]+=1;}
    i=0;
    if(a[i]<=a[i+1]&&i<9)
    
    m=a[i+1];
    else
    m=a[i];
    i++;
    printf("%d\n",m);
    }
    thanx
    Last edited by hahan; 03-15-2009 at 11:10 PM. Reason: for spelling mistake

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by hahan View Post
    i wrote a program,wanting to type in several sentences and count how many words there are in each one,then output the number of the line where the sentence has most words.


    Code:
    #include<stdio.h>
    
    main()
    {
       int str[10][10];
       int a[10]
       int m,c,i,j;
       {
       for(i=0;i<10;i++)
          for(j=0;j<10;j++)
             c=getchar())=str[i][j]; 
    /* if fyou're assigning letters to str[][], then str[][], needs to be on the left 
    hand side of this assignment code, not the right hand side.
    */
    
          {
             a[i]=0;
             for(i=0;i<10;i++)
                for(j=0;j<10;j++)
                   while(str[i][j]=='\n'||str[i][j]=='\0')
                      a[i]+=1;
       }
    
             i=0;
            if(a[i]<=a[i+1]&&i<9)
                m=a[i+1];
            else
                m=a[i];
         
             i++;
             printf("%d\n",m);
       }
    thanx
    Ok. You'll need a char array or other char data structure. Yours is int, but you're putting char's in it, anyway.

    Your sentence may need more than just 10 char's, also. I'd increase it to 50 char's per row, or so.

    Here's what I'd recommend. Take one sentence into your array, and work to count the words in that one sentence. Then try and expand that working code, into multiple sentences.

    And please, indent your code. It is almost unreadable, as it is.

    You've got enough loops to sink a battleship, please add no more loops!

    I suggest you start your word count with the first letter you find in the sentence, and reset it for the next word when you discover a space or newline char, instead of when it finds an end of string marker - '\0' or newline.

    Start simple, and make it something that will compile with only a few errors, at most. Nothings worse than having 100 lines of code, with 73 errors, staring you in the face!

    Take it step by step, and check it by printing out what you THINK the program is doing, with it's actual variables, as it goes along. (Watch values also works well).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    but it seems only 5 erroes that the m,c,i,j, are undefined variables and a expression sntax which i don't where.anyway,thanx a lot,i'll rethink abou it

  4. #4
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    how do you exit from the program?

    Code:
    #include<stdio.h>
    
    main()
    {
       int str[10][10];
       int a[10];
       int m,c,i,j;
       {
       for(i=0;i<10;i++)
          for(j=0;j<10;j++)
             c=getchar();
             c=str[i][j]; 
    /* if fyou're assigning letters to str[][], then str[][], needs to be on the left 
    hand side of this assignment code, not the right hand side.
    */
    
          {
             a[i]=0;
             for(i=0;i<10;i++)
                for(j=0;j<10;j++)
                   while(str[i][j]=='\n'||str[i][j]=='\0')
                      a[i]+=1;
       }
    
             i=0;
            if(a[i]<=a[i+1]&&i<9)
                m=a[i+1];
            else
                m=a[i];
         
             i++;
             printf("%d\n",m);
       }
    }
    i just compiled and gcc gave me the errors...
    i corrected the errors and gave you the corrected code...the logic is up to you
    Last edited by creeping death; 03-19-2009 at 09:47 AM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    pls send your code to me ,creeping death. tons of thanks.the problem now i find is how to get the char into the char string.i'll refer to books whether getchar or scamf function's use.anyway thanks a lot.

Popular pages Recent additions subscribe to a feed