简体   繁体   中英

I am not sure why I am getting this output from my function

My function is supposed to take the user input of dollars and optionally pennies and convert it into all cents result. But for some reason the number I am getting is bigger than what the math should be producing. This is my code.

#include <iostream>
using namespace std;

double NumberOfPennies(int dollars, int pennies = 0)
{
    int totalPennies = 0;
    totalPennies = (dollars * 100) + pennies;
    cout << totalPennies;
    return 0;
}

int main()
{
    cout << NumberOfPennies(5, 6) << endl; // Should print 506
    cout << NumberOfPennies(4) << endl; // Should print 400
    return 0;
}

The output from this is 5060 and 4000. I am confused because 5 times 100 plus 6 should produce 506 like what the comments indicate. I am not sure why 5 times 100 and adding 6 gives the result of 506. Mathmatically I can't see how I am wrong but perhaps there is a reason because of my code?

You should be returning the totalPennies from NumberOfPennies (and not printing inside NumberOfPennies). Right now you're printing the correct result in NumberOfPennies (506) and then adding the 0 that you return.

Have no idea why you put penny=0 in the parameter; Please override the method if there is a need.

Your extra "0" comes from "return 0" in your NumberOfPennies.

In main function , you intended to output the value of NumberOfPennis(5,6), which has a value of 0, that is the reason why you got 5060 rather than 506.

You must think like a computer.

cout << NumberOfPennies(5, 6) << endl;

instructed the computer to call NumberOfPennies and print out the value returned by NumberOfPennies .

However inside NumberOfPennies , you

cout << totalPennies;

which prints the commuted number of pennies, 506 in this case, before returning the number 0.

so

  1. totalPennies is computed.
  2. totalPennies is printed.
  3. 0 is returned.
  4. 0 is printed.
  5. end of line is printed and stream is flushed

output

          5060
totalPennies^^return value

You have two options that I would consider reasonable:

  1. Do all of the printing in NumberOfPennies or
  2. Return totalPennies and do the printing in main

My preference is option 2 as it keeps the responsibilities of NumberOfPennies restricted to doing exactly one thing: computing the number of pennies. If NumberOfPennies does nothing but compute, any function can now call NumberOfPennies and do whatever it wants with the result, including print. For example using NumberOfPennies in another computation or even as an input to itself:

NumberOfPennies(64, NumberOfPennies(3,6));

So

#include <iostream>
using namespace std;

int NumberOfPennies(int dollars, int pennies = 0)
{
    int totalPennies = (dollars * 100) + pennies;
    return  totalPennies;
}

int main()
{
    cout << NumberOfPennies(5, 6) << endl; // Should print 506
    cout << NumberOfPennies(4) << endl; // Should print 400
    return 0;
}

Note that the return type of NumberOfPennies has been changed to match totalPennies and total pennies is no longer initialized to zero since that zero was immediately overwritten with the computed result. That mean we can also go one step further if we want:

int NumberOfPennies(int dollars, int pennies = 0)
{
    return (dollars * 100) + pennies;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM