CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Finish Loop

  1. #1
    Join Date
    Mar 2003
    Location
    Auckland
    Posts
    9

    Finish Loop

    This code compiles but performs an illegal operation when i run it. It was fine until i added lines 41 & 42. I put these lines in so that the function would break out of the loop when it came to the end of the string. Could someone please tell me where it is wrong.
    Attached Files Attached Files
    tsacol

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    Code:
    if( s[i] = (endlength - 1) )
                       s[i] == ',' ;
    these are the two lines... Are you sure that you wanted this. I dont think so....

    Code:
    if( s[i] == (endlength - 1) )
                       s[i] = ',' ;
    this would look better.. but i still don't like it. Comparing a character to an integer a part of a word to a the word's length ????

    also try using <iostream> intstead of <iostream.h>
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    your code should be

    Code:
    int wrd_lcte( const char* s, char* t, int i ) 
    {
            int endlength = strlen(s);
    
    	for( int j = 0;i < endlength && s[i] != ',';i++,j++ )
    		t[j] = s[i];
    
            t[j] = 0;
            return i;
    }
    Last edited by mwilliamson; March 29th, 2003 at 05:59 PM.

  4. #4
    Join Date
    Jul 2000
    Location
    In a flat, Wimbledon, UK
    Posts
    251
    tsacol,

    Just a solution to your problem : To overcome the errors of putting an assignment operator instead of an equality operator you could do the following :



    Code:
    if((endlength - 1)  == s[i])
    {
         s[i] = ',' ;
    }
    by applying this practice if you happen to put the following statement by accident, as you did :

    Code:
    if((endlength - 1)  = s[i])
    {
        s[i] = ',' ;
    }
    then the compiler would flag an error stating that the left operand must be an l-value.


    Hope that has helped

    Regards

    John

    My reply postings are being treated as new threads... must be my crap ISP, which is constantly crapping out on me!
    Ask a question and you're a fool for three minutes;
    do not ask a question and you're a fool for the rest of your life.
    - Chinese Proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured