954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Little Problem

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;

}
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
 

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.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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++?

james6754
Junior Poster
188 posts since Dec 2010
Reputation Points: 50
Solved Threads: 15
 

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

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
 

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

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
 

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)

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

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;
     
}
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You