-1

Here's the lab objective: Write a C++ program that retrieves all the numbers stored in a data file. As each number is retrieved, it is displayed on the screen. After the end of file is reached, the numbers should be sorted into numerical order and then the the result should be displayed.

Here's my code:

int main(){
    fstream infile;
    int numbers[25], size = 0, i = 0;

    infile.open("lab1.txt");
    if (infile.fail())
    {
        cout << "Error Opening File" << endl;
    }
    while (!infile.eof())
    {
        infile >> numbers[i];
        cout << numbers[i] << endl;
        i++;
    }


    size = i;
    cout << size << " number of values in file" << endl;

    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size - i - 1; ++j) {
            if (numbers[j] > numbers[j + 1])
             {
                int temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
             }
        }
    }

    infile.close();

    return 0;

}

It keeps resulting in an infinite loop. Does anyone know why? I appreciate any help thank you

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
CPP Noob
  • 1
  • 2
  • Ever heard of [std::sort](http://en.cppreference.com/w/cpp/algorithm/sort)? – Jesper Juhl Mar 12 '17 at 19:54
  • @JesperJuhl We weren't taught that in class yet, my professor wants us to sort using actual loops and logic by ourselves right now :\ thank you though – CPP Noob Mar 12 '17 at 19:56
  • In case the file is failed to open, you just print a message and keep going. – Shibli Mar 12 '17 at 19:58
  • So what else were you not taught in class that you can't use? The answer for C++ is to use `std::sort`. Anyway, 1) [while (!infile.eof()) is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), 2) You never checked if there are more than 25 numbers in the file. – PaulMcKenzie Mar 12 '17 at 20:01
  • @PaulMcKenzie why is the while statement wrong? And yeah I actually realized that I think I should make the array size after I find out the size of the file – CPP Noob Mar 12 '17 at 20:24
  • @CPPNoob -- Go to the link I have in my comments. Second, you never stated which loop is infinite. Is it the loop where you're reading in data, or the loop where you're trying to sort? – PaulMcKenzie Mar 12 '17 at 21:02
  • @PaulMcKenzie Oh I didn't realize it was a link, thank you I will read that. ANd I believe the first loop is infinite because my output displays a bunch of numbers constantly then crashes – CPP Noob Mar 12 '17 at 21:23

2 Answers2

0

Remove the i variable from declaration. Use just the size variable.

int main(){
    fstream infile;
    int numbers[25], size = 0;

    infile.open("lab1.txt");
    if (infile.fail())
    {
       cout << "Error Opening File" << endl;
    }
    while (!infile.eof())
    {
        infile >> numbers[size];
        cout << numbers[size] << endl;
        size++;
     }

    cout << size << " number of values in file" << endl;

    for (int i = 0; i < size - 1; ++i) {
        for (int j = i+1; j < size; ++j) {
            if (numbers[i] > numbers[j])
            {
              //.....
user3811082
  • 198
  • 1
  • 7
0

try

char x;
while (infile.get(x)){
    //add numbers to array
}
Andre V
  • 13
  • 4