hello , i want to cin n numbers then to sort the positive numbers in one array, the negative numbers in another array , then to print positive array. So let me explain , i run the program , i enter the 9 , then enter 1 2 3 4 5 6 7 8 -1 , after this it couts me the Positive array , but at the end it prints me some numbers , i have no clue where they came from . I know that there is another way to do that , but i want only with arrays. Help me pls)

``````#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

int main() {
int nNumber;
cout<<"Enter a Number"<<endl;
cin>>nNumber;
int nPozitive[nNumber] , nNegative[nNumber] , nTotal[nNumber];
cout<<"Enter "<<" "<<nNumber <<" "<<"Numbers" <<endl;
for (int i = 0; i<nNumber; i++)
{
cin>>nTotal[i];

}
for (int j = 0; j < nNumber; j++) {
if (nTotal[j]>0) {
nPozitive[j] = nTotal[j];
}
else
{
nNegative[j] = nTotal[j];
}
}
for (int k = 0 ; k<nNumber; k++) {

cout<<nPozitive[k]<<endl;

}

getch();
return 0;

}``````

In this example, you only put a few even numbers into the new array, but you're printing a TOTAL amount of numbers not just the even ones. When your nPozitive is delcared on the stack (and not initialized), it can have ANY values in it.

And what compiler are you using exactly. I have not long started learning C++, but I did not think variable length arrays were valid C++?

yes , and that is the difficulty i meet . How to print just the "filled places" from the array? is there a way to do that? or what must i do to get the program working good?

p.s MingW GCC 4.6.1 32-bit

You also will need to call "new" on the array for the total.

@thines01 , can you rewrite the code? so i can see where i made mistakes.. thanks.

I would start out like this:

``````#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int nNumber = 0;
cout<<"Enter an amount of numbers: ";
cin>>nNumber;

int* nTotal = new int(nNumber);
int* nPositive = new int(nNumber);
int* nNegative = new int(nNumber);

cout<<"\nNow enter "<<nNumber <<" Numbers\n" <<endl;

for (int i=0; i<nNumber; i++)
{
cout << "Enter number (" << (i+1) << "): ";
cin>>nTotal[i];
}

// code continues here...``````

Then make a counter to keep track of the positive numbers and one to keep track of the negative numbers.
THOSE will be the amounts you print of each (not the nNumber)

finally i solved it) huh.

``````#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int main() {

/*---------------------------------*/
int nNumber;
cout<<"Enter n Number ";
cin>>nNumber;
/*---------------------------------*/
int nTotal[nNumber],nPos = 0, nNeg = 0;
int nPosnum[nPos] , nNegnum[nNeg];
//Place to declare Variables
/*---------------------------------*/
cout<<"Enter "<<nNumber<<" numbers"<<endl;
for (int i = 0; i < nNumber ; i++)
{
cin>>nTotal[i];
if (nTotal[i]>0)
{
nPosnum[nPos++]= nTotal[i];
}
else if(nTotal[i]<0)
{
nNegnum[nNeg++] = nTotal[i];
}

}
//Here i cout the total of positive and negative numbers, then put them in array
/*---------------------------------*/
cout<<setw(21)<<left<<"Positive Numbers"<<"Negative Numbers"<<endl;

for (int k = 0 ; k < nPos ; k++)
{
cout<<nPosnum[k]<<endl;
}
//Here i @cout@ the positive numbers
/*---------------------------------*/
for (int l = 0 ; l <nNeg; l++)
{
cout<<setw(21)<<left<<""<<nNegnum[l]<<endl;
}
//Here the negatives
/*---------------------------------*/
getch();
return 0;

}``````
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.