Thread: Replace Function I created

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Replace Function I created

    Okay, so my problem is I have created a replace function that allows an argument to choose which node to be replaced and enter a new one. I have a code that works fine, but it uses integers and I need it to use strings instead. My code with strings will not work and terminates the program after I enter what I want to replace the chosen word with. I will put the two codes below.

    Code:
    void rep(int i) { 
      int old, n;
      struct node* temp;
      temp = root;
      old = i;
    
    
      printf("\nEnter what you want to replace with: ");
      scanf("%d", &n);
    
    
      if(temp == NULL) {
        printf("No such index\n");
        return NULL;
    }
      while (temp != NULL) {
      if(temp->data == old) {
        temp->data = n;
          printf("Replaced\n");
    
    
      }
      temp = temp->next;
    }
    }
    And this is the code I need fixed:
    Code:
    // Command replace
    void rep(char* i) { 
      char old, n;
      struct node* temp;
      temp = root;
      strcmp(old,i);
    
    
      printf("\nEnter what you want to replace with: ");
      scanf("%s", &n);
    
    
      if(temp == NULL) {
        printf("No such index\n");
        return NULL;
    }
      while (temp != NULL) {
      if(strcmp(temp->text, old) == 0) {
        strcpy (temp->text, n);
        printf("Replaced\n");
      }
      temp = temp->next;
    }
    }

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I don't understand your variables. What are root and text? What does your struct node look like? If you have more code, I think it would be helpful to me to be able to see the whole thing even if it is just this one function that should be concentrated on.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,749
    This line is wrong.

    Code:
    char old, n;
    You can not put an c-string into an single char!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-integers from created function?
    By minty33 in forum C Programming
    Replies: 4
    Last Post: 10-27-2012, 04:04 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. A data structure i created with a function
    By Darkinyuasha1 in forum C Programming
    Replies: 23
    Last Post: 11-14-2006, 10:03 PM
  4. Using DrawDoll(); function (self created)
    By bluehead in forum Windows Programming
    Replies: 8
    Last Post: 11-03-2002, 04:49 PM
  5. Replies: 13
    Last Post: 04-29-2002, 11:06 PM

Tags for this Thread