Search code examples
c++text-filesbubble-sort

How to adjust this bubble sort


The issue at the moment is this program only works by typing in the name of the text file.

I've been told to shorten it to where it just opens up the text file right off the bat and performs the bubble sort along with it (which it does in its current state).

Example of text file:

-14, -5, 7, 1, 7, 71, -3, 59 [bubble short] -14, -5, -3, 1, 7, 7, 59, 71

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;

void bubbsort(int arr[]);

int main()
{

    string file;

    const int a = 100, b = 10, c = 10;

    int count = 0, count1 = 0, d = 0, swap = 0;

    int clam[a] = { 0 }, ray[b][c] = { 0 };

    cout << "Type name of the file: " << endl;
    cin >> file;

    ifstream data;
    data.open(file);

    vector<int> array;

    int number;

    while (data >> number);
    {
        array.push_back(number);
        count++;
        d = count;
        clam[count];
    }

    data.close();
    data.open(file);

    while (data.good())
    {
        int i;
        for (i = 0; i<d; i++)
        {
            data >> clam[i];
            cout << clam[i] << " ";
            count1++;
        }
    }
    cout << endl << "There are " << d << " integers within " << ' " ' << file << '"' << " file!" << endl;

    data.close();


    for (int k = 0; k <= count - 1; k++)
    {
        for (int l = k + 1; l <= count - 1; l++)
        {
            int temp = 0;
            if (clam[k]>clam[l]){
                temp = clam[k];
                clam[k] = clam[l];
                clam[l] = temp;
                swap++;
            }
        }

    }

    cout << endl << "Sorting this " << swap << " # of swaps" << endl;

    data.close();

    cout << endl;

    for (int y = 0; y<count; y++)
    {
        for (int z = 0; z <= 9; z++)
        {
            if (y != count)
            {
                cout << right << setw(4) << clam[y];

                y++;
            }
            else 
            {
                cout << endl;

                system("pause");
                return 0;
            }
        }
        y = y - 1;
        cout << endl;

    }

    system("pause");

    return 0;
}

Renaming the infile function didn't seem to do the trick, I'm not sure on how to tweak this.


Solution

  • Simply get rid of:

    cout << "Type name of the file: " << endl;
    cin >> file;
    

    and change your definition of file to:

    string file = "filename.txt";