# [C++] My first IT course with simple output

• C/C++
• Silicon Waffle
In summary: You can use integers and count cents, or you can look at fixed precision or decimal libraries. If you can't find anything better, there's cpp_dec_float in boost. I believe that in commercial code some juristictions require the use of decimal libraries for currency.
Silicon Waffle
Hi, my C++ course has started since last week and I am learning basic input and output.
In my simple project, I have a function that will return a double number like this
Code:
double getLong(long long ll)
{
return ll/10.00;
}

The input is 12345 and the return value after the code is executed is 1234.500000
I find it strange as to why 0's are added unexpectedly.
I create a console application and test again with main like this
Code:
int main()
{
cout<<12345/10.00;
}
But the result becomes 1234.5 which is correct.

Another case I run with my function is if I input e.g 1234567890 then I expect the result should be 123456789.0 (I don't want the compiler to automatically delete my 0 after the decimal point). How can I achieve this ? Thank you a lot.

When you print your number cout uses a default which escapes me at the moment but which I suspect is a variable format which supresses trailing zeros.

However, you can adjust how numbers are printed:

http://www.arachnoid.com/cpptutor/student3.html

Silicon Waffle said:
The input is 12345 and the return value after the code is executed is 1234.500000

How do you know what the return value is?

I see the values in the debugger.
By the way I fixed it using the tutorial link in jedishrfu's post. I learned new thing in C++, setw and set precision. My teacher seemingly forgot them in his previous lecture.
Thanks.

Silicon Waffle said:
I find it strange as to why 0's are added unexpectedly.
Silicon Waffle said:
I see the values in the debugger.

The (default) number of decimal places to show is a decision by the authors of the debugger. There might be an option in the debugger's preferences section to change this.

Silicon Waffle
jtbell said:
The (default) number of decimal places to show is a decision by the authors of the debugger. There might be an option in the debugger's preferences section to change this.
Really ? I don't know if there is.

How exactly is the numerical value 1234.500000 any less correct than 1234.5? Or 1234.500 for that matter?

The output of your program (the text string "1234.5") is what is important and that is correct.

BoB

rbelli1 said:
How exactly is the numerical value 1234.500000 any less correct than 1234.5? Or 1234.500 for that matter?

The output of your program (the text string "1234.5") is what is important and that is correct.

BoB
Thank you BoB for your insight. Yes, it should be that.
I'm working on a financial application so I need to read from and write to the file the exact values.

Silicon Waffle said:
Thank you BoB for your insight. Yes, it should be that.
I'm working on a financial application so I need to read from and write to the file the exact values.
You need to be very careful when using floating point to represent money.
Code:
#include <iostream>
int main ()
{
double x = 0.10;
double y = 0.20;
double z = 0.30;
if ((x+y) != z)
{
std::cout << "This doesn't seem right!\n"
<< x << " + " << y << " - " << z << " = " << x+y-z << '\n';
}
}
On most computers, this will print
Code:
This doesn't seem right!
0.1 + 0.2 - 0.3 = <some small but non-zero number>

Last edited:
Silicon Waffle
The best way to be careful with floating point and currency is not to use floating point for currency.

You can use integers and count cents, or you can look at fixed precision or decimal libraries. If you can't find anything better, there's cpp_dec_float in boost. I believe that in commercial code some juristictions require the use of decimal libraries for currency.

Silicon Waffle

## 1. What is C++ and why is it important for IT?

C++ is a high-level programming language that is widely used in the field of IT. It is important because it allows for efficient and powerful manipulation of data and has a wide range of applications such as operating systems, game development, and scientific computing.

## 2. What does "simple output" mean in the context of IT?

In the context of IT, "simple output" refers to the process of displaying data or information on a screen or other output device in a straightforward and easy-to-understand format. This can include text, numbers, or graphics.

## 3. What are the basic concepts that I should know before starting my first IT course with C++?

Before starting your first IT course with C++, it is important to have a basic understanding of programming concepts such as variables, data types, control structures, and functions. It is also helpful to have a basic understanding of object-oriented programming principles.

## 4. How can I practice and improve my skills in C++?

The best way to practice and improve your skills in C++ is to write code and work on projects. There are also many online resources, tutorials, and exercises available that can help you practice and improve your skills.

## 5. Are there any common mistakes that beginners make when learning C++?

Some common mistakes that beginners make when learning C++ include not understanding the syntax and structure of the language, not properly using memory management techniques, and not fully understanding object-oriented concepts. It is important to take the time to thoroughly understand these concepts in order to avoid common mistakes.

Replies
23
Views
2K
Replies
5
Views
2K
Replies
22
Views
2K
Replies
39
Views
4K
Replies
6
Views
9K
Replies
17
Views
1K
Replies
2
Views
974
Replies
14
Views
32K
Replies
8
Views
3K
Replies
8
Views
2K