By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Beginner needs help fast

P: n/a
Just started learning C++ and I need some help with my homework, shouldn't
take long for people around here. I need to create a simple money
calculator but I don't know how to make the output numbers two decimal
places long like 10.01 I only know how to define numbers as int or double.
Do I use float?
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting. The project
is really easy but I'm totally frustrated by the lack of information in the
book or from the professor none of this has been covered. The project is
simple: Input your name and how much money you make, the program performs
an algorithm to figure out your tax situation, everything is done and
running I just don't know how to keep the output at two decimal places so
it looks like money. Please help
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* BobJohnson <sc*****@nope.com> schriebt:

Just started learning C++ and I need some help with my homework, shouldn't
take long for people around here. I need to create a simple money
calculator but I don't know how to make the output numbers two decimal
places long like 10.01 I only know how to define numbers as int or double.
Do I use float?
There is unfortunately little useful help in the standard library. It does
define a class 'std::moneypunct' for parsing and generating character
sequences that denote monetary values, but it's so complicated to use that one
would think it was a joke if it weren't for the fact that it actually is a
standard library class. What's sorely needed is a standard class for monetary
_values_, and that's nowhere to be found in the standard library.

From what you write I don't think you're meant to use 'moneypunct' (it's
absolutely not something for the beginner, or even the very experienced).

And I don't think you're expected to define a monetary value class.

Presumably you're meant to use simple 'double' values, in spite of 'double'
not guaranteeing the necessary number of digits for monetary values.

And if so, all you have to do is
#include <iostream>

int main()
{
std::cout.precision( 2 ); // 2 decimals
std::cout.setf( std::ios_base::fixed ); // fixed point format
std::cout << 1.5 << std::endl; // produces "1.50"
}
Also, I'm using Visual Studio .NET
Ah, OFF-TOPIC. Anything to do with _particular_ compilers, if it's not of
general interest, should be asked in groups dedicated to the relevant compiler.
Likewise for development environment, operating system, libraries and so on.

The FAQ has much to say on this and other things.

Google finds the C++ FAQ for you in a jiffy.

is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting.


You probably mean, the program's output.

Yes, no matter which system you're using you can always run a text-mode
C++ program from a command interpreter.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
BobJohnson <sc*****@nope.com> wrote:
Just started learning C++ and I need some help with my homework, shouldn't
take long for people around here. I need to create a simple money
calculator but I don't know how to make the output numbers two decimal
places long like 10.01 I only know how to define numbers as int or double.
Do I use float?
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting. The project
is really easy but I'm totally frustrated by the lack of information in the
book or from the professor none of this has been covered. The project is
simple: Input your name and how much money you make, the program performs
an algorithm to figure out your tax situation, everything is done and
running I just don't know how to keep the output at two decimal places so
it looks like money. Please help


cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision( 2 );
Jul 22 '05 #3

P: n/a
"BobJohnson" <sc*****@nope.com> wrote in message
news:Xn*************************@216.196.105.138
Also, I'm using Visual Studio .NET is there anyway to keep the
compiler on the screen long enough to actually see what it's
outputting.


Try

system("pause");

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #4

P: n/a

"BobJohnson" <sc*****@nope.com> wrote in message
news:Xn*************************@216.196.105.138.. .
Just started learning C++ and I need some help with my homework, shouldn't
take long for people around here. I need to create a simple money
calculator but I don't know how to make the output numbers two decimal
places long like 10.01 I only know how to define numbers as int or double.
Do I use float?
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting. The project
is really easy but I'm totally frustrated by the lack of information in the book or from the professor none of this has been covered. The project is
simple: Input your name and how much money you make, the program performs
an algorithm to figure out your tax situation, everything is done and
running I just don't know how to keep the output at two decimal places so
it looks like money. Please help


Hi Bob. I'm not a c++ expert, or even close. However, the following may be
of use. As a C++ novice, getting help is often confusing b/c a given
problem often has many solutions that work...some more portably than others.
For example, the "system("pause");" suggestion is reasonable if you want to
execute the program from Windows by "double clicking". This solution is,
however, non-portable, and offensive to many who peruse this
forum...although it's likely a good pragmatic solution in your case. I
rarely reply here, since my knowledge-base is generally insuficcent, but
thought that an explaination might help you, since your "my window won't
stay open" problem is so basic and fundamental to the OS, and not to c++,
that you may not get a good explaination on this very good (but intolerant
of non-c++ issues) forum. Cheers.
Jul 22 '05 #5

P: n/a
> Also, I'm using Visual Studio .NET is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting.


#include <iostream>

int main()
{
// rest of program

std::cin.get(); // will keep the console window open until a key is pressed
return 0;
}
Jul 22 '05 #6

P: n/a
"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on the screen long enough to actually see what it's outputting.
#include <iostream>

int main()
{
// rest of program

std::cin.get(); // will keep the console window open until a key

is pressed

False. This keeps the console window open until one or more keys and
<Enter> are pressed.
return 0;
}

Jul 22 '05 #7

P: n/a

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on the screen long enough to actually see what it's outputting.
#include <iostream>

int main()
{
// rest of program

std::cin.get(); // will keep the console window open until a key

is pressed

False. This keeps the console window open until zero or more keys and
<Enter> is pressed.
return 0;
}

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.