-4

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.

  • Will you be using multiple text-files(different file names)? The instruction to "open it right off the bat" indicates not? – HvS Jul 25 '14 at 09:57
  • Look, there aren't many ways around this. If the file name is fixed, hard-code it. The only other option that would avoid you having to enter the file-name at some stage is to modify the application to read one(or more) files(with certain extension) within the applications' path(which would necessitate some constraints). – HvS Jul 25 '14 at 10:06

2 Answers2

0

Simply get rid of:

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

and change your definition of file to:

string file = "filename.txt";
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

For the sake of trying to avoid a hard-coded file-name:

Modify the application to read your files(which have defined extension(s)) from the application path.

Get the application path(number of ways exist): See How to get program path.

Iterate through the application path and look for files which have your chosen extension(s). Have a look at: Boost Filesystem.

Now you should have your file-name(s).

Constraint: You can't have 'garbage' files in the application path which have the same extension as your bubble-sort files.

If this constraint is not acceptable, you can consider 'tagging' your files with data(at the start of the file). Then you can first check for this 'tag' before proceeding with the bubble-sort.

Community
  • 1
  • 1
HvS
  • 504
  • 1
  • 14
  • 22