I know a little bit of Objective-C and I'm not too familiar with the differences between C++ and Obj. C I'm looking for a solution to a business need that I have. I'm looking to create a C++ program on my Windows computer to solve a few simple equations, one after the other. I don't know where to start or if using Python would be better suited for this problem.
Problem: I want to be able to open up the application on my computer and then be prompted to enter a value for 2 different variables (these values will be called x and p). I then need the program to run through a set of equations (6 in total). I then need the program to return the value of 3 different variables (we'll call them: c, g and h). A big issue I'm finding, is that I need to round up/down a couple of variables for the other equations to work, yet I don't know what function I need to use to make this happen. Here is a breakdown of how it should go:
Prompt-
Please enter the Buy Price:
1. 1000/x = t
2. t (rounded to the nearest one) = c (this value to display as the result)
3. c(x) = f
4. (p + f) / c = k
5. k (rounded to the nearest hundredth) = g (this value to display as the result)
6. g(c) - f = h (this value to display as the result)
Has anyone ever worked with a website or program builder that can easily go through these set of equations? Or can anyone tell me how to use the round up/down function in C++? Also how would I go about carrying the value of a variable over to the next equation in order to solve it?
if working with $USA I recommend working as if 1.0 = 1 penny, not 1 dollar. Then you may choose to use integers in some places, avoiding some issues (but unable to handle fractional cents, of course).
second, watch rounding too much. If you round stuff, then compute, then round again, then compute something else, after a while your rounding accumulates and creates error. keeping the full doubles as long as possible before rounding, or rounding only the result before display, may be better depending on what is needed.
Assuming that we are using a standard library implementation which supports our locale
(ie. libraries other than the GNU library on non-linux platforms):
#include <iostream>
#include <locale>
#include <iomanip>
#include <cmath>
int main()
{
// set the appropriate locale for stdin, stdout
// in this example, we use en_US.UTF-8 / en-US
// note: locale names are implementation specific
std::locale en_us( "en_US.UTF-8" ) ;
std::cin.imbue(en_us) ;std::cout.imbue(en_us) ;
// get the monetary values p and x
// since the locale is en_US.UTF-8, the monetary unit is a cent
// note: using the lowest denomination monetary unit for all our
// computations minimises/eliminates rounding errors
// ie. input of $123.45 or USD 123.45 or just 123.45 is returned as 12345
// http://en.cppreference.com/w/cpp/io/manip/get_moneylongdouble p, x ;
std::cout << "enter p and x: " ;
std::cin >> std::get_money(p) >> std::get_money(x) ;
// to print out monetary values, we use std::put_money
// http://en.cppreference.com/w/cpp/io/manip/put_money
std::cout << std::showbase ; // show the currency symbol on output
std::cout << "\np: " << std::put_money(p)
<< "\nx: " << std::put_money(x) << "\n\n" ;
// compute the integer value p/x, rounded to the nearest integer
// http://en.cppreference.com/w/cpp/numeric/math/roundconstlonglong c = std::llround(p/x) ;
std::cout << " #contracts: " << c << '\n' ;
constlongdouble f = c * x ; // compute f (in cents)
constlongdouble delta = 14000 ; // $140.00 ie. 14000 cents
// compute k (to the nearest cent) using std::llround
constlongdouble k = std::llround( (f+delta) / c ) ;
std::cout << " sell price: " << std::setw(8) << std::put_money(k) << '\n' ;
// calculate gross profit (in cents) and print out the amount
constlongdouble g = std::llround( k*c - f ) ;
std::cout << "gross profit: " << std::setw(8) << std::put_money(g) << '\n' ;
}