Beginner needs help fast

B

BobJohnson

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
 
A

Alf P. Steinbach

* BobJohnson said:
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.
 
D

Daniel T.

BobJohnson said:
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 );
 
J

John Carson

BobJohnson said:
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");
 
J

Joe C

BobJohnson said:
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.
 
V

velthuijsen

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;
}
 
G

Gary Labowitz

velthuijsen said:
#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
 
G

Gary Labowitz

velthuijsen said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top