0

I'm trying to read some characters from a file, sort them in an array and print them to a new file. The problem I have is nothing is happening. I've been trying to figure it out for a couple of days and I simply cannot come up with a solution or find one since this is how it's done everywhere but for some reason I cannot get it to work.

int main() {
    int i = 0, sortType;
    char c[100];
    cout << "Input the type of sorting you want...\nBubble sort: Press 1 \nSelection sort: Press 2 \nInsertion sort: Press 3 \nQuick sort: Press 4\n\n";
    cin >> sortType;


    ifstream inFile("AllAlpha.txt");
    if (!inFile) {
        cout << "\nFile is unavailible";
        return 0;
    }

    for (;; i++) {
        if (!inFile.eof())
            break;
        inFile.get(c[i]);
    }

    switch (sortType) {
    case 1: bubbleSort(c, i);
    case 2: selectionSort(c, i);
    case 3: insertionSort(c, i);
    case 4: quickSort(c, 0, i - 1);
    }

    ofstream outFile("output.txt");
    if (!outFile) {
        cout << "\nFile is unavailible";
        return 0;
    }


    for (int j = 0; j < i; j++) {
        outFile << c[j] << " ";
    }

    inFile.close();
    outFile.close();
}
Random Davis
  • 4,358
  • 3
  • 11
  • 21
Dragan
  • 1
  • 3
  • 1
    Have you tried with a debugger? – Ceros Oct 31 '16 at 17:28
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 31 '16 at 17:29
  • 3
    What do you think `if (!inFile.eof()) break;` means? – molbdnilo Oct 31 '16 at 17:29
  • Wow @molbdnilo you actually solved my headaches. The point of that snippet is to break if EoF is reached, but i made a stupid mistake. Now i have another problem, the output is adding "M\n\n" before the rest of the array is outputted to the file. – Dragan Oct 31 '16 at 17:46
  • You should probably read [this](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), too. – molbdnilo Oct 31 '16 at 18:11
  • @Dragan If you have another problem, you should post that as a separate question, and only after you've used a debugger to try and track down the issue. And, as always, make sure to provide only a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). For more info on debugging, see [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Random Davis Oct 31 '16 at 18:23

0 Answers0