-3

My program is supposed to calculate the price of items (customers get a discount off regular price of the item if they purchase 10 or more of the item). It's all working well except for the int Product::fulfillOrder(int orderq) function. It says a value must be returned, but the orderq should be returning a value for the function to use (I believe I did it right). Here's the code for the problem function:

int Product::fulfillOrder(int orderq)
{
    if (orderq < 0)
    {
        cout << "Error" << endl;
        orderq = 0;
        cout << "Shipped: " << orderq << endl;
    }

    else if (orderq <= Quantity)
    {
        orderq = Quantity;
        Quantity -= orderq;
        cout << "Shipped: " << orderq << endl;
    }
    else
    {
        orderq = Quantity;
        orderq = 0;
        cout << "Shipped: " << orderq << endl;
    }
}
valiano
  • 16,433
  • 7
  • 64
  • 79
  • 3
    where's the return statement – frozen Jul 09 '17 at 02:42
  • When I added that it gave me a number like 9.108+e10. I removed it because of that. Do you think I should change it to like "return 1;"? – Rez The Ripper Jul 09 '17 at 03:04
  • 1
    Apparently you meant `void Product::fulfillOrder`. – GSerg Jul 09 '17 at 10:53
  • Possible duplicate of [Is a return statement mandatory for C++ functions that do not return void?](https://stackoverflow.com/questions/2784075/is-a-return-statement-mandatory-for-c-functions-that-do-not-return-void) – Raymond Chen Jul 09 '17 at 11:04
  • Compile with all warnings and debug info (e.g. `g++ -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...) then **use the debugger** (e.g. `gdb`) to run your program step by step, query its state, thus understanding what is happenning – Basile Starynkevitch Jul 09 '17 at 14:04
  • What does g++ -Wall -Wextra and -g mean? I'm really new to C++ and I've never heard of these terms. – Rez The Ripper Jul 09 '17 at 14:23
  • @RezTheRipper It generates more warnings and compiles with debugging information. https://linux.die.net/man/1/g++ – klutt Jul 16 '17 at 16:21

2 Answers2

1

I think you need to add

return orderq;

at the end of the function.

Harsh Shankar
  • 506
  • 5
  • 16
0

The return statement is missing. If you want to return an int you must return what you need. What I can see in your code is that you put the input parameter to 0 whatever condition is. You must check just if the input is greater than 0 because it doesn't make sense if the order is less than 0. Something impossible. Meaning why do you want to check if the order is below zero? No sense, check if its greater in order to go inside the calculus.

When the input is -1 the two first 'if' statements are true. Also you are doing something like:

orderq = Quantity
Quantity = orderq

If you want to exchange values, you need a third auxiliar variable, but analyzing your code and what you need to do. You need to resolve your problem on paper first. Then coded it.

Good luck!

thecatbehindthemask
  • 413
  • 1
  • 6
  • 15