0

I'm trying to break this problem into function, but my problem is that I always get different sum, positive and negative count when I print out the result.

Can someone give me a hint?

Write a program that reads ten integer numbers and outputs the sum of all the positive numbers among them. The program should ignore all numbers which are less than or equal to 0. The program should also display count of positive numbers and count of negative numbers or zero.

#include <iostream>

using namespace std;
void input(int number, int positiveCount, int negativeCount, int sum);
void output(int positiveCount, int negativeCount, int sum);

int main()
{
int number, positiveCount, negativeCount, sum;
input(number, positiveCount, negativeCount, sum);
output(positiveCount, negativeCount, sum);


return 0;
}
void input(int number, int positiveCount, int negativeCount, int sum)
{
cout << "Enter 10 integers: " << endl;
for (int i = 0; i < 10; i++)
{

    cin >> number;
    if (number > 0)
    {
        positiveCount++;
        sum = sum + number;
    }
    else
    {
        negativeCount++;
    }
 }


}
void output(int positiveCount, int negativeCount, int sum)
{
     cout << sum << endl;
     cout << positiveCount << endl;
     cout << negativeCount << endl;

}
BBjornsson
  • 11
  • 1
  • 2

4 Answers4

2

Your input() function needs to take its arguments by reference so it can modify them. And you need to initialize all those ints to 0 at the start or they contain garbage.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Without initialization it's UB, not only simply garbage. – Deduplicator Oct 11 '14 at 14:24
  • @Deduplicator: sort of. I think the UB will be prevented by the first change, to use references instead of pass-by-value for the `input()` function. This relies on the idea that taking a reference is equivalent to taking its address, which rules out UB and gives just an indeterminate value. See here: http://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior-in-c - but whether I'm right about merely passing the variables by reference saving us here, I'm not sure, and the program is wrong without initialization in any case. – John Zwinck Oct 11 '14 at 14:32
  • 1
    The linked Q&A is for C, not C++. In C++, using indeterminate values is straight UB. Need not be from memoryless variables for that to hold. (And you are right, that does not change the question whether the program is right, only how badly it is wrong.) – Deduplicator Oct 11 '14 at 14:36
1

The operations you have done inside input() function is lost because the scope of the variables are only inside the function.

You need to use either pointers or reference while passing the parameters into the input() function so as not to use a local copy.

While using pointers you need to do dereferencing also. And initialize the variable to 0 before passing to the function.

Inquisitive
  • 475
  • 1
  • 5
  • 13
0

Because there is serious mistake in your program. You define four local variables in function main() and send them by value when invoking function input(). This function DO NOT modify variables defined in the function main(). It simply modify their copies. These copies is deleted when you're out of function input().

In order to modify them you should use reference:

void input(int &number, int &positiveCount, int &negativeCount, int &sum);

But it has no meaning to create four integers in function main() and to send them in the functions input() and output(). You can create four local variables in input() and then print them in this function. Then you shouldn't define function output() and you can delete it in your code. I.e. you should modify your program.

gsa
  • 516
  • 5
  • 9
0

In your assignment there is written:

Write a program that reads ten integer numbers and outputs the sum of all the positive numbers among them

So there is no need to write separate functions for this simple program. It can look like

#include <iostream>

int main() 
{
    const size_t N = 10;

    std::cout << "Enter " << N << " integers: ";

    size_t i = 0, count = 0;
    long long sum = 0;

    int num;

    for ( ; i < N && std::cin >> num; i++ )
    {
        if ( num > 0 )
        {
            sum += num;
            ++count;
        }
    }

    std::cout << "You have entered " << count << " positive numbers\n"
              << "and " << i - count << " negative numbers or seroes\n"
              << "Sum of positive numbers is " << sum << std::endl;

    return 0;
}

If you want to write separate functions then for example function input could be declared as

long long input( size_t &positive_count, size_t &negative_count );

or

long long input( size_t &total_count, size_t &positive_count );

or

long long input( size_t *positive_count, size_t *negative_count );

or

long long input( size_t *total_count, size_t *positive_count );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335