Thread: Funny characters being printed

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    20

    Funny characters being printed

    Hi all! What i'm trying to do is to store the longest string being input into the program and print it out later. However when i print the longest string, it has some wierd characters at the back. Try inputing the string: there

    Code:
    #define MAX 50
    
    int main()
    {
       char input[MAX];
       char temp [MAX];
       char store[MAX]; //longest string stored here
       int length=0, i, option, choice;
       int maxLength = strlen(store);
    
       choice = 1
       
       switch(choice)
       {
          case 1: printf("Input = ");
                  fgets(input, MAX, stdin);
                  
                  length = stringLength(input);
                  
                  //temp is needed to presevere what is stored in input
                  for(i=0;i<=length;i++)
                  {
                       temp[i] = input[i];
                  }
                   
                  //convertString will manipulate the input
                  if(convertString(input)==1)	
                  {
                      if(length>=maxLength)
             	  {
             	      maxLength = length;
             	      for(i=0;i<length;i++)
             	         store[i] = temp[i];
                          option = 1;
             	  }
                  }
                  fputs(store, stdout); //print out the string
                  break;
            //the rest of case n default case goes here
       }
       return 0;
    }
    // find length of string in array
    int stringLength(char str[])
    {
       int i = 0;
       for(i=0; i<MAX; i++)
       {
           if(str[i]=='\n')
              return i;
       }
       return i;
    }
    Please pardon my silly mistakes I've made if any as I am quite new to C.
    Last edited by learninC; 03-17-2005 at 08:29 AM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Are you sure this is all?
    Code:
    choice =1;
    thats one, still this program doesnt compile.
    edit: just woke up.
    Last edited by InvariantLoop; 03-17-2005 at 08:35 AM.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Its not a complete program, you have to include the header files, <stdio.h>, <ctype.h> and <string.h>. Also convertString is function i wrote, but it doesnt not have any effect on the main code, so I do not put it in. Also, you have to include the function prototype of function stringLength.

    Sorry, cos I tot that most posts here only post snippets of the code.
    Last edited by learninC; 03-17-2005 at 08:31 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Silly me, i just woke up so i just copied and pasted your program. Still choice is undeclared, and you need to provide your function in order to see whats really going on.
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
       char store[MAX]; //longest string stored here
       int length=0, i, option, choice;
       int maxLength = strlen(store);
    Store hasn't been initialized yet so the result of strlen is indeterminate.
    Those funny chars sound like the string not being NULL terminated.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Thx Quantum1024. I was also thinking about the same problem, but how do i initialise it? Cos I want to start of with an empty array.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    oh thanks!!! Quantum1024, your hint has just woke me up. I've managed to solved the problem by initialising store. Thanks for all yr help guys, and thanks InvariantLoop

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1.
    Code:
    fgets(input, MAX, stdin);
    
    length = stringLength(input);
    
    ...
    
    int stringLength(char str[])
    {
       int i = 0;
       for(i=0; i<MAX; i++)
       {
           if(str[i ]=='\n')
              return i;
       }
       return i;
    }
    fgets will store at most 49 meaningfull characters (reserving the 50th one for the NULL terminating character) meaning that the maximum string length for something stored in input should be 49. Your stringLength function however will currently return 50 as the length for such a string.

    It might just be better to use strlen on the input array (not calling stringLength at all) and then check if the last character is a newline or not and change it to a NULL if it is (also adjusting the length by -1):

    Code:
    fgets(input,MAX,stdin);
    length = strlen(input);
    if(length > 0 && input[length-1] == '\n')
    {
        input[length-1] = '\0';
        --length;
    }
    2.
    Code:
    for(i=0;i<=length;i++)
    {
        temp[i ] = input[i ];
    }
    Why? Just use strcpy instead, that's what it is there for.

    3.
    Code:
    char store[MAX]; //longest string stored here
    ...
    int maxLength = strlen(store);
    ...
    if(length>=maxLength)
    {
    maxLength gets set to some indeterminate size at the beginning of the program. The array store can have random junk in it and strlen(store) may return anything from 0 on up. You probably want to set maxLength to MAX initially.
    "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
    Mar 2005
    Posts
    20
    Cool! hk_mp5kpdw thank you so much. I have gain more knowledge on C programming now =). Silly me, din use the strcpy function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. Replies: 1
    Last Post: 03-21-2009, 02:32 AM
  4. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  5. Maps with VC++6 - funny looking warnings
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2003, 07:34 PM