Thread: Need help to fix code

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Need help to fix code

    Hello guys, I made program using with c. And i found compiling is not successful because small error. Please see full coding work below and error is highlighted in colour. Please help me to fix this code as i have to give this Assignment today. Do something. Thanking you
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    #define STR_SIZE 100
    
    struct node
    {
    char data;
    struct node *next;
    };
    
    void start(void);
    void getString(void);
    void makeLinkList(struct node *head);
    void printInPutStr(struct node *head);
    void getNumber(void);
    void replaceNode(num inPut, struct node *head); 
    void printOutPutStr(struct node *head);
    void printStr(struct node *head);
    void end(void);
    void emptyLinkedList(struct node *head);
    
    
    
    char inPutStr[STR_SIZE];
    num number;
    
    
    int main(void)
    {
    char inPutChar; // USED AS PART OF PROGRAM TERMINATION
    num inPut; // HOLDS VALUE OF INDEX ENTERED BY THE USER
    struct node *head = NULL; // SAME
    int count; // INDEX VALUE
    
    printf("\n Advanced Assignment - Data Flow Structures & Linked Lists (V1.01)");
    printf("\n -----------------------------------------------------------------\n\n");
    
    printf(" USER INSTRUCTIONS \n");
    printf(" > Write a program that creates a linked list of characters from a string \n");
    printf(" > entered by the user.\n");
    printf(" > The program is then to accept a second string and an index from the user. \n");
    printf(" > The second string is then to be inserted into the first string at the \n");
    printf(" > location given by the index. \n\n");
    
    start();
    
    getString();
    
    head = (struct node*) calloc(1, sizeof (struct node));
    
    makeLinkList(head);
    
    printInPutStr(head);
    
    inPut = getNumber();
    
    replaceNode(inPut, head);
    
    printOutPutStr(head);
    
    end();
    }
    
    
    void start(void)
    {
    printf(" STARTING PROGRAM...\n");
    }
    
    
    void getString(void)
    {
    printf(" Please enter a string (Maximum length %d characters) \n\t C:\> ", STR_SIZE);
    gets(inPutStr);
    fflush(stdin);
    
    while(inPutStr[0] == 0 || inPutStr[0] == ' ')
    {
    printf("\n\t NOTHING INSERTED! RE-ENTER A STRING \n\t C:\> ");
    gets(inPutStr);
    fflush(stdin);
    }
    }
    
    
    void makeLinkList(struct node *head)
    {
    struct node *current, *previous;
    int i;
    
    current = head;
    current->next = (struct node*)calloc(1, sizeof(struct node));
    current=current->next;
    for ( i = 0 ; inPutStr[i] != 0 ; ++i )
    {
    current->data= inPutStr[i];
    current->next = (struct node*)calloc(1, sizeof(struct node));
    previous = current;
    current = current->next;
    }
    free(current);
    previous->next = NULL;
    current = head;
    }
    
    
    void printInPutStr(struct node *head)
    {
    printf("\n USER INPUT:\n > ");
    emptyLinkedList(head);
    printStr(head);
    }
    
    
    num getNumber(void)
    {
    int inP1, inP2;
    printf("\n\n Enter Index value \n\t C:\> ", STR_SIZE);
    scanf("%d",&number.inP1);
    fflush(stdin);
    
    while(number.inP1<1)
    {
    printf("\n\t INVALID ENTRY! RE-ENTER DIGIT \n\t C:\> ");
    scanf("%d",&number.inP1);
    fflush(stdin);
    }
    
    return (number);
    }
    
    void replaceNode(num inPut, struct node *head)
    {
    struct node *current, *previous, *forward;
    int i=0,j=0;
    int l,c;
    l=number.inP1;
    current = head;
    
    for( ; j<l; j++)
    {
    current=current->next;
    }
    
    while (current!=NULL)
    {
    inPutStr[i]=current->data;
    current=current->next;
    i++;
    }
    
    current = head;
    j=0;
    
    while(j<=l+1)
    {
    inPutStr[i]=current->data;
    current=current->next;
    i++;
    j++;
    }
    
    current = head;
    i=0;
    
    while(current!=NULL)
    {
    current->data = inPutStr[i];
    i++;
    current = current->next;
    }
    
    }
    
    
    void printOutPutStr(struct node *head)
    {
    printf("\n MODIFIED STRING:\n > ");
    emptyLinkedList(head);
    printStr(head);
    }
    
    
    
    void printStr(struct node *head)
    {
    struct node *current;
    
    current = head;
    do
    {
    current = current->next;
    printf("%c", current->data);
    
    }
    while(current->next != NULL);
    }
    
    
    void end(void)
    {
    char inPutChar;
    printf(" PRESS ANY KEY TO EXIT PROGRAM...\n");
    scanf("%c",&inPutChar);
    fflush(stdin);
    }
    
    
    void emptyLinkedList(struct node *head)
    {
    if(head->next == NULL)
    {
    printf("\tEmpty Linked List!!!!");
    }
    }
    Last edited by Salem; 05-28-2010 at 09:44 AM. Reason: Restoration

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, I've done something - I just stood up and jumped on one leg across the room. That will give you good luck.

    However, if you want more constructive help, I could perhaps help you figure out what the error is if you tell us what the actual error is...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You didn't try to declare a variable of type num, did you?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem with your code is that there's a HUGE RED LINE in the middle of it. Your compile doesn't understand HUGE RED TEXT. Try using smaller letters, and maybe a different color.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    I told you code in red colour was highlighted when i compiled it. I used Dev Shed software. The error said:

    1) variable or field `replaceNode' declared void
    2) `num' was not declared in this scope
    3) expected primary-expression before "struct"

    Can you help me with this minor problem?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think post #3 already gives the answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    16
    Oh my god. I made huge red text to show you which code has an error after compiling. Coding size has nothing to with error.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Perhaps by num you meant "int"?

    It's a tough house sometimes
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sometimes Pinky, I even amuse myself.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    16
    As i followed what MK27 told me about "num" to "int". I changed it. Again i got an error with this code
    Code:
      inPut = getNumber();
    How to change this? Instead of this what would be the new code.

    Thanking You

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't suppose you could happen to remember WHAT ERROR you got? Those little word-thingies actually tell you what's wrong if you read them.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by anik18 View Post
    As i followed what MK27 told me about "num" to "int". I changed it. Again i got an error with this code
    Code:
      inPut = getNumber();
    How to change this? Instead of this what would be the new code.

    Thanking You
    first thing... inPut is declared as num. I hope you have changed it to int.
    second getNumber declaration void getNumber(void); it is not matching with the way you are using..

    your declaration should have num/int getNumber(void). Give a return inside the function.

    Third from next time when you post, give the compiler error too. That will make the job easy.
    Last edited by chakra; 05-28-2009 at 07:56 PM.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    16
    the errors are:
    1) In function `int main()':
    2) void value not ignored as it ought to be
    3) [Warning] unknown escape sequence '\>'

    Now what to do.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to use \\ if you want to have a \ in a string.
    Code:
    printf("\n\n Enter Index value \n\t C:\> ", STR_SIZE);


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    16
    Still there is a problem with this code
    Code:
     inPut = getNumber();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Replies: 1
    Last Post: 05-26-2006, 08:13 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM