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