Thread: Parsing a line and saving args

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    YEAH!!! That did the trick, thanks so much guys I really appreciate everybody's help.

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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
    csh: <bye
    >
    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
    Last edited by Dave Evans; 09-09-2004 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Replies: 5
    Last Post: 03-02-2009, 08:33 AM
  3. Question about parsing a string from a line
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 04-23-2005, 03:18 PM
  4. Reading lines from a file and saving to a variable
    By Rare177 in forum C Programming
    Replies: 1
    Last Post: 06-09-2004, 03:47 PM
  5. Reading a line from a text file Please help
    By Blizzarddog in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 12:35 PM