Re: Double output problem
P.S. Yes, this is a Header file. Thus the lack of int main().
Re: Double output problem
You shouldn't put using namespace std; in a header file. Also, putting your function definitions in a header isn't a good habit; it'll cause you trouble later if you try to include that header in multiple cpp files. You should only put declarations in the header, not definitions.
As to your problem, try
cout << fixed << value;
Re: Double output problem
Yeah, so will that still work with this?
Code:
cout << "text" << setw(x) << setprecision(y) << fixed << right << value << endl;
Because our professor is adamant about our code having setw, setprecision and right in there.
Also, I added MoneyMath.cpp to my project. That contains implementations of my functions now and the .h only contains the declarations. Only thing is, I've been doing everything in .h so long i forgot. Do you include the .cpp now or the .h still?
Re: Double output problem
Quote:
Originally Posted by ccubed
Yeah, so will that still work with this?
Code:
cout << "text" << setw(x) << setprecision(y) << fixed << right << value << endl;
Because our professor is adamant about our code having setw, setprecision and right in there.
Also, I added MoneyMath.cpp to my project. That contains implementations of my functions now and the .h only contains the declarations. Only thing is, I've been doing everything in .h so long i forgot. Do you include the .cpp now or the .h still?
Okay, so I put it in but now something else is happening. With fixed it makes all the doubles set to their precision. Meaning, it adds 0s. Can i stop it from adding zeros?
Re: Double output problem
Quote:
Originally Posted by Lindley
You shouldn't put using namespace std; in a header file.
Hi Lindley,
how are ya?
The books I'm learning from do not advise against putting using namespace std in a header file. But, I saw many guys giving advice against it here on the forum many times. They just didn't explain why.
From what I can see, I frankly don't understand why I shouldn't do it. My take on why many experts advise against it is the possibility of a name collision? or re-declaration which doesn't really make sense to me either.
Even without the header guards, and we have a single header file used in multiple cpp files, I don't see why using declaration would cause problems.
So, I'm kinda lost on this one.
Can you or someone help me understand this better?
Thanks.
Re: Double output problem
If you put a "using" statement in a header file, then all cpp files which include the header will inherit that statement. This completely circumvents the purpose of namespaces, which is to allow abbreviations while maintaining strict control over what they mean. If you don't have a using statement in a cpp file, you shouldn't expect that namespace to be exposed.
For my own part, I don't even put in "using namespace std;" anymore; I only put in statements for the particular parts of the namespace I'm going to use, eg "using std::vector;".
Don't know about the zeros, sorry, I use printf() much more often than cout, myself.
Re: Double output problem
Quote:
Don't know about the zeros, sorry, I use printf() much more often than cout, myself.
Same here because TBH, the printf family of functions are infinitly easier to use for formatted output compared to streams, and make for much cleaner code.
All of your output, for example, using printf
Code:
printf("\t\tAmount of Loan:\t$%11.3f\n", amount);
printf("\t\tAnnual Interest Rate:\t%6.3f\n", rate);
printf("\t\tPayments Per Year:\t%3i\n", npy);
printf("\t\tTotal Number of Payments:\t%4i\n", totalPayments);
printf("\t\tMonthly Payment:\t%f\n", pmt);
Re: Double output problem
Thanks Lindley, that makes more sense.
Can I say, then, that
1. using namespace xxx technically does not causes compile or run time errors, and that it just doesn't add up with the purpose of the namespace?
2. When one of the multiple cpp files do not use the xxx, then that source file has redundancy?
and whats the zeros??
Re: Double output problem
It's just bad design. It (probably) won't cause program errors unless there's actually a name collision. Say, if you had a math library and math::vector collided with std::vector because both namespaces where using'd in their respective header files.....
Quote:
Same here because TBH, the printf family of functions are infinitly easier to use for formatted output compared to streams, and make for much cleaner code.
The one exception I take to that is reading unknown-length string inputs. The overloads for std::string are far cleaner than trying to do everything with scanf and C-style strings. But by and large, yeah, format strings rock.
Re: Double output problem
Re: Double output problem
Quote:
1. using namespace xxx technically does not causes compile or run time errors, and that it just doesn't add up with the purpose of the namespace?
It will definetly trigger compiler errors if used improperly in a header. Consider
Code:
#include "HeaderWithBadUsingNamespacestdDirective.h"
#include <string>
class string
{
public:
void Foo( ) { }
};
...
std::string text("Hello World!");
string temp;
text.clear( );
temp.Foo( ); // error here
If you're lucky, your compiler might give you an error about "string temp" being an ambiguous symbol. If you're not lucky, you'll just get an error that Foo() is not a member of string... and you'll soon be ready to claw your eyeballs out, since it clearly is a member of your string class.
Now, obviously a class named "string" may be an unlikely example, but there are so many names in the std namespace that you're begging for problems. Literally just yesterday on another forum I saw a topic from someone who had gotten themselves into this very problem when using a games library. The game library defined an integer type name "fixed", which was colliding with std::fixed.
Re: Double output problem
Quote:
Originally Posted by Speedo
Same here because TBH, the printf family of functions are infinitly easier to use for formatted output compared to streams, and make for much cleaner code.
All of your output, for example, using printf
Code:
printf("\t\tAmount of Loan:\t$%11.3f\n", amount);
printf("\t\tAnnual Interest Rate:\t%6.3f\n", rate);
printf("\t\tPayments Per Year:\t%3i\n", npy);
printf("\t\tTotal Number of Payments:\t%4i\n", totalPayments);
printf("\t\tMonthly Payment:\t%f\n", pmt);
And I wish i could use printf, though I probably could. I'm afraid he might get mad that i'm not using cout which is what he wants us to use. I'm already testing the waters with functions. I suppose I'll ask him.
Re: Double output problem
Reading up on the documentation, it appears that setprecision() controls how many digits are printed after the decimal point when in fixed-point mode. If you have to call setprecision() with those values, then you're stuck with the zeros.
However, setw()'s padding can be controlled using setfill(). So if you'd like to get rid of *leading* zeroes, you should be able to use that. Since setfill()'s fill character defaults to whitespace, I'm not sure how much help that is.
Re: Double output problem
So what about rounding a double to a certain number of significant figures? I know what's wrong with it. There's like 20+ decimal places in payment and i only need 2.