3

I'm trying to read a list of numbers from a file and sort them by reading them into an array and then sorting the contents of the array. But I'm getting

error:incompatible types in assignment of 'std::basic_ostream<char, std::char_traits<char> >' to 'int [1]' 

I'm fairly new to programming and this is my first time working with C++ Can anyone tell me how to write the list of numbers to an array so that I can sort them? Here is what I have:

#include <fstream>
#include <iostream>
#include <iomanip>
#define ANYSIZE_ARRAY 1
using std::cout;
using std::endl;


int main()
{
  const char* filename = "test.txt";
  std::ifstream inputFile(filename);
  int numbers[ANYSIZE_ARRAY];
  int i, key;

  // Make sure the file exists
  if(!inputFile)
  {
    cout << endl << "The File is corrupt or does not exist. " << filename;
    return 1;
  }

  long n = 0;
  while(!inputFile.eof())
  {
    inputFile >> n;
    numbers = cout << std::setw(10) << n;
  }

  for(int j=1;j<5;j++)
    {
        i=j-1;
        key=numbers[j];
        while(i>=0 && numbers[i]>key)
        {
                    numbers[i+1]=numbers[i];
                    i--;
        }
        numbers[i+1]=key;
    }

    //Display sorted array
    cout<<endl<<"Sorted Array\t";
     for(i=0;i<5;i++)
       cout<<numbers[i]<<"\t";
    cout<<endl;
}
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Jmh2013
  • 2,321
  • 2
  • 22
  • 36
  • 1
    Your array `numbers` is too small for the number of rows you're trying to read into it. The `1` in `int[1]` in the error message let's us know that there's a problem in the `for` loop at the end where you're trying to output 5 values from a 1-value array. It would be worth printing the unsorted array before you sort it. And ultimately, you will want to use the standard `sort` template function. And maybe a vector of `int` to dynamically allocate entries as you go. – Jonathan Leffler May 20 '12 at 21:00
  • How many numbers are in the file? Is it a constant amount? – Ove May 20 '12 at 21:00
  • @Ove currently there are 20 numbers in the file but I would like it to take a variable so that it does not have to be constant. – Jmh2013 May 20 '12 at 23:08
  • 2
    @JonathanLeffler I think I am going to go with using the sort function and vectors. After looking at the answer given by awoodland using vectors and doing some research they seem to be much easier use for my needs. Thanks for the info. I did get it to work my initial way too. I'm slowly starting to learn C++ – Jmh2013 May 21 '12 at 00:05

3 Answers3

6

The line causing the error is:

numbers = cout << std::setw(10) << n;

I'm not quite sure what you're trying to do here, it looks like you just want to print it in which case the numbers = isn't needed.

The structure of your loop to read all the data is problematic also. The line: while (!inputFile.eof()) isn't idiomatic C++ and won't do what you hope. See here for a discussion on that issue (and here).

For reference you can do this quite simply with less work by using std::sort

#include <iterator>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>

int main() {
  std::ifstream in("test.txt");
  // Skip checking it

  std::vector<int> numbers;

  // Read all the ints from in:
  std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(),
            std::back_inserter(numbers));

  // Sort the vector:
  std::sort(numbers.begin(), numbers.end());

  // Print the vector with tab separators: 
  std::copy(numbers.begin(), numbers.end(), 
            std::ostream_iterator<int>(std::cout, "\t"));
  std::cout << std::endl;
}

This program also uses a std::vector instead of an array to abstract the "how big should my array be?" problem (which your example looked to have a possible problem problem with).

Community
  • 1
  • 1
Flexo
  • 82,006
  • 22
  • 174
  • 256
  • 1
    wow this is amazing! I had to look up a few things to understand how this worked but this seems like its alot easier than what I was trying to do. – Jmh2013 May 20 '12 at 23:27
3

Here is what you need to do to get your program to work:

  • Change ANYSIZE_ARRAY to 5
  • Where you read the numbers, replace the while with this:

    long n = 0;
    i=0;
    while(!inputFile.eof())
    {
            inputFile >> n;
            cout << std::setw(10) << n;
            numbers[i]=n;
            i++;
    }
    

This way, you will create an array which can hold 5 numbers, and you will read the numbers into that array. I used 5 because I saw that you were using the same number in the sorting algorithm, so I assumed that you only have to read 5 numbers. This is a bad thing, because your program will only work with 5 numbers.

If you need it to work with a variable amount of numbers, you can use a std::vector<long> to store the numbers. You can create a vector, and then use push_back() to add numbers to it. The vector will resize automatically and will hold as many numbers as you put in it. Then you can replace the 5s in your code with the vector's size() method.

You were getting your original error because this line doesn't make sense:

numbers = cout << std::setw(10) << n;

C++ views that line as if you are trying to print out the number to the stdout stream (which is in our case the console, the screen), and then assigning that stream to the "numbers" array. You cannot assign an output stream to an integer array (hence the error cannot convert from ostream to int[1]).

Ove
  • 5,917
  • 2
  • 37
  • 63
  • Thanks! I need to mess with my compiler I think because after making the changes you suggested and build and run I get "Process terminated with status 255" It goes through about halfway so it works, just doesn't finish. – Jmh2013 May 20 '12 at 23:50
3

First, you should not assign an outstream variable to an int. Second, n is long type and numbers is an integer array. There is type-unsafety. Third, you should pre-allocate array size. Forth, you can directly use sort() in STL function to do the sorting.

Try the following code:

#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

#define ANYSIZE_ARRAY 5

int main()
{
const char* filename = "test.txt";
std::ifstream inputFile(filename);
int numbers[ANYSIZE_ARRAY];
int i, key;

// Make sure the file exists
if(!inputFile)
{
    cout << endl << "The File is corrupt or does not exist. " << filename;
    return 1;
}

int n = 0;
i = 0;
while(!inputFile.eof())
{
    inputFile >> n;
    cout << std::setw(10) << n;
    numbers[i++] = n;
}

sort(numbers, numbers + ANYSIZE_ARRAY);


//Display sorted array
cout<<endl<<"Sorted Array\t";
for(i=0; i<ANYSIZE_ARRAY; i++)
    cout<<numbers[i]<<"\t";

cout<<endl;
}
reconditesea
  • 150
  • 5
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Flexo May 20 '12 at 21:40
  • Thanks for the response! This works but like awoodland shows, while(!inputFile.eof()) is not idiomatic C++ So I probably need to use another way of doing that. – Jmh2013 May 20 '12 at 23:57