Click to See Complete Forum and Search --> : Finish Loop


tsacol
March 29th, 2003, 12:25 AM
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.

SeventhStar
March 29th, 2003, 05:15 AM
if( s[i] = (endlength - 1) )
s[i] == ',' ;


these are the two lines... Are you sure that you wanted this. I dont think so....


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>

mwilliamson
March 29th, 2003, 09:11 AM
your code should be

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;
}

caffineplease
March 29th, 2003, 04:45 PM
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 :



if((endlength - 1) == s[i])
{
s[i] = ',' ;
}


by applying this practice if you happen to put the following statement by accident, as you did :( :


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!:o