What I have to do:
I have to read numbers from a file input from the keyboard into an array, sort the numbers, and write the sorted numbers to a file.

My problems are twofold:

First, I don't know how big to make the array. When I try to write the sorted numbers I can't get the junk numbers in my oversized array out of the way. I think that if I used a vector it would solve the problem, but I was specifically instructed to use a one dimensional array.

Second, I have to open the output file in function main, but I need to have at least four functions, and that entails writing to my output file in a separate function. I can get my compiler to okay the code, but it doesn't actually write to the file.

Assistance would be greatly appreciated. Thanks!

Dani AI

Generated

— the “junk” comes from processing the whole array instead of only the elements you actually read. is right that a vector makes this trivial, but if your instructor requires a one‑dimensional array, use a small, reliable pattern: (1) do a quick first pass to count values, (2) allocate exactly that many elements, (3) rewind and fill the array, (4) sort and write. Pass the array pointer and the element count to every function so nothing uses uninitialized slots.

A compact example (reads whitespace‑separated numbers, sorts, writes):

#include <fstream>
#include <algorithm>

void writeSorted(std::ofstream& out, const double* a, size_t n) {
    for (size_t i = 0; i < n; ++i) out << a[i] << '\n';
}

int main() {
    std::ifstream in("input.txt");
    if (!in) return 1;

    double tmp; size_t count = 0;
    while (in >> tmp) ++count;

    double* arr = new double[count];        // allocate exact size
    in.clear(); in.seekg(0);
    for (size_t i = 0; i < count && (in >> arr[i]); ++i) {}

    std::sort(arr, arr + count);
    std::ofstream out("output.txt");
    if (!out) { delete[] arr; return 1; }
    writeSorted(out, arr, count);

    delete[] arr;
}

Troubleshooting tips: always check streams with if (!in) / if (!out). If you open the output file in main but write in another function, pass the std::ofstream& (streams are not copyable). If you read the file twice without reopening, in.clear(); in.seekg(0); is required after the first pass. Finally, never loop to the array’s declared capacity — loop to the counted n to avoid printing garbage.

Recommended Answers

All 3 Replies

1. How is the data arranged in your file? Please provide an example if that's not too much to ask.

2. A vector object can handle a file of infinite size and should be the container of choice for this project. Tell your instructor that a vector is an array.

3. Push your file into your vector array.

4. Sort your array as desired.

5. Create/open a new file to write to.

6. Write your vector to the new file.


You state that you are having difficulty writing to a file. It's as simple as creating on ofstream object, calling the open( ) function, and supplying a path/name/extention. Using the same ofstream object, use the << insertion operator to send data to the file. It's just like a cout << operation, just using a different stream.

do you pass vectors like you pass arrays?

if you want to pass a vector into a function, just prototype the parameter list just like you would any other data type; 'type' then 'var name':

//Pass by reference in order to directly modify the vector
void bubble_sort(vector<string>& file)
{
     bool is_swap = true;
     
     for(int i=0, size=file.size(); i<size && is_swap; i++)
     {
          is_swap = false;         

          for(int j=0; j<size-1; j++)
          {
               if(file[j] > file[j+1])
               {
                     swap(file[j], file[j+1]);
                     bool is_swap = true;
               }     
          }
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.