n00b help

This is a discussion on n00b help within the C++ Programming forums, part of the General Programming Boards category; 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"; ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    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

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    it worked fine when I compiled it...
    --LiKWiD
    Becoming un-noobified one day at a time.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    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?

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    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.
    Last edited by kashifk; 07-08-2003 at 10:59 AM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use 'endl' instead of '\n' so that you flush the output buffer. I suspect that is your problem.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, I just ran your program in MSVC++ and used endl instead of \n and it worked just fine.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    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.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    thanks guys, it worked....so how is endl different from \n that it was causing problem

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. n00b questions
    By C+noob in forum C++ Programming
    Replies: 43
    Last Post: 07-09-2005, 03:38 PM
  2. n00b problems
    By piebob in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2005, 01:51 AM
  3. n00b Code Error.
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2004, 05:15 PM
  4. n00b doing a Socket operation on non-socket
    By Kinasz in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-25-2004, 02:29 AM
  5. ISO someone daring to look at some n00b code!
    By Rev. Jack Ed in forum Game Programming
    Replies: 4
    Last Post: 10-17-2003, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21