#include <iostream>
#include <cmath>
using namespace std;
void main()
{
float iR;
double Time;
double P;
double newBalance;
float interest;
cout << "What is your interest rate? " << endl;
cin >> iR;
cout << "How often is it compounded? " << endl;
cin >> Time;
cout << "What's the principle amount?" << endl;
cin >> P;
{
newBalance = P * pow(1 + iR/100/Time,Time);
interest = P * pow(1 + iR/100/12,Time) - P;
}
cout << "Interest rate " << "4.25" << iR << endl;
cout << "\nTimes Compounded " << Time << endl;
cout << "\nPrincipal " << "$" << P << endl;
cout << "\nInterest " << "$" << interest << endl;
cout << "\nAmount in Savings " << "$" << newBalance << endl;
system("Pause");
}
how to round up?
Page 1 of 111 Replies - 1189 Views - Last Post: 06 September 2014 - 10:18 AM
#1
how to round up?
Posted 05 September 2014 - 05:52 PM
I'm new to c++ and I'm in my 3rd week of class. I've got a program complete but I can't for the life of me figure out how to get the one of the outputs to round up. The interest is supposed to output as 43.34 but mine outputs as 43.3382. I've tried things from the book and the internet and so far no luck.
Replies To: how to round up?
#2
Re: how to round up?
Posted 05 September 2014 - 06:28 PM
if you just want to print rounding you can do
also include <iomanip>
double x = 43.3382; std::cout << std::fixed << std::setprecision(2) << x;
also include <iomanip>
#3
Re: how to round up?
Posted 05 September 2014 - 06:48 PM
hmmm, I don't really understand where to put that in the code
#5
Re: how to round up?
Posted 05 September 2014 - 08:20 PM
Also in a C++ program main() must be defined to return an int and you should return an int from this function.
Edit: And just using setprecison() and fixed() will not cause your variable to round up it will just display the value to the number of decimal places specified, no rounding will occur.
You can solve some of the problem by sticking with variables of type double instead of mixing float and double variables. But this may not solve all your issues since floating point numbers are approximations and have possible inaccuracies.
Jim
int main()
{
return 0;
}
Edit: And just using setprecison() and fixed() will not cause your variable to round up it will just display the value to the number of decimal places specified, no rounding will occur.
You can solve some of the problem by sticking with variables of type double instead of mixing float and double variables. But this may not solve all your issues since floating point numbers are approximations and have possible inaccuracies.
Jim
This post has been edited by jimblumberg: 05 September 2014 - 08:29 PM
#6
Re: how to round up?
Posted 06 September 2014 - 07:07 AM
I had also asked in my class discussion board and replied that I should add in cout << setprecision(2) << fixed; Once I added that at the beginning of the cout statements everything is working
I'm still not 100% sure on when to use float so that's why it's here. Would it be better to have them set as double in this situation?
Okay so this is my code now, everything seems to be working perfectly
Okay so this is my code now, everything seems to be working perfectly
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void main()
{
double iR;
double Time;
double P;
double newBalance;
double interest;
cout << "What is your interest rate? " << endl;
cin >> iR;
cout << "How often is it compounded? " << endl;
cin >> Time;
cout << "What's the principle amount?" << endl;
cin >> P;
{
newBalance = P * (pow(1 + iR/100/Time,Time));
interest = P * pow(1 + iR/100/12,Time) - P;
}
cout << setprecision(2) << fixed;
cout << "Interest rate " << "%" << iR << endl;
cout << "\nTimes Compounded " << Time << endl;
cout << "\nPrincipal " << "$" << P << endl;
cout << "\nInterest " << "$" << interest << endl;
cout << "\nAmount in Savings " << "$" << newBalance << endl;
system("Pause");
}
#7
Re: how to round up?
Posted 06 September 2014 - 07:10 AM
> Okay so this is my code now, everything seems to be working perfectly
You're still using void main though, so perhaps it really is "seems to be working".
The first lesson is that the program isn't bug-free as soon as it stops crashing.
You're still using void main though, so perhaps it really is "seems to be working".
The first lesson is that the program isn't bug-free as soon as it stops crashing.
#8
Re: how to round up?
Posted 06 September 2014 - 07:50 AM
Salem_c, on 06 September 2014 - 10:10 AM, said:
> Okay so this is my code now, everything seems to be working perfectly
You're still using void main though, so perhaps it really is "seems to be working".
The first lesson is that the program isn't bug-free as soon as it stops crashing.
You're still using void main though, so perhaps it really is "seems to be working".
The first lesson is that the program isn't bug-free as soon as it stops crashing.
I would go even further and say that just because it displays what you expect, doesn't mean that the program is bug-free.
*EDIT*:
I'd also like to add that in <cmath> there is a function to round up: ceil(). Look at the reference page for the entire header for other helpful functions.
This post has been edited by vividexstance: 06 September 2014 - 07:52 AM
#9
Re: how to round up?
Posted 06 September 2014 - 08:54 AM
I'm using the void main because that's what the professor told us to use. Same with the <cmath> I'll look up the ceil()
#10
Re: how to round up?
Posted 06 September 2014 - 08:59 AM
Edit: Thank you very much!!!! After research the ceil() function I got it working as well as another program!!!!! You guys have been a huge help!!
#11
Re: how to round up?
Posted 06 September 2014 - 10:02 AM
> I'm using the void main because that's what the professor told us to use.
Perhaps you should get them to read the standard for once, rather than just relying on what out-dated compilers will let you get away with.
http://www.open-std..../2011/n3242.pdf
If they can't get the very first declaration right, you have to ask yourself what other nonsense are they 'teaching' you.
Perhaps you should get them to read the standard for once, rather than just relying on what out-dated compilers will let you get away with.
http://www.open-std..../2011/n3242.pdf
Quote
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
If they can't get the very first declaration right, you have to ask yourself what other nonsense are they 'teaching' you.
#12
Re: how to round up?
Posted 06 September 2014 - 10:18 AM
He actually even told us, at least last week to run the program without debugging
He hasn't really said anything this week either way. I'll start using int main because the book uses it a lot.
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|