Bored Coders

This is a discussion on Bored Coders within the C Programming forums, part of the General Programming Boards category; Can anyone tell me why the output here is garbled? It's supposed to place words in one container, everything else ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705

    Bored Coders

    Can anyone tell me why the output here is garbled?

    It's supposed to place words in one container, everything else in the next, so on and so on till the array of structures is filled. Instead, well, compile for yourself and see!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    
    typedef struct temporary
    {
    char t[25];
    };
    
    
    
    
    int main()
    {
    
    
    char buffer[]="   This is 1very long sentence. It may be boring 4 you to read, and you may care < about what it says but it is #1 for Testing!!!";
    
    
    
    
    int start_word, u = 0;
    int count=0;
    
    ///////////// First Count ///////////////////////////
    
    if( isalpha(buffer[0])  ){start_word = 0;}         // <---Precondition
    else if( !isalpha(buffer[0])  ){start_word = 1;}  // <---
    
    for( yy = 0; yy< strlen(buffer);yy++)
    {
    
    
    if( start_word == 0)
    {
    
    
     if( !isalpha(buffer[yy]) )
      {
       start_word = 1;
       count++;
      }
    }
    
    else if( start_word == 1)
    {
    
    
     if( isalpha(buffer[yy])   && buffer[yy] != '\n'  )
      {
    
        start_word = 0;
        count++;
      }
    }
     }
    /////////////// End Counting ///////////////////////
    
    char tem[25];
    
    struct temporary temp[count];   <---use count
    
    int s = 0;
    
    
    
    if( isalpha(buffer[0])  ){start_word = 0;}         // <---Precondition
    else if( !isalpha(buffer[0])  ){start_word = 1;}  // <---
    
    for( yy = 0; yy<  strlen(buffer);yy++) 
    {
    
    
    if( start_word == 0)
    {
    
    
    
     if(  isalpha(buffer[yy])   )
      {
       tem[u] = buffer[yy]; u++;
      }
    
    
     if( !isalpha(buffer[yy])    )
      {
    
       ///tem[u-1] = '\0';
       strcpy(temp[s].t,tem);
       start_word = 1;
       u = 0;
       s++;
       strcpy(tem,"");
      }
    }
    
    else if( start_word == 1)
    {
    
    
     if( !isalpha(buffer[yy])    )
      {
       tem[u] = buffer[yy]; u++;
      }
    
    
     if( isalpha(buffer[yy])    )
      {
       ///tem[u-1] = '\0'; <--didn't help
       strcpy(temp[s].t,tem);
       start_word = 0;
       u = 0;
       s++;
       strcpy(tem,"");
      }
    }
     }
    
    
    
    
    for(i=0;i<count;i++){ printf("%s",temp[yy].t);  getch(); }
    
    
    return 0;
    }
    Last edited by Sebastiani; 09-23-2001 at 03:44 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay, here's some stuff...
    1) Did you declare yy before you used it? I can't see it, but I'm inclined to think you did because otherwise it shouldn't compile.

    2)
    Code:
    for(i=0;i<count;i++){ printf("%s",temp[yy].t);  getch(); }
    I'm pretty sure there's your problem, since at the time this line is called, yy = strlen(buffer), which is generally bad. I haven't comiled the code, but what I'm guessing is you're getting a long string of meaningless characters (possibly not), and you just keep getting the same characters every time you hit a key, for count times.

    EDIT: This is probably a good time to point out a pretty good rule of thumb. When your output is meaningless, the problem is usually your output routine reading from the wrong memory. If the problem was in your input routine, the program would just crash, and if it were the logic of your program, it'd either crash, or you'd get wrong, but meaningful answers....
    This is just a rule of thumb however.
    Last edited by QuestionC; 09-23-2001 at 07:32 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Sorry, I'm using the macro "#define forr(n) for(yy=0;yy<n;yy++)" and replaced it with the "i" loop in the post so as not to confuse. But yes, all is being declared, it just doesn't work correctly. The logic of the code seems pretty straight-forward to me but apparently I'm wrong...
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Pro Coders!
    By pabloaimer in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 09:58 AM
  2. Coders
    By askala_mud in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 12-09-2004, 05:39 AM
  3. if you are bored
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 06-25-2003, 03:24 PM
  4. Bored
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-21-2002, 06:03 AM
  5. just sharing how bored i am
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-24-2001, 08:51 PM

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