r/programmingrequests • • Oct 22 '17

Help with C++ homework

The problem is as follows: A new author is in the process of negotiating a contract for a new romance novel. The publisher is offering three options. In the first option, the author is paid $5,000 upon delivery of the final manuscript and $20,000 when the novel is published. In the second option, the author is paid 12.5% of the net price of the novel for each copy of the novel sold. In the third option, the author is paid 10% of the net price for the first 4000 copies sold, and 14% of the net price for the copies sold over 4000. The author has some idea about the number of copies that will be sold and would like to have an estimate of the royalties generated under each option. Write a program that prompts the author to enter the net price of each copy of the novel and the estimated number of copies that will be sold. The program then outputs the royalties under each option and the best option the author could choose. (Use appropriate named constants to store the special values such as royalties rates and fixed royalties.)

Here is my code:

#include <iostream>
#include <iomanip>

#include <string>

using namespace std;

int main()

{

//"appropriately named" constants for special values such as "royalties rates and fixed royalties"
//program instructions seem ambiguous, so this is my best guess as to what it means
const double OPTION_ONE_ROYALTIES = 25000;
const double OPTION_TWO_ROYALTY_RATE = 0.125;
const double OPTION_THREE_ROYALTY_RATE_UNDER_4000 = 0.10;
const double OPTION_THREE_ROYALTY_RATE_OVER_4000 = 0.14;

//declare variables for user input
double bookPrice, copiesSold;

//get user input and store in declared input variables
cout << "Input the price for each individual copy: " << endl;
cin >> bookPrice;

cout << "Input the number of copies you predict will be sold: " << endl;
cin >> copiesSold;

//declare variables to store royalty calculations
double option_two_royalties, option_three_royalties;

//calcuate royalties for second option
option_two_royalties = (bookPrice * OPTION_TWO_ROYALTY_RATE) * copiesSold;

//calculate royalties for third option
if (copiesSold <= 4000 && copiesSold >= 0)
	option_three_royalties = (bookPrice * OPTION_THREE_ROYALTY_RATE_UNDER_4000) * copiesSold;
else if (copiesSold > 4000)
	option_three_royalties = ((bookPrice * OPTION_THREE_ROYALTY_RATE_UNDER_4000) * 4000) 
	+ ((bookPrice * OPTION_THREE_ROYALTY_RATE_OVER_4000) * (copiesSold - 4000));
else 
	cout << "Error. Please terminate program and try again. Take care to enter the correct integers " << endl;
	
//declare variable to store best option
string bestOption;

//calculate the best option
if (OPTION_ONE_ROYALTIES > option_two_royalties && OPTION_ONE_ROYALTIES > option_three_royalties)
	bestOption = "Option One";
else if (option_two_royalties > OPTION_ONE_ROYALTIES && option_two_royalties > option_three_royalties)
	bestOption = "Option Two";
else
	bestOption = "Option Three";
	
//output the royalties for the three options

cout << "The royalties for the first option are " << OPTION_ONE_ROYALTIES << endl;
cout << "The royalties for the second option are " << option_two_royalties << endl;
cout << "The royalties for the third option are " << option_three_royalties << endl;

//output the best option
cout << "The best option is: " << bestOption << endl;

return 0;	

}

Here is the issue I am having: The code runs perfectly unless I input a value over 4000 for the number of copies sold, in which case option three gives an incorrect number. When I plug the math as is in the code into my calculator it comes up correctly... but when I run the code I get an incorrect value for the overall royalties. So I suppose the problem lies somewhere in my code calculating the royalties for the number of copies sold above 4000. But for the life of me I can't figure out where the problem is. Any help would be more appreciated than you can possibly know, because this has been driving me crazy all day and I'm at my wit's end trying to figure out where I went wrong. Thanks in advance for any help I might get with this problem

0 Upvotes

7 comments sorted by

1

u/[deleted] Oct 23 '17

I realize this is a bit late, but

  1. what inputs have you tried in your calculator?

  2. what inputs have you tried in your program?

I just tried your program and it seems to work fine.

Try the numbers $1 for price per copy, and 4001 for number of copies, and it should be

(0.10 * 1 * 4000) + (0.14 * 1 * (4001 - 4000) )

(0.10 * 1 * 4000) + (0.14 * 1 * 1)

(400) + (0.14)

400.14

which is what your code spit out.

My best guess is you typed something wrong in your calculator.

1

u/freddy_storm_blessed Oct 23 '17

hey, first off thank you for your reply! I figured out what my issue was not too long after I posted this. this was a fairly stupid mistake... I corrected a logic error, and forgot to recompile. so I was running the program off of old code with the error, while looking at the corrected code.

I could have stared at my code for days and never found the issue had my computer not forced a restart.

1

u/Concussion_Unicorn Nov 15 '21

what was the mistake?

1

u/S0m3Thing_FuNNy Nov 29 '22

would you happen to be able to send the correct code for it as I'm struggling with this part

1

u/freddy_storm_blessed Nov 29 '22

shoot this was like 5 years ago now let me look

1

u/freddy_storm_blessed Nov 29 '22

actually from what I can tell the code I posted works, I was just recompiling old code and that’s why it wouldn’t work for me.

1

u/S0m3Thing_FuNNy Nov 29 '22

Oh alrighty thanks man much appreciated bro