How would you make this code more efficient.

Hello all,

This is my second day of learning programming. I wanted a real world problem that could be solved with some simple lines of code.

Scenario
Sometimes, I may have to price match a product with an online competitor. However, that competitor may not charge sales tax when I have to. Essentially, I want to make my final product price (after taxes) the same as the online competitor.

My solution:
If I was to do this without programming, I usually state it in an equation like:
x + (x*salesTax) = competitors price
I then input the two variables I know (comp price and sales tax)and solve for x. After thinking about that, this became my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int main()
{
	
	float productPrice;
	float taxDecimal;
	float competitorPrice;

	cout << "What would you like the final price to be? ";
	cin >> competitorPrice;
	cout << endl << "Enter the sales tax in decimal form: ";
	cin >> taxDecimal;
	taxDecimal++;

	productPrice = competitorPrice / taxDecimal;

	cout << "The product price is " << productPrice << "." << endl;

	return 0;
}


My question is, with this only being my second day of learning, how would you guys handle this differently? Right now, I don't know how to stop a decimal after two places (or round up). Is there anything you would do differently?

Thanks in advance,
iWillLearn
closed account (48T7M4Gy)
Good start.
Try this for learning about decimal places etc.

http://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout

Look up the corresponding references at places like http://www.cplusplus.com/reference/ios/fixed/
Last edited on
Thanks for the advice.
Topic archived. No new replies allowed.