String array outputting gibberish

This is a discussion on String array outputting gibberish within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm in my first semester of C++ programming and am working on a simple program to load words ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Angry String array outputting gibberish

    Hey all,
    I'm in my first semester of C++ programming and am working on a simple program to load words from an input file into a string array, sort that array, and then print out the array. I have the code written, which compiles and works perfectly save for one problem: no matter what I've tried the first index of the array prints out gibberish. I'm lost at this point, so I figured I'd try asking for some help. Here's the code I have written:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void sortArray(string[], int);
    void printArray (string[], int);
    int loadArray (string[], ifstream&);
    
    const int SIZE = 50;
    
    int main() {
    
        ifstream inFile1;
        inFile1.open("words1.txt");
        ifstream inFile2;
        inFile2.open("words2.txt");
        
        string words1[50], words2[50];
        int count1, count2;
    
        count1 = loadArray(words1, inFile1);
        cout << "Array 1, unsorted:" << endl;
        printArray(words1, count1);
        cout << endl << endl;
    
        count2 = loadArray(words2, inFile2);
        cout << "Array 2, unsorted:" << endl;
        printArray(words2, count2);
        cout << endl << endl;
    
        sortArray(words1, count1);
        cout << "Array 1, sorted:" << endl;
        printArray(words1, count1);
        cout << endl << endl;
    
        sortArray(words2, count2);
        cout << "Array 2, sorted:" << endl;
        printArray(words2, count2);
        cout << endl << endl;
    
    
    }
    // loadArray places value into array a from an input file.
    // preconditions: input file has been succesfully opened.
    // postconditions: array a is filled with i values. i values is returned.
    int loadArray (string a[], ifstream& infile) {
        int i = 0;
        while (i < SIZE && infile >> a[i]) {
            i++;
        }    
        return i;
    }
    
    // printArray prints the values in a.
    // preconditions: array hold size values.
    // postconditions: prints array values, label starting at 1.
    void printArray (string a[], int count) {
        int i;
        for (i = 0; i < count; i++) {
            cout << left;
            cout << setw(2) << i + 1 << "    " << setw(10) << a[i] << endl;
        }
    }
    // sortArray will put the array values into ascending order, using
    // the selection sort algorithm.
    // preconditions: array must be filled with size values.
    // postcondition: array values are in ascending order.
    void sortArray(string a[], int size) {
        int i, j, min; // these hold index values to be sorted.
        string temp; // must be of same type as array elements.
        for (i = 0; i < size - 1; i++) {
            min = i;
            for (j = i + 1; j < size; j++) { // find the index of smallest value.
                if (a[j] < a[min])
                    min = j;
    
                if (min != i) { // swap value at a[min] with "top" value, i.
                    temp = a[min];
                    a[min] = a[i];
                    a[i] = temp;
                }
            }
        }
    }
    The input files contain all single words, separated by a newline.
    No matter what I've tried, the first index (no matter if it's 0, 1, 2... etc) prints out garbage characters followed directly by the first word in the file.
    How or what can I do to remove the "garbage" that's being put in the array?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    So, you're saying that right after you call loadArray, the printArray call "prints out gibberish" for the first entry? This is even before you call sortArray?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Yes, the sortArray function isn't causing any issue. I'm almost certain it's being caused from loadArray, but it's so simple that I have no idea why. When I call printArray, it's printing out three characters prior to the first word in the first index of the string: 
    I have no idea why they are being printed, or stored in a[0] in the first place...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Hmm... check the file. Does it contain exactly what you expect it to? If it does, check the file encoding. Is there a byte order mark?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    The input files contain one word per line, and contain no more than 50 words total. They're files provided by my instructor for us to use for this program.
    Also no, there is no BOM to my knowledge.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Right. Your implementation of loadArray and printArray looks like it should work, so I did a quick test and I must say that I cannot reproduce your problem. What exactly do you mean by "prints out gibberish"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    OK, so since you mentioned the files, and I wasn't really thinking about them being the problem, I deleted the files my instructor sent to me and retyped all the words into a new file... and viola! It works perfectly. I'm guessing the files must have somehow been corrupted, even though there weren't any extra characters in the file itself. Thank you for the responses nonetheless, but I guess having a second pair of eyes always helps!

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    I'd say they likely contained a byte order mark then. Editors normally detect that and don't show it, so you would normally only see it if you opened it in a hex editor.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. outputting an array
    By slee8251 in forum C Programming
    Replies: 2
    Last Post: 04-17-2012, 08:46 PM
  2. Array not outputting properly
    By NinjaFish in forum C Programming
    Replies: 9
    Last Post: 04-16-2011, 11:51 PM
  3. help simple string outputting
    By kingdm in forum C Programming
    Replies: 10
    Last Post: 11-08-2009, 12:15 PM
  4. Outputting a string to the screen
    By Soopafly in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2002, 02:11 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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