Thread: not running properly

  1. #16
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by kiwi101 View Post
    which one of my strings are equal?
    All strings are created equal. Some are more equal than others. :P

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    210
    OKay I get what you guys are saying and I did that first
    Code:
     if(strcmp(command ,"new")==0) {
          getString(string);
    but then nothing runs at all atleast first I was getting an enter your string value

    I know I have some small error somewhere or some concept I don't get but could you please tell me why my output isn't coming right

    Code:
    #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 buf [BUFSIZ];
      char command[MAX_CMD_LEN];
      char string[SIZE];
      char newstr[SIZE];
      char *p;
      int flag;
    
    
      do {
        printf("cmd> ");
         
    
    
        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);
           }
    
    
        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)
    {
      // if(fgets(buf, SIZE(buf), stdin != NULL){
     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);
    }
    Last edited by kiwi101; 10-23-2012 at 08:21 PM.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    210
    Is it because of my while part of the do while loop, if so what should I write?

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, your do while(flag==1) is a problem, because you haven't assigned a value to flag yet. make it int flag=1; right near the top of the program.

    Everything in your program will be flowing out and returning to that big do while() loop, and that's the way it should be. Call other functions to keep the size of it, reasonable.

  5. #20
    Registered User
    Join Date
    May 2012
    Posts
    210
    So when i intialized the flag =1 the cmd> printed perfectly everytime I wrote enter just as it should. But thats all that happens. If it write new "Enter your string" doesn't pop up. Unless I change the
    Code:
    if(strcmp(command ,"new")!=0)
    but i still get the wrong answer

    i know im really close to getting this code and its frustrating trying to figure out my mistakes
    PLEASE HELP!!!
    Last edited by kiwi101; 10-23-2012 at 08:30 PM.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's great fun to try different things with C, but when you are serious about working on a program, it's time to limit the guessing on this, guessing on that, approach.

    Take it step by step (don't jump ahead of what you're doing atm), and don't go crazy with guesses and emotions. Let's see what's up now.

    Let's change this part, from == NULL, to != NULL, and add the curly braces to it as well. The curly braces aren't required, but your other if statements have them, and it's VERY good to be consistent and clear in your code and coding style.

    Code:
        if(fgets(command,  MAX_CMD_LEN, stdin) != NULL) { //add curly braces
           if((p = strchr( command, '\n')) != NULL)             //change from == NULL to != NULL
             *p = '\0';
        } //add curly brace.
    Give that a try.
    Last edited by Adak; 10-23-2012 at 08:47 PM.

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    210
    You tell me I'm out of ideas of what could be wrong

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up your new code, after you make the changes I mentioned. Report your next problem. Step by step, meter by meter, life is sweeter.

  9. #24
    Registered User
    Join Date
    May 2012
    Posts
    210
    OMGSH your a lifesaver!
    i don't know why i didn't think of that before
    do you know why my list doesn't print out my "appended function"
    it prints out the new just fine

  10. #25
    Registered User
    Join Date
    May 2012
    Posts
    210
    Nevermind got it had to change my function append

    THANK YOU!!!

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kiwi101 View Post
    OMGSH your a lifesaver!
    i don't know why i didn't think of that before
    do you know why my list doesn't print out my "appended function"
    it prints out the new just fine
    Actually, I HAVE BEEN a lifesaver, but not any more for sure.

    You're welcome, Kiwi.

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