Array[] garbage output

This is a discussion on Array[] garbage output within the C++ Programming forums, part of the General Programming Boards category; Could anyone tell me why I might get output such as this printed from a char array[60]; Original Line: 10 ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    Array[] garbage output

    Could anyone tell me why I might get output such as this printed from a char array[60];

    Original Line: 10 10 10 20 22 34
    @AZ: AA AA AA BA BB BB
    @A
    15 231 -12 354 7 44 19 22 11 -6 9 5 44 -3
    Mean = 52 Count = 14

    11 12 58 43 29 16 288 25 999 124 98
    Mean = 154 Count = 11

    Original Line: 22 31 16 7 88 1297 41
    Scrambled Line: BB BA AC C DD ABDC BAW
    @AZ
    @A


    Where is the garbage coming from? The output is correct, however I get all of the extra garbage?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Maybe random values in memory. Try setting it to 0 initially.

    Code:
    char array[60] = { 0 };

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    bingo!! I knew it was garbage memory, however I wasnt sure how to get rid of it :-),

    Thanks,

    although now I have one other issue, the output is perfect except that I get one extra output of blank characters:

    Code:
    Original Line:  10 10 10 20 22 34
     Scrambled Line:  AA AA AA BA BB BB
     15 231 -12 354 7 44 19 22 11 -6 9 5 44 -3 
     Mean = 52  Count = 14
    
     11 12 58 43 29 16 288 25 999 124 98 
     Mean = 154  Count = 11
    
     Original Line:  22 31 16 7 88 1297 41
     Scrambled Line:  BB BA AC C DD ABDC BA
    
     Original Line:           //This is the output I dont want
     Scrambled Line: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
    Here is the function that is doing the work:

    Code:
    void Scramble( istream & inf, ofstream & outf )
    {
            char x;
            int j = 0;
            char aLine[60]={0};
            //inf.getline(aLine, 60); 
            cout << " Original Line: ";
            outf << " Original Line: ";
            while(inf.peek() != '\n')
            {
                    inf.get(x);
                    aLine[j] = x;
                    cout << aLine[j];
                    outf << aLine[j];
                    j++;
            }
            cout << endl;
            outf << endl;
    
            outf << " Scrambled Line: ";
            cout << " Scrambled Line: ";
            for(int i = 0; i <= 59; i++ )
            {
                    if( aLine[i] == '0' || aLine[i] == '1')
                            aLine[i] = 'A';
                    if( aLine[i] == '2' || aLine[i] == '3' || aLine[i] == '4')
                            aLine[i] = 'B';
                    if( aLine[i] == '5' || aLine[i] == '6' || aLine[i] == '7')
                            aLine[i] = 'C';
                    if( aLine[i] == '8' || aLine[i] == '9')
                            aLine[i] = 'D';
                            cout << aLine[i];
                            outf << aLine[i];
            }
            cout << endl;
            outf << endl;
    }
    
    int main ()
    {
            char x;
            ifstream infile("lines.txt");
            ofstream data("data.txt");
            while(!infile.eof())
            {
                    infile >> x;
                    if( x == 'S' )
                            Scramble( infile, data);
                    if( x == 'C' )
                            CFunc( infile, data );
            }
    data.close();
            infile.close();
            return (0);
    }
    Now I cant determine where the extra line of blank output is coming from at the end?
    Not to mention all the B's.

    any ideas anyone?

    thanx.
    Last edited by guda; 11-11-2002 at 10:16 PM.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    I agree with setting a value to the array initially but how you did it doesnt work

    Code:
    char array[1] = { 0 };
    would be fine, but the rest of those 59 values in

    Code:
    char array[60] = { 0 };
    Would be "jumbled" because its getting recently used memory. To initialize the array do something like

    Code:
    char array[60];
    
    for(int i = 0; i < 60; i++)
    array[i] = 'B'; // this will assign all the different values in the array //to the letter 'B'

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    Would this be a problem in int main() or would this be an actual issue with the function?

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    This fixed it:

    for( int i = 0; i <= 60 && inf.peek() != '\n'; i++ )

    Thanks everyone ! .... this board rocks.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by D-Man
    I agree with setting a value to the array initially but how you did it doesnt work

    Code:
    char array[1] = { 0 };
    would be fine, but the rest of those 59 values in

    Code:
    char array[60] = { 0 };
    Would be "jumbled" because its getting recently used memory. To initialize the array do something like

    Code:
    char array[60];
    
    for(int i = 0; i < 60; i++)
    array[i] = 'B'; // this will assign all the different values in the array //to the letter 'B'
    What are you talking about.

    char array[60] = { 0 };

    This will initialize the entire array to 0. This is exactly the same as using memset or ZeroMemory except that it is FASTER.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    That only initializes the first char in the array, at least thats what my instructor has told me. If im wrong i appologize.
    Last edited by D-Man; 11-12-2002 at 11:00 AM.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by D-Man
    That only initializes the first char in the array, at least thats what my instructor has told me. If im wrong i appologize.
    Your instructor is wrong....tell him to try reading The C++ Programming Languge(3rd Ed) 5.2.1 - Array initialisation

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    I probley just mis interprited what he had said. Live and learn.
    Last edited by D-Man; 11-12-2002 at 11:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21