# n00b help

• 07-08-2003
kashifk
n00b help
Hi just have a simple question, c++ noob here
Code:

```#include<iostream.h> #include<stdlib.h> #include<iomanip.h> int main() {         float usedGallons=0.00, milesDriven=0.00,costpGallon=0.00,milespGallon=0.00,centspMiles=0.00,avgMipgall=0.00,totalmiles=0.00,totalgallons=0.00;                 cout<<"*************************************\n";                 cout<<"Enter gallons used(-1 to end): ";                 cin>>usedGallons;                 while(usedGallons!=-1)                 {                         cout<<"Enter miles driven: ";                         cin>>milesDriven;                         cout<<"Enter cost per gallon: ";                         cin>>costpGallon;                         cout<<"\n\n";                         milespGallon=(milesDriven/usedGallons);                         centspMiles=((usedGallons*costpGallon*100)/milesDriven);                         totalmiles+=milesDriven;                         totalgallons+=usedGallons;                         cout<<"  Results for this tank of gas: \n";                         cout<<setprecision(3)<<milespGallon<<"miles per gallon\n";                         cout<<centspMiles<<"cents per mile\n\n";                         cout<<"*************************************\n";                     cout<<"Enter gallons used(-1 to end): ";                     cin>>usedGallons;                 }                 cout<<"\n\n";                 avgMipgall=(totalmiles/totalgallons);                 cout<<"The average miles per gallon was " <<setprecision(3)<<avgMipgall<<endl;                 if(avgMipgall<20.0)cout<<"That is bad mileage.\n";                 if(avgMipgall>=20.0 && avgMipgall<35.0)cout<<"That is good mileage\n";                 if(avgMipgall>=35.0)cout<<"That is excellent mileage\n";                 system("PAUSE");                 return 0; }```
why dont the last 3 lines execute when the requirements are met
• 07-08-2003
LiKWiD
it worked fine when I compiled it...
• 07-08-2003
kashifk
those last few if statement dont execute when i run them on my machine, using microsoft visual c++, it goes straight to the system pause. But when i execute it on a linux machine it works, fine, any ideas why this is happening?
• 07-08-2003
DougDbug
This is just a guess...
Try putting a space before cout. I dunno if it's required, but there is usually whitespace between the if statement and the following conditional statement.
• 07-08-2003
VirtualAce
It's probably because of how the compiler compares floating point values.

Comparisons between floating point values are not always as reliable as those between integral values, especially when using the less than and greater than operators. Check Intel's website or books concerning the FPU for why this is.

So if the conditions inside of your if statements are never true because of improper floating point evaluation, they will never execute.

Perhaps the Linux OS is compensating for this floating point evaluation although I cannot see how this would be.

Since in your comparisons you only need to compare using the integral portion, you should be able to typecast your variables an integral type or you could use temporary integral variables in your comparison.

In assembly a compare is done something like this:

mov ax,Value
cmp ax,SomeValue
ja ValueIsLargerThanSomeValue

Note that you cannot place a floating point operand into the ax or eax register. Hence the compiler is using the FPU, if specified in compiler options, or a floating point emulator. Check to make sure that MSVC is using the FPU for all floating point operations and comparisons.

If it is because of syntax, then ignore this post, but remember that comparing floating point values may not always yield what you expect.
• 07-08-2003
kashifk
whitespace dont work at all, this is so strange....I have the same program in c, and all i did was convert it with the c++ sytax, it works there but not here.
• 07-08-2003
Zach L.
Use 'endl' instead of '\n' so that you flush the output buffer. I suspect that is your problem.
• 07-08-2003
PJYelton
Yeah, I just ran your program in MSVC++ and used endl instead of \n and it worked just fine.
• 07-08-2003
VirtualAce
Actually to compare two floating point values to see if they are exactly the same value is where most problems occur.

double x=5.0;
double y=10.0;

if (x==y) ....

This is not a good idea. Check here for the reason.

http://www.boost.org/libs/test/doc/f...comparison.htm

But your problem is most likely what the others have noted.
• 07-08-2003
kashifk
thanks guys, it worked....so how is endl different from \n that it was causing problem
• 07-08-2003
Cat
endl flushes the output as well. When you write to a stream, it doesn't immediately happen; it writes to a buffer and then, at some point, writes the buffer to the screen/file/whatever. When you need the buffer to be written immediately, you need to flush the buffer. std::endl flushes the buffer. You could also use \n and manually flush it, as well.