How do I store numbers in C++ without the decimals being truncated?

JasonZ

I am writing a simple percentage calculator in C++. I am a beginner and I can't figure out how to store the decimals without them being removed. If I enter any input that results in a decimal, it changes to a 0 and messes up the rest of the calculation.

#include <iostream>
using namespace std;
int main ()
{
 int a;
 int b;
 int c;
 int result;
 cout << "Enter your goal: ";
 cin >> a ;
 cout << "Enter the amount achieved: ";
 cin >> b ;
 c = b / a;
 result = c * 100;
 cout << result;
 cin.get();
} 

I know that the integer c is where the problem is, but I have know idea what I need to do to make this work. I don't know why I am having trouble with what should be a simple program.

Juan Carlos Coto

This looks like a case of a solution working for the wrong reasons. Like @MikeCAT says in his answer, if you multiply by 100 first, it "works" (i.e. the result you get is not 0). While that might look like a solution, it isn't, especially since multiplication should be commutative, right? It doesn't make sense for the order of factors to change the answer. It shouldn't.

It works, right?

Well, yes, and no. The real problem (like others have said) in your question is that you're not using data types the right way. As I understand, you don't have much programming experience, which is cool. Welcome to programming! Computers are dumb, and when I say "dumb", I mean oh-my-god-you-are-the-stupidest-machine-ever dumb.

A number is not always just a number

As other answers have mentioned, there are two main ways of handling numbers in programming (computers are so dumb that they need to be told which one to use): integers and floating points. Integers (as you probably know) are for the numbers in the set {0, 1, -1, 2, -2, ...}. They cannot represent rational numbers. Floating point numbers (or floats), represent rational numbers, up to a certain degree of exactitude. The math behind floats is a bit complex (and I'm definitely not an expert), but basically, they allow you to represent a rational number up to a number of decimal points (since computer memory is limited, it is not possible to represent an infinite number of decimals). doubles like others have mentioned, pretty much just double the amount of decimal points you can specify. Since memory was much more limited before, it was common to use float, but you can use double know without much worrying about it - and it's going to give you better results for calculation. Note that there are some specific problems you can run into with doubles and floats rounding and overflowing and all sorts of crazy stuff (which you should read about if you're doing more serious calculation), but in general, they're fine for most rational calculations.

The difference between these two data types is in the way they are represented in memory, when translated to binary. Computers understand binary only, so everything needs to be translated so they can work with it. You don't need to know the details right now, but in essence, the data type you choose really makes a difference.

So what do I do?

Well, you have to know what sort of calculation you want to do, and what you want to use it for. Sometimes, the language in which you program automates this choice (or chooses the more powerful version) for you, but in C++, that is not the case. In C and C++, where your programming is closer to what computers understand than in other languages, you usually have to be much more specific (and careful) when programming. That's also why programming in C and C++ tends to be considered as more difficult than others: less things are automated for you.

In your case, you probably don't really want integers. When you use integers, it's impossible to use decimals. So, when you divide, for instance 2 / 3, the answer is 0 (with a remainder of 2) - and when you multiply by 100 remains 0. We all know it's not really 0, but the computer cannot represent 0.66 in an int. It just can't. So, it gives you the closest thing, which is 0. Now, if you do something like 100 * 2 / 3, then the answer would be 66.6..., which is represented as 66. The .6 is lost, but the answer is no longer 0. So, it appears to "work", but it doesn't, really. Even for percentages, there is a pretty big difference between 66% and 66.66%.

If you used doubles, however, the story would be different. In that case, 2 / 3 would be 0.6..., and when you multiply that by 100, then you get 66.6..., which is the right answer. That would be the exact same result as doing 100 * 2 / 3.

Putting it all together

So, your program should look like this:

#include <iostream>
using namespace std;
int main ()
{
 double a;
 double b;
 double c;
 double result;
 cout << "Enter your goal: ";
 cin >> a ;
 cout << "Enter the amount achieved: ";
 cin >> b ;
 c = b / a;
 result = c * 100;
 cout << result;
 cin.get();
} 

This would work the right way, and you will have learned something cool in the process. Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Java, how do I store large numbers in an array?

How do I execute a Git command without being in the repository?

How do I format decimals in C?

How do I print 8 decimals in php

How can I prevent a matTooltip text from being truncated?

How do I store a large int in python without the newlines being counted by len()?

How do I write files to a USB without being root?

How do i store numbers bigger than 10 billion

Rails How to store signed decimals and how to parse a string for numbers in ruby

Using an array, how do I store and display the unique numbers?

How do I kill strace without killing the process being traced?

How do I store lookup tables in magic numbers?

How do I reference a reactive value in shiny without being dependent on it?

How do I deal with C# decimals when using IronPython?

Java getFloat troubles - decimals being truncated

How can I store these numbers in C?

How do I store the printed numbers from this function into a list (Python)?

How do I Randomize four numbers without them being the same thing in c#?

How do I stop Excel from rounding numbers when format has been set to number and to 2 decimals?

How do I get decimals values without rounding them off

How do I get rid of php decimals

How to remove Numbers with and without decimals using regular expression (numbers without spacing in alphabets and numbers)

ASP Core Minimal API - How do I send decimals without trailing zeros with System.Text.Json?

convert list of decimals to hexidecimals without getting truncated

How do I format numbers as decimals in NextJS

How do I make a button in C# that executes a file without being installed on the machine?

How do I wait on a condition without it being a race condition?

How Do I Investigate a CPU Usage Spike Without Being Present?

How do I update a value of an object without being inside of the object?