|
-
February 19th, 2012, 01:56 PM
#1
int cannot equal
So this is my code and apparently somethings going on at around the if statement. It makes my calculations show up as 0. I know it's some bizarre coding, but Im sure you guys get what's going on. Please let me know of what I can do for the mistake and also what I can do to improve or clean up my code. Thanks.
Jeremy
-
February 19th, 2012, 01:59 PM
#2
Re: int cannot equal
Code:
#include <iostream>
using namespace std;
int main()
{
int frank;
int john;
int userage;
//the reason I declared one as 1 was because I wasnt able to figure out how to divide a number by a variable in the If statement.
//Only a variable by a variable.
int one=1;
//Fun
cout << "My name is Jeremy Shawaf \n";
cout << "Some things i like are desert parties and computers. \n";
cout << "I like techno music, and I like computer science, hence it being my major :D \n\n";
cout << "now that we got that out of the way....well frank is 27 years old, and john's age is unknown, but somehow....... \n";
cout << "We seem to always get the same answer...why dont you enter in your age...\n";
cin >> userage;
frank=27;
john=one/userage;
int totalages=(frank*userage*john);
cout << "now if we multiply your age by john and frank's then we should get " << totalages << " which is frank's age. \n";
if(totalages>=frank){
cout << "yours, frank's and john's age multiplied together equals " << userage << " which is your age.";
}
//back to HW
return 0;
}
-
February 19th, 2012, 04:05 PM
#3
Re: int cannot equal
An int can't hold a decimal value so your john=one/userage; division will make john equal to zero for all values of userage except 1.
Switch to double if you want to do decimal calculations.
Edit: See also this thread regarding floating point precision http://www.codeguru.com/forum/showthread.php?t=518005
Last edited by S_M_A; February 19th, 2012 at 04:07 PM.
-
February 19th, 2012, 09:28 PM
#4
Re: int cannot equal
Ohhhhhh I see, that completely makes sense. Yeah you cant tell it's a decimal unless you know what the cin is going to be........okay, well, thanks for the heads up Ill try changing it into a double. Thanks for the floating point reference also.
Jeremy
Also, I wanted to know......how can I make it so I can multiply numbers with my ints inside of my if statement.
Such as
if(john*4*frank>=27*one)
{
cout ect ect....;
}
How would I go about that?
Last edited by eyekantbeme; February 19th, 2012 at 09:56 PM.
-
February 20th, 2012, 01:46 AM
#5
Re: int cannot equal
That's a perfectly alright statement.
-
February 20th, 2012, 02:15 AM
#6
Re: int cannot equal
Okay, that's what I found out after I turned that int into a double, that was the reason why it wasnt working at first, great, thanks for that you guys, I cant wait til I start getting into more complicated code hehe but it's a great approach to think about all the ints and doubles and other values as such.
I will bring up anything else once it comes up, but with C++ is there much of a reason to clean up code like there is with html and CSS which I know arent languages, but they have their similarities.
Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|