Originally Posted by
pooty tang
Ok I used the strcmp(csh, "bye") on the outer while loop but it does not exit the while loop when I type bye. It isn't recognizing that they are equal.
I tried using strcmp(csh, "bye") and strcmp(csh, bye) with bye being predefined and either way it always returns that they are unequal.
Here's a suggestion: when something like a comparison fails, put a printf() just before the comparison to see what's really happening.
In this case, that's before the while(strcmp...) loop and just before the end of the while(strcmp...) loop
For example
Code:
printf ("csh: <%s>\n", csh);
while(strcmp(csh, "bye") != 0)
{
/* here's your loop calculations etc. */
printf ("csh: <%s>\n", csh);
} /* end of the while (strcmp...) */
You would see something like
sh% This is a test
csh: <This>
sh% bye
csh: <bye
>
sh%
See the line
There is a newline after the bye (as Dave_Sincula's post indicated).
Solve the problem any way you want: get rid of the '\n' in the string that you got from fgets(), use strtok(" \n"), instead of strtok(" "), or whatever.
My point is: use printf() to see what's happening. Once you find the problem, you can usually solve it more quickly than posting a request for help and waiting for a helpful response.
By the way: Before exiting the program, don't forget to free() all of the memory that you allocated (!)
Regards,
Dave