Reading a list of numbers and sorting C ++

I am 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 get

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

I'm new to programming and this is my first C ++ experience. Can someone tell me how to write a 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;
}
+3
source share
4 answers

Error line:

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

I'm not quite sure what you are trying to do here, it looks like you just want to print it, and in this case numbers =it is not required.

. : while (!inputFile.eof()) ++ , . . ( ).

, 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;
}

std::vector " ?". ( ).

+6

, :

  • ANYSIZE_ARRAY 5
  • , while :

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

, , 5 , . 5, , , , 5 . , 5 .

, std::vector<long> . , push_back() . , . 5 size().

, :

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

++, , stdout ( - , ), "". (, ostream int [1]).

+3

-, outstream int. -, n - , - . -. -, . -, sort() STL .

:

#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;
}
+3

, char, int, , atoi().

 int myint = atoi("12345");

will give myint a value of 12345.

-2
source

All Articles