How can i get this code to show once and it counting all the positive?

When i enter a 10 numbers. it show

The positive are....
The positive are....
The positive are....
The positive are...
The positive are...
...
until 10

can someone show me how to make it show only once and keeping counting the number?

 if (findNum[i] > 0)
        {
            cout <<"The positive are " << findNum [i] << endl;

        }

        else
        {
            cout <<"The Negative number are " << findNum[i] << endl;

        }

Recommended Answers

you didn't post the loop, but you need to declare a counter before the loop starts then increment it every time a positive integer is encountered inside the loop. Only after the loop finishes should you display the final count.

Jump to Post

What are the contents of findNum? That's somewhat critical as to your results. However, it strikes me that you'd want to split it into two loops to get a more reader friendly output:

cout << "Positive: ";

for (int i = 0; i < num; i++) …
Jump to Post

All 6 Replies

you didn't post the loop, but you need to declare a counter before the loop starts then increment it every time a positive integer is encountered inside the loop. Only after the loop finishes should you display the final count.

I dont understand!

for (int i=0; i < num ; i++)
    {
        if (findNum[i] > 0)
        {
            cout <<"The positive are " << findNum [i] << endl;

        }

        else
        {
            cout <<"The Negative number are " << findNum[i] << endl;

        }



    }

if i change to this way

if (findNum[i] > 0)
        {
            cout <<"The positive are " << i + 1 << endl;

        }

it show me how many i got but still have the

The positive are....
The positive are....
The positive are....
The positive are...
The positive are...
...
until 10

What are the contents of findNum? That's somewhat critical as to your results. However, it strikes me that you'd want to split it into two loops to get a more reader friendly output:

cout << "Positive: ";

for (int i = 0; i < num; i++)
{
    if (findNum[i] > 0)
    {
        cout << findNum[i] << ' ';
    }
}

cout << "\nNegative: ";

for (int i = 0; i < num; i++)
{
    if (findNum[i] < 0)
    {
        cout << findNum[i] << ' ';
    }
}

cout << '\n';

this is what I meant

int counter = 0;
for (int i=0; i < num ; i++)
    {
        if (findNum[i] >= 0)
        {
            ++counter;
        }

    }
cout <<"The number of positive integers are " << counter << endl;
cout <<"The number of negative integers are " << num - counter << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.