Hello ,
I've been working for an hour now searching for a solution for a problem i have for tomorrow's workshop so I wanted to be ready for it and here was the question :

Write a program that reads 3 integers. Then, display:
 The count of positive integers followed by the following text: positive integers are entered
 The count of negative integers followed by the following text: negative integers are entered
The following picture shows a sample output of your program:

so I wrote the following code , but i was wondering if there was a simplest way to do it , because I can never make it like that if i had like 10 integer instead of 3 so I am aware my way of writing the code isn't the best , please any suggestions ?
(ps: so far we learned the following , cin and cout , if statement , if.. else statement , and the logical operators )

#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "enter 3 integers : ";
cin >> a >> b >> c;
if (a > 0 && b > 0 && c > 0)
cout << "3 numbers are positif \n 0 numbers are negatif \n";
if (a > 0 && b > 0 && c < 0)
cout << "2 numbers are positif \n 1 numbers is negatif \n";
if (a > 0 && b < 0 && c > 0)
cout <<"2 numbers are positif \n 1 numbers is negatif \n";
if (a < 0 && b > 0 && c > 0)
cout << "2 numbers are positif \n 1 numbers is negatif \n";
if (a > 0 && b < 0 && c < 0)
cout << "1 number is positif \n 2 numbers is negatif \n";
if (a < 0 && b < 0 && c > 0)
cout << "1 number is positif \n 2 numbers is negatif \n";
if (a < 0 && b > 0 && c < 0)
cout << "1 number is positif \n 2 numbers is negatif \n";
if (a < 0 && b < 0 && c < 0)
cout << "0 numbers are positif \n 3 numbers is negatif \n";




system("pause");
return 0;


}] 

For three integers you can hardcode it fairly simply as

int a, b, c;
std::cin >> a >> b >> c;
int positiveCounter = 0;
int negativeCounter = 0;
if (a < 0)
    negativeCounter++;
else
    positiveCounter++;
if (b < 0)
    negativeCounter++;
else
    positiveCounter++;
if (c < 0)
    negativeCounter++;
else
    positiveCounter++;

std::cout << positiveCounter << " positive integers are entered\n";
std::cout << nagativeCounter << " negative integers are entered\n";

If you wan to do this for an indefinte number of variables then you should write a function to to this for you. You would get all off the input into some sort of data structure (array, vector, set, list, ...). Then you would pass that to a function which would do the counting. Tha function would look like

template<typename ForwardIt>
std::pair<int, int> CountPosativeAndNegative(ForwardIt it, ForwardIt end)
{
    std::pair<int, int> returner{0,0};
    while(it != end)
    {
        if (*it < 0)
            returner.second++;
        else
            returner.first++;
        ++it;
    }
    return returner;
}

or you could simply use a loop (in pseudo-code):

int posCount=0, negCount=0
while (more input to process)
   input next number
   if (next number > 0) posCOunt++
   else negCount++

print counts

Hello Sara 13;

Since the topic was related with logical operators and if else statement.

I suggest doing this. Soon you can do it with Loop. But for now I only use else if statement with logical operators. And for a readable output put white blanks before and after " ".

I hope this help you; Cheers;

PS: This code have limitation; Like if you type a char or string. That will be the problem.

-zelrick;

#include <iostream>
using namespace std;
int main() 
{
    int a, b, c;
    cout<<"Enter the numbers: ";
    cin>>a;
    cout<<"\n";
    cout<<"Enter the numbers: ";
    cin>>b;
    cout<<"\n";
    cout<<"Enter the numbers: ";
    cin>>c;
    cout<<"\n";

    if (a >= 0 && b >= 0 && c >= 0)
        cout<<"\n" <<a <<", " <<b <<", and " <<c <<" are Positive.\n";

    else if (a < 0 && b >= 0 && c >= 0)
        cout<<"\n" <<b <<" and " <<c <<" are Positive and " <<a <<" is Negative.\n";

    else if (a < 0 && b < 0 && c >= 0)
        cout<<"\n" <<c <<" is the only Positive and " <<a <<" and " <<b <<" is Negative.\n";

    else if ( a >= 0 && b < 0 && c >= 0)
        cout<<"\n" <<a <<" and " <<c <<" are Positive and " <<b <<" is Negative.\n";

    else if ( a >= 0 && b < 0 && c < 0)
        cout<<"\n" <<a <<" is the only Positive and " <<b <<" and " <<c <<" is Negative.\n";

    else if (a < 0 && b >= 0 && c < 0)
        cout<<"\n" <<b <<" is the only Positive and " <<a <<" and " <<c <<" is Negative.\n";

    else if ( a < 0 && b < 0 && c < 0)
        cout<<"\n" <<a <<", " <<b <<", and " <<c <<" are Negative.\n";

    else if ( a >= 0 && b >= 0 && c < 0)
        cout<<"\n" <<a <<" and " <<b <<" are Positive and " <<c <<" is Negative.\n";


    cout<<"\n You Entered " <<a <<"," <<b <<"," <<c;
    cout<<"\n";


    cout<<"\n";
    system("pause");
    return 0;
}

A next step might be to use functions to get valid input in a loop?

Also maybe use a struct tp hold the 'counts'?

Take a look:

// countPosNeg.cpp //  // 2015-0915 //

#include <iostream>

using namespace std;


struct Counts
{
    int pos, neg;

    // default ctor...
    Counts() : pos(0), neg(0) {}
} ;


int takeInInt( const char* msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "Invalid input ... numbers only!\n";
            cin.clear(); // clear error flasgs
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

int takeInChr( const char* msg )
{
    cout << msg << flush;
    int c = cin.get();
    cin.sync();
    return c;
}

bool more()
{
    int c = takeInChr( "More (y/n) ? " );
    if( c == 'n' || c == 'N' ) return false;
    //else ...
    return true;
}




int main()
{
    Counts cts; // construct with default values //

    do
    {
        int i = takeInInt( "Enter an integer: " );
        if( i >= 0 ) ++cts.pos;
        else ++ cts.neg;
    }
    while( more() );

    cout << "Out of " << (cts.pos+cts.neg) << " numbers, you entered "
         << cts.pos << " positive and " << cts.neg << " negative.\n";

    takeInChr( "Press 'Enter' to exit ... " );
}
commented: We need more code like this - real structure vs one endless block +15
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.