Help me please i dont know what is wrong with my program

#include <iostream>

using namespace std;

int main()
{


int a;
int b;
int c;
cout<<"type how many alcholic beverages you have had times 12:\n";
cin >> a;
cout <<"type your weigth:\n";
cin>> b;
cout <<"type 0.68 if you are male or 0.55 if you are a woman:\n";
cin>> c;
cin.ignore();
int result = b * c / a;
cout<<"Result is"<<" "<<result<<endl;
cin.get();
return 0;
}

ive made this program.. but it only returns 0 when i run it.. anyone who can se my mistake?
In C++, if you divide an int by an int, you get an int. Remember that.



1
2
cout <<"type 0.68 if you are male or 0.55 if you are a woman:\n";
cin>> c;

You made c an int. How do you expect an int to hold a value of 0.55? It can't.

c / a;
This is an int divided by an int. It will give you an int. An int can only hold integer values (you know what integers are, yes? Seems silly to ask, but some beginners do not know what an integer is).


You need to start actually thinking about what kinds of values you are trying to store and calculate.

1
2
3
int a;
int b;
int c;
These should NOT all be int.
Last edited on
You're asking for a float, but using an int in
1
2
cout <<"type 0.68 if you are male or 0.55 if you are a woman:\n";
cin>> c;

Uhm i should change C to what then.. im a little confused its not my first program but i havent been working much on it.
Like kbw said, use a float(floating point number). A float (or it's bigger cousin the double) is the computer's way of storing decimal numbers. In your code you try to store decimal numbers in an integer, but they will just get "trunctuated" to zero, since floats are always rounded down on casting to int:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
int a = 0.1;
int b = 0.9;
int c = 1.1;
std::cout<<a<<":"<<b<<":"<<c;
std::getchar();
}

If you try compiling this program you'll see that both a and b end up as zero. Instead, use a float to store the values:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
float a = 0.1;
float b = 0.9;
float c = 1.1;
std::cout<<a<<":"<<b<<":"<<c;
std::getchar();
}

Now you should be seeing the right values being output. One downfall to floats is that they aren't perfectly precise, and some values can not be represented perfectly. For example, from Wikipedia:
"The representable number closest to 0.01 is
0.009999999776482582092285156250 exactly."
So if you need high precision, be very careful about floats, and always remember that they can introduce error into your code. Doubles use twice the number of bits as floats, and as such have "double" the precision, which can help but is still not perfect.
Why are [ int a, b ,c ] not initialized? Also, did you mean [ (b*c)/a ] or [ b*(c/a) ]?
The computer cannot tell what is in your mind.
Thanks both and i ment B*c/a :) and i will try and fix it now :)
closed account (3TXyhbRD)
There are different ways to make int division to float division. For instance, C/C++ makes int division only when both operands are integers (short, int, long, long long), therefore if one of the operands is float (float or double) the division is a float division.
Examples:
1
2
3
4
5
6
7
int a = 21, b = 13, c;
float d;
c = a / b; // int division
d = static_cast<float>(a) / b; // float division
d = a / static_cast<float>(b); // float division
d = static_cast<float>(a)/13; // float division
d = a/13.0; // float division 
Also, did you mean [ (b*c)/a ] or [ b*(c/a) ]?

Setting aside precision issues, the two are equivalent.
Also, did you mean [ (b*c)/a ] or [ b*(c/a) ]?

Setting aside precision issues, the two are equivalent.


... assuming that the variables have been changed to floats or doubles.
Thanks for the help guys :) i've made the program work :)
Topic archived. No new replies allowed.