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.
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.
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.
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 float
s), 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). double
s 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 double
s and float
s 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.
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 double
s, 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
.
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.
Comments