Thread: Not sure why the output is strange characters

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    10

    Not sure why the output is strange characters

    So this snippet of code is supposed to go through a file and pick out some strings, when I print it inside the loop it works fine, however when I print the itemArr array outside of the loop it displays weird characters like a question mark in a box.

    Code:
    int encoding = 0; //variable to represent whats in the room 0-8 (9 different items)
        int temp;
        char item[18];// item
        char itemArr[9][18]; //array of items in the room
        int numItems[9]; //number of item in each room
    
    
         while (fscanf(input, "%i %s", &temp, item) == 2){
            if(strlen(item) > 2){
                strcpy(itemArr[encoding], item);
                printf("%s\n", itemArr[encoding]);
                encoding++;
            }
            encoding++;
            while((le = fgetc(input)) != '\n' && le != EOF);
        }
        printf("%s",itemArr[0]);
    for some reason the itemArr array works fine inside the while statement, but after the statement ends the print function prints strange characters instead.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why is encoding incremented twice for each loop?

    Is encoding overflowing the bounds of itemArr ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you are reusing them, it would be better to avoid magic numbers with some named constants, e.g.,
    Code:
    #define ITEM_MIN_LEN 3
    #define ITEM_MAX_LEN 17
    #define NUM_ROOMS 9
    Then, you can make use of these to ensure that your definitions don't get out of sync, and that you are specifying a field with for %s with fscanf so as to avoid buffer overflow. For example:
    Code:
    int encoding = 0; //variable to represent whats in the room 0-8 (9 different items)
    char item[ITEM_MAX_LEN + 1];// item
    char itemArr[NUM_ROOMS][ITEM_MAX_LEN + 1]; //array of items in the room
    int numItems[NUM_ROOMS]; //number of item in each room
    
    /* format not expected to exceed field width of 5 digits, e.g., "%*i %10000s" */
    char format[12];
    sprintf(format, "%%*i %%%ds", ITEM_MAX_LEN);
    while (encoding < NUM_ROOMS && fscanf(input, format, item) == 1){
        if (strlen(item) >= ITEM_MIN_LEN){
            strcpy(itemArr[encoding], item);
            printf("%s\n", itemArr[encoding]);
            encoding++;
        }
        encoding++;
        while((le = fgetc(input)) != '\n' && le != EOF);
    }
    if (encoding > 0){
        printf("%s\n", itemArr[0]);
    }
    By the way, why do you increment encoding twice when the item string length is greater than the minimum length? This could be a problem where say, the first line of input is too short, upon which itemArr[0] is left uninitialised, but you have no way to tell. Notice that I added a check for encoding > 0 because it is also possible that the first line of input is simply invalid, upon which the loop will terminate before having given itemArr[0] an initial value. It is also possible that there are more lines of valid input with strings within range than there are rooms, hence I added a check for encoding < NUM_ROOMS into the loop condition.
    Last edited by laserlight; 07-14-2016 at 12:23 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc printf with strange characters....
    By Gil Carvalho in forum C Programming
    Replies: 5
    Last Post: 06-18-2012, 02:09 PM
  2. Strange Output
    By jokes_finder in forum C Programming
    Replies: 1
    Last Post: 05-13-2011, 07:44 AM
  3. Strange characters in output to terminal
    By hellogamesmaste in forum C Programming
    Replies: 4
    Last Post: 08-30-2009, 10:25 AM
  4. Strange characters in output Need Help ASAP
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2003, 06:35 PM
  5. Strange Characters
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 08-26-2002, 11:01 PM

Tags for this Thread