Hello all

As the title suggests, I have tried to write a sorting program in C++

The code compiles right, but does print the sorted numbers, but rather prints a list of zeros. I have tried to find the cause of this error for a while, but I can't seem to find it.

Any help would be greatly appreciated!

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    //almost all variable declarations, except for two arrays
    int a = 0;
    string b("");
    string temp("");
    int prev = 0;
    int curr = 0;
    int * ptrinput = 0;

    cout << "How many numbers to sort? ";
    getline(cin,b);
    a = atoi(b.c_str());

    //the two arrays, input[] for holding unsorted input, and sorted[] for holding sorted integers
    int input[a];
    int sorted[a];

    //recieves input for the input[] array
    for(int x = 0; x<a; x++)
    {
        cout << "Enter input for " << x << ": ";
        getline (cin,temp);
        input[x] = atoi(temp.c_str());
    }
    //main logic loop
    for(int x = 0; x<a; x++)
    {
        for(int y = 0; y<a; y++)
        {
            if(input[y] != -1)
            {
                prev = input[y];
                ptrinput = &input[y];
            }
            if((input[y] != -1)&&(&input[y] != ptrinput))
            {
                curr = input[y];
            }
            if(prev < curr)
            {
                sorted[x] = prev;
                input[y] = NULL;
                break;
            }
            if(curr < prev)
            {
                sorted[x] = curr;
                input[y] = NULL;
                break;
            }
            if(prev == curr)
            {
                sorted[x] = prev;
                sorted[x+1] = curr;
                input[y] = NULL;
                input[y+1] = NULL;
                x = x + 1;
                break;
            }
            else {}
        }
    }
    //final result
    cout << endl << endl << "Final Result:" << endl << "----------------------" << endl;
    for (int x = 0; x < a; x++)
    {
        cout << sorted[x] << endl;
    }
    return 0;
}

Perhaps you should explain the sorting algorithm you had in mind, because it isn't really obvious.

In the meantime, I have lots of comments about the code:
Line 7-9: Make sure to use descriptive variable names. A proper name for "a" could be arraySize or numberCount. b and temp aren't needed, because...
Line 15-16, 26-27: ...you can input a number directly without using a string (cin >> a)
Line 8-9; No need to initialize string explicitly with an empty string literal, string b; does the trick
Line 19-20: The array size must be a constant known at compile-time, so this is not valid standard C++. Replace this by vector<int> input(a); (same for sorted) - you need to include the vector header for this.

In various lines: don't use NULL for integrals, that's confusing. NULL is just a define for 0 - and if you use NULL at all, it should only be used to indicate a null pointer.

Thanks for the reply!
The algorithm I tried to implement was the selection sort.
I needed the strings b and temp, because I used getline() in place of cin >>, and getline() only accepts strings.
Thanks for all the tips!

Hm, sure doesn't look much like selection sort...
I particularly don't get what the checks for -1 are supposed to do and what ptrinput is for.

Selection sort can be done simpler and in-place (i.e. without an extra array):

#include <iostream>
#include <vector>
using namespace std;

int main()
{
  int numberCount;
  cout << "How many numbers to sort? ";
  cin >> numberCount;

  vector<int> input(numberCount);
  for (int i=0;i<numberCount;i++)
  {
    cout << "Enter number #" << i+1 << ": ";
    cin >> input[i];
  }

  for (int i=0;i<numberCount;i++)
  {
    int bestIndex=i;
    for (int j=i+1;j<numberCount;j++)
    {
      if (input[j]<input[bestIndex])bestIndex=j;
    }
    swap(input[i],input[bestIndex]);
  }

  cout << endl << endl << "Final result:" << endl << "----------------------" << endl;
  for (int i=0;i<numberCount;i++)cout << input[i] << endl;
}
Comments
+1
This question has already been answered. Start a new discussion instead.