Thread: problems in decoing userinput

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    problems in decoing userinput

    the function decode input suppose to copy first letter of array to character c and then copy rest of array to character array input2.
    but it copies first letter to c, but it does not copy rest of array to input 2. after run decodeinput(), input2 is always empty.
    i put printf("userinput"); to check gets and it works fine.
    what can be wrong? I tried fgets but same result..

    Code:
    gets(userinput); /* Get user input*/ 
    /* copy first letter of array to c and copy rest of array to input2 */ 
    decodeinput(userinput); 
    
    /*Insert Character to List*/ 
    InsertCharacter(buffer, input2); 
    
    void decodeinput(char input[100]) 
    { 
    int i; 
    int max; 
    char input3[100]; 
    
    strcpy(input3,input); /* Copy input to input3 */ 
    
    strupr(input3); /* Make command to upper case */ 
    
    if('0' < input[0] && input[0] <'9') 
    num = input3; 
    else 
    {   
         
        c = input3[0]; /* Store first letter of array to a character c */ 
      num = 0; 
    } 
    max = strlen(input3); 
    for(i=1;i< max; i++) 
    input2-> next = input[i]; 
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's usually a BadThing(TM) when you spot problems in the first line of code:
    Code:
    gets(userinput); /* Get user input*/
    You forgot three things:
    1) The letter 'f'.
    2) The second argument, which is the size of the buffer.
    3) The third argument, which is the stream to read from. In this case: stdin

    Vola:

    fgets( buf, BUFSIZ, stdin );

    No, you're technicly not incorrect, but gets is a nasty piece of code which may cause your otherwise happy little function to crash.

    Furthermore, since you didn't use fgets, it's possible that I can crash your line with this code, assuming you've gotten this far:
    Code:
    strcpy(input3,input); /* Copy input to input3 */
    Again, we can overflow your buffer here, assuming the first line is over 100 characters. I prefer strncpy, which avoids the whole mess when used correctly.
    Code:
    strupr(input3);
    My compiler doesn't have this function. You should use ANSI standard functions whenever you have the option, or write your own to do the job.
    Code:
    if('0' < input[0] && input[0] <'9') 
    num = input3; 
    else 
    {   
         
        c = input3[0]; /* Store first letter of array to a character c */ 
      num = 0; 
    }
    What is num? It's not declared in your code anywhere. Unless it's a ponter, your assignment is incorrect. Even if it is a pointer, there's a pretty good chance (I'd be my paycheck on it) that at least one of your assignments is incorrect. (IE: It's not doing what you thing it is, nor is it doing what you want it to.)
    Code:
    for(i=1;i< max; i++) 
    input2-> next = input[i];
    And one final time... What is input2? Additionally, array indexes start at zero, not one.

    Quzah.
    Last edited by quzah; 11-15-2002 at 10:14 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    32
    the input2 is global variable, it suppose to take rest of characters in array. Since input[0] has first character to tell what to do, I want to copy characters other than input[0] to input2.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Naturally you missed the point. How are we supposed to fix incomplete code?
    1) Read what I've written.
    2) Become wise.
    3) ???.
    4) Profit!

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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I want to copy characters other than input[0] to input2.
    Simple, just slice off the first element:

    strncpy ( input2, input + 1, 100 );

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM