Thread: not running properly

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    not running properly

    So i have this code and I want it to run like this
    cmd> new
    enrer your array
    cmd> list
    *displays array*
    cmd>

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define SIZE 200
    #define HISTORY_SIZE 10 /*the maximum size of the history*/
    //#define MAX_STRING_LEN 100 /*the maximum size of strings*/
    #define MAX_CMD_LEN 10/*the maximum size of a command*/
    
    
    
    
    void getString( char* );
    void printString (const char *);
    void appendString(char *str, char *newstr);
    
    
    
    
    int main()
    {
      char command[MAX_CMD_LEN];
      char string[SIZE];
      char newstr[SIZE];
      int flag;
     do {
        printf("cmd> ");
        fgets(command,  MAX_CMD_LEN, stdin);
    
    
        if(strcmp(command, "new")!=0) {
          getString(string);
        }
        if(strcmp(command, "list")!=0) {
          printString(string);
          }
        if(strcmp(command, "append")!=0) {
          appendString(string, newstr);
        }
    
    
    
    
      }while (flag!=1);
    
    
    
    
    
    
      return 0;
    }
    
    
    void getString(char *string)
    {
    
    
      printf("Please enter your string");
      fgets(string, SIZE, stdin);
    }
    
    
    void printString (const char *string)
    {
     printf( "\n\nHere is the text you entered:\n%s\n", string);
    
    
    }
    void appendString(char *string, char *newstr)
    {
      printf("Enter string to append");
      fgets(newstr, SIZE, stdin);
      strcat(string,newstr);
      fgets(string,SIZE, stdin);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    210
    Please connect this with my other thread problems with pointers since I guess they are different topics and that is why I stopped getting replies

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    fgets() retains the ENTER. You need to compare with "new\n" etc...
    or edit the buffer, removing the ENTER, before doing the comparison.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    210
    I need to use fgets because it doesn't work properly with gets or scanf command.
    Could you elaborate more how should i edit the buffer?

  5. #5

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    210
    Should I put the if statement of the buff in the function of printString?
    for example
    Code:
    if((string = strchr(buf, '/n')) != NULL)
    string = \0;
    and the my printf statement

    Thanks for the article

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    210
    Could someone please tell me the location where should I write it?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by kiwi101
    Could someone please tell me the location where should I write it?
    1. The only place where this matters is where you perform strcmp calls.
    2. The only strcmp calls you do are on the command string.
    3. Therefore... if you want to change the buffer it would need to be after you get the command from the user and before you start calling strcmp.
    4. This would suggest somewhere between lines 28 and 30 in the above code sample.






    Please note that your flag variable is uninitialized. It could happen to be 1 and therefore your loop would only execute once... or it could be any other value and your loop would never end (gracefully that is). You should probably exit the loop whenever the user types in a certain command such as "exit" or perhaps "end".




    I hope that "buffer" code (post #6) isn't what you are really planning on putting in your program, it wouldn't work as you have it written above. You can't assign to an array as you are attempting with "string" (you need another variable [char pointer variable] like the sample/tutorial code shows) and "buf" would need to be "command".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    Please connect this with my other thread problems with pointers since I guess they are different topics and that is why I stopped getting replies
    I think the most likely reason is that the people here are not your private tutors and have also other things to do.
    It's normal that you don't get an immediate answer to your problem on a forum like this.

    So next time please refrain from starting a new thread just because it takes a little bit longer for an answer.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    210
    Sorry I didn't mean to offend you

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    210
    okay so i did what you said and its pretty much printing the same thing except now cmd> just shows up again at the end. I haven't changed my while statement yet i still have to make a command called exit and then make a function with it.
    But could you tell me what I am doing wrong

    the program is the same i just added this
    Code:
     do {
        printf("cmd> ");
        fgets(command,  MAX_CMD_LEN, stdin);
    
    
        if(fgets(command,  MAX_CMD_LEN, stdin) != NULL)
           if((p = strchr( command, '\n')) != NULL)
             *p = '\0';
    
    
        if(strcmp(command, "new")!=0) {
          getString(string);
          }
    
    
        // if((string = strchr(buf, '/n')) != NULL){
        // *string = \0;
        if(strcmp(command, "list")!=0) {
          printString(string);
           }

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fgets(command,  MAX_CMD_LEN, stdin);
    if(fgets(command,  MAX_CMD_LEN, stdin) != NULL)
        if((p = strchr( command, '\n')) != NULL)
            *p = '\0';
    Why do you call fgets() twice before looking for '\n'?

    Code:
    if(strcmp(command, "new")!=0) {
    I've told you already here what's wrong with your comparison.
    (See why it's not a good idea to open several threads for the same topic?)

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    210
    Okay so I removed the first fgets statement and just stuck with the if fgets statement

    Could you elaborate more on how I should fix my comparisons

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    210
    What do you mean by strcmp equals zero if both strings are equal
    which one of my strings are equal?

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What do you mean by strcmp equals zero if both strings are equal
    strcmp - C++ Reference

    Returns an integral value indicating the relationship between the strings: A zero value indicates that both strings are equal...
    ---

    which one of my strings are equal?
    Huh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. For loop not running properly
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 04-04-2008, 05:27 PM
  3. How to properly use flock()
    By samps005 in forum Linux Programming
    Replies: 5
    Last Post: 05-06-2003, 12:22 AM
  4. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:58 PM
  5. Am I using *this properly?
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2001, 06:06 PM