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

  • C/C++
  • Thread starter Silicon Waffle
  • Start date
  • Tags
    Course Output
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.
  • #1
Silicon Waffle
160
203
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.
 
Technology news on Phys.org
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 
  • Like
Likes Silicon Waffle
  • #6
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.
 
  • #7
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
 
  • #8
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.
 
  • #9
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:
  • Like
Likes Silicon Waffle
  • #10
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.
 
  • Like
Likes Silicon Waffle

FAQ: [C++] My first IT course with simple output

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.

Similar threads

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
14
Views
32K
Replies
8
Views
2K
Back
Top