d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Programming Question > Don't Even Know How To Start This ...
12Next
Add Reply New Topic New Poll
Member
Posts: 25,415
Joined: Aug 30 2009
Gold: Locked
Trader: Scammer
Feb 19 2014 01:27am


#include <iostream>
#include <cmath>

using namespace std;
double findQuadratic(a, b, c);
{
double quadratic = (-b + sqrt(b2-4ac))/2a); --- Not right but will fix, this is temporary.
return quadratic;
}


int main()
{
double x2value, xvalue, constantvalue;
cout << "x2 coefficient value:" value:;
cin >> x2value;
cout << "x coefficient value: ";
cin >> xvalue;
cout << "constant value: ";
cin >> constantvalue;
double quadratic = findQuadratic(x2value, xvalue, constantvalue);
cout << "The roots are: " << quadratic;
cin.ignore();
cin.get();
return 0;
}

This is an example of a problem I had along with the current one, so you can see the setup. Had to find a way to utilize the quadratic equation (I didn't fill in the actual formula yet but the setup is there I think....)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 19 2014 01:47am
What's your question?
Member
Posts: 25,415
Joined: Aug 30 2009
Gold: Locked
Trader: Scammer
Feb 19 2014 01:48am
Quote (AbDuCt @ Feb 19 2014 02:47am)
What's your question?


How do I create a formula that will allow the user to input P and R and then allow me to choose for certain T values?
Do I create seperate functions for each T-Value. Not sure how to just change T given the amounts they input and get the values.
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 19 2014 12:09pm
for user input, please see this http://www.cplusplus.com/forum/articles/6046/ , it shows you how to get user input as a string and then converting it to a number type. You just repeat this for P, r and T.
You don't need to create separate functions, just create one function that takes P, r and T as arguments, so you can compute A for any T value.

Hope this helped :)

/e can show actual code if you really can't manage it

This post was edited by m0hawk on Feb 19 2014 12:10pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 19 2014 02:37pm
Quote (m0hawk @ Feb 19 2014 02:09pm)
for user input, please see this http://www.cplusplus.com/forum/articles/6046/ , it shows you how to get user input as a string and then converting it to a number type. You just repeat this for P, r and T.
You don't need to create separate functions, just create one function that takes P, r and T as arguments, so you can compute A for any T value.

Hope this helped :)

/e can show actual code if you really can't manage it


Please don't listen to this guy you are doing it the correct way.

Quote (PiK @ Feb 19 2014 03:48am)
How do I create a formula that will allow the user to input P and R and then allow me to choose for certain T values?
Do I create seperate functions for each T-Value. Not sure how to just change T given the amounts they input and get the values.


Depends, you can submit to the function 8 times, each time hardcoding the time variable in ex:

Code
function(a, b, 1);
function(a, b, 5);
...


Or you can also create an array of time and loop through them

Code
int years[1, 5, 10, 20, 30, 50, 75, 100];

for(int i = 0; i < years.length(); i++) {
function(a, b, years[i]);
}


If you can find a mathematical correlation between those numbers, say that they are all n*10 (10, 20, 30, 40, 50) you could also do

Code
for(int i = 0; i < 100; i*10) {
function(a, b, i);
}


Although this will be harder with data sets that don't have a clear pattern.

If you are comfortable with arrays, I'd just input all your dates and loop through them. Simplest and cleanest way.

As for your formula you just need to create a function that accepts three(or more) variables.

Code
double getInterest(double primitive, double rate, double time){
//use these three variables to calculate your math
return interest;
}


Maybe I am not understanding, but I don't think you need the quad formula, you can simply do

Code

double interest = pow(princible * 2.718), rate * years);


Just woke up so I may be wrong.

This post was edited by AbDuCt on Feb 19 2014 02:46pm
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 19 2014 03:43pm
I dont see what's wrong with my answer... I just showed him a cleaner way to handle user input and said he just needed one function with the three arguments just like you did in your answer :huh:

This post was edited by m0hawk on Feb 19 2014 03:44pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 19 2014 04:17pm
Quote (m0hawk @ Feb 19 2014 05:43pm)
I dont see what's wrong with my answer... I just showed him a cleaner way to handle user input and said he just needed one function with the three arguments just like you did in your answer :huh:


How is grabbing a string from STDIN then converting a string to a double cleaner? You must be out of your mind if you think it is.
Member
Posts: 25,415
Joined: Aug 30 2009
Gold: Locked
Trader: Scammer
Feb 19 2014 06:10pm
Well I figured it out and used very minimal help from here (no offense, not saying your help was bad or anything), so I'm very happy with myself :D
On my way to tech guru.
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 19 2014 11:49pm
Quote (AbDuCt @ Feb 20 2014 12:17am)
How is grabbing a string from STDIN then converting a string to a double cleaner? You must be out of your mind if you think it is.


Did you even read the link i posted ? Using cin directly with a type other than string can cause undefined behavior when entering eg a letter instead of a number. Avoiding that is indeed "cleaner".

This post was edited by m0hawk on Feb 19 2014 11:50pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 20 2014 12:19am
Quote (m0hawk @ Feb 20 2014 12:49am)
Did you even read the link i posted ? Using cin directly with a type other than string can cause undefined behavior when entering eg a letter instead of a number. Avoiding that is indeed "cleaner".


You aren't wrong. std::cin does have a tendency to provoke input validation concerns. But there are ways around this without introducing strings. Especially for a beginner, let's not bog him down with things he hasn't learned yet.

The following will loop until the user enters an integer:
Code

int d;
while(!(std::cin>>d))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll