0

I would like to make program that uses bubble sorting algorithm to sort some data from input program and write sorted values into output file - however everything seems fine, but the output file is empty...

I've tried to use a console as output but it didn't help. I know that there are functions for bubble sorting, but I'm quiet new in c++ topic and would like to gain some experience in it.

#include <iostream>
#include <fstream>
#include <string>

struct Datas
{
    std::string day;
    float value;
};
using namespace std;

int main ()
{
    fstream file_in, file_out;
    string name, line, data;
    Datas current [7];

    cout << "Input file name with data to open" << endl;
    cin>>name;
    file_in.open(name.c_str());

    if (file_in.fail())
    {
        cout << "Unable to open file - try again" << endl;
        //  pause(TIME);
        system ("CLS"); //Clears the console
        main();
    } else {


        cout << "Input output file name to save sort datas" << endl;
        cin>>name;
        file_out.open(name.c_str());

        if (file_out.fail())
        {
            cout << "Unable to open file - try again" << endl;
        } else {

            while (file_in.eof() == 0)
            {
                for (int pointer = 0; pointer < 7; pointer++)
                {
                    file_in >> current[pointer].day >> current[pointer].value;
                }

                string tmp_day;
                float tmp_val;

                for (int current_val = 0; current_val < 7; current_val++)
                {
                    if (current[current_val].value > current[current_val+1].value ||
                        current[current_val].value > current[current_val+2].value ||
                        current[current_val].value > current[current_val+3].value ||
                        current[current_val].value > current[current_val+4].value ||
                        current[current_val].value > current[current_val+5].value ||
                        current[current_val].value > current[current_val+6].value )
                    {
                        tmp_day = current[current_val+1].day;
                        tmp_val = current[current_val+1].value;

                        current[current_val].day = tmp_day;
                        current[current_val].value = tmp_val;

                        tmp_day = current[current_val].day;
                        tmp_val = current[current_val].value;
                    }
                }

                for (int stream = 0; stream < 7; stream++)
                {
                    file_out << current[stream].day << '\t' << current[stream].value << endl;
                }
            }

            file_in.close();
            file_out.close();
            return 0;
            system("PAUSE");    //Keeps console window open after program execution
        }
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Maciarena
  • 9
  • 3
  • 1
    `if (current[current_val].value > current[current_val+1].value || current[current_val].value > current[current_val+2].value || current[current_val].value > current[current_val+3].value || current[current_val].value > current[current_val+4].value ||current[current_val].value > current[current_val+5].value || current[current_val].value > current[current_val+6].value )` -- Explain what you're testing here. What if there were 100 values? Surely you wouldn't write 100 lines of code. – PaulMcKenzie Dec 10 '19 at 21:39
  • 1
    As far as I know: calling `main`, like you do in your `if (file_in.fail())`, is undefined behavior – Algirdas Preidžius Dec 10 '19 at 21:47
  • @AlgirdasPreidžius [You know rightly.](http://eel.is/c++draft/basic#start.main-3) – user4581301 Dec 10 '19 at 21:54
  • 1
    Recommended reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Dec 10 '19 at 21:58
  • Unrelated: `system("PAUSE"); //Keeps console window open after program execution` The comment here is not quite true. It prevents the program from ending until the PAUSE program exits. More interesting, it sits after `return 0;` and will never run. The function exits as soon as it returns, so code after `return` is never reached. – user4581301 Dec 10 '19 at 22:01
  • The programmer's secret weapon is the debugger and learning to use debuggers is almost mandatory. The debugger allows you to control the rate of execution of the code, allowing you to see what the program does as it does it. "Stepping" the program line by line through code that is giving you trouble is one of the fastest ways to see what is wrong with a program. You advance a line and make sure all of your expectations were met. If the program did something different, you've just found a bug. – user4581301 Dec 10 '19 at 22:05

0 Answers0