Home > Blockchain >  I made this code where it will display the product of all positive integer inputs inputted by the us
I made this code where it will display the product of all positive integer inputs inputted by the us

Time:05-25

So we were tasked to create a program that will ask user a positive integer until the user inputted a non-positive number or zero and then display the product of all positive inputs. Using function prototypes. Everything is working fine, however, the displayed output is wrong. I'm guessing it's related to the formula I used but I can't figure it out. Can someone help me please?

#include <iostream>

using namespace std;

int accept_number(){
    int x;
    cin >> x;
    return x;
}

bool ispositive(int x){
    if (x > 0)
        return true;
    else
        return false;
}

int product(int x, int y){
    return (x*y);
}

void display (int a){
    cout << "The product is " << a << ".";
}

int main(){
    int userNum, total = 1;

    do {
        cout << "Enter a number: ";
        userNum = accept_number();
        total  = product(userNum, total);
    } while (ispositive(userNum));

    cout << endl;
    display(total);
    cout << endl;

    return 0;
}

CodePudding user response:

I thik there are two minor issues on your code:

  1. You want the product of all positive numbers, have I understood correctly? Then why the line total = product(userNum, total);? This calculates the product between the last input number and the total, and then adds it to total. If you need only the product, you should overwrite total, not update it with the product, i.e. something like total = product(userNum, total), or directly total *= userNum;

  2. In a do-while loop, the statement is checked for at the while level. This means that the loop exits only after the negative number has been taken into account for the product. You could either add an if statement before updating total and then directly exit your cycle with a break or something like that, actually it is a matter of coding style.

CodePudding user response:

The problem is the statement:

total  = product(userNum, total);

In the above statement, you're adding in total the result returned by the call to product. Moreover, there is no need to call the function product. You can just directly multiply total with userNum as shown below:

//---vv----------->also note the * instead of  
total*= userNum; //there is no need to call any function 

Working demo

CodePudding user response:

Let's say you input 2 at the beginning of the program. Now you'd expect total to be 2 (2*1 = 2). But with what you're doing, this is not the case:

total  = product(userNum, total);

..substituting values in this case:

1  = product(2, 1);

..which is 3. What you're doing is adding the product of userNum and total to total, which, according to the question, is not you want to do here. Try this:

total *= userNum; // can also be written as total = total * userNum

Also, your code's loop is wrong. Why? Because if you enter a negative number, it will get multiplied to total, then the loop will be exited, which will lead to incorrect results. So you should restructure your loop as such:

int userNum = 1, total = 1; // initialize userNum = 1

do {
    total *= userNum; // this was the last instruction previously
    cout << "Enter a number: ";
    userNum = accept_number();

} while (ispositive(userNum));
  •  Tags:  
  • c
  • Related