Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Something to do with programming [split thread]

  • 31-10-2008 12:58PM
    #1
    Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭


    I win!
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main () {
    	double hours, rate;
    	string name;
    	cout.setf(ios::fixed);
    	cout << "Enter hours worked, hourly rate and name: ";
    	cin >> hours >> rate >> name;
    	cout << "Working for " << setprecision(0) << hours;
    	cout << " hours, at a rate of pay of &#8364;";
    	cout << showpoint << setprecision(2) << rate;
    	cout << " per hour, " << name << " will earn &#8364;";
    	cout << showpoint << hours*rate << ".\n";
    	
    return 0;
    }
    
    ee4mac27:Desktop b$ ./test2
    Enter hours worked, hourly rate and name: 10 1.5 b
    Working for 10 hours, at a rate of pay of €1.50 per hour, b will earn €15.00.

    Could anyone please
    1. Explain cout.setf(ios::fixed); - I got that off some website but don't understand it. It's something to do with setting the number of decimals printed rather than total numbers printed, as before I used it I was getting earnings like €310.5 instead of €310.50
    2. Tell me how to display 10.5 hours if 10.5 is entered, and 10 if 10 is entered. ie entry dependent decimal places.

    PS had the final cout as one line, but it didn't fit in the coding box ^

    cout << name << " will earn ";
    	if (hours*rate<=9000){
    		cout << "&#8364;" << showpoint << hours*rate << ".\n";}
    	if (hours*rate>9000){
    		cout << "OVER &#8364;9000 .\n";}
    
    Ahem.


«1

Comments

  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    I'd dock you a mark for including iomanip when it's not needed.


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    ...What do you win? :P

    edit: yeah but probably give him a bonus point for being a first year engineer and knowing about namespaces. He's already beaten some of his 3rd/4th year counterparts.


  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    Nah, we where thought in first year "just add this magic line".


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    Sorry I had the redundant iomanip because in the first iteration of the program I was using getline.

    Anyone got any responses to my edits? I edited the post several times, but the page was reloading from the cache so I didn't realise people had replied.


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    for your second question, i'm quite rusty at the moment, and only way I can think of is to do a modulus on it - so hours mod 1 and if its non-zero then you cast your hours double to an int. Its not the nicest, there must be an easier way, but it'd work fine.


  • Advertisement
  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    getline is part of iostream not iomanip.

    you should change it to
     cout << "Working for " << setprecision(0) << hours
    << " hours, at a rate of pay of &#8364;"
    << showpoint << setprecision(2) << rate
    << " per hour, " << name << " will earn &#8364;"
    << showpoint << hours*rate << ".\n";
    
    

    the ; ends a statement, you can have as much whitespace as you want within a statement. The above results in one call to cout as opposed to five consecutive ones.


    ios:fixed sets a fixed decimal place. use precision() to indicate how many digits you want after the decimal point.


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    if (hours%1 !=0){cout << "Working for " << setprecision(1) << hours;}
    if (hours%1 ==0){cout << "Working for " << setprecision(0) << hours;}

    ?


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    You can do it that way alright. Though why you need two if's is beyond me ;) - remember, if its not one condition, then it has to fulfill the other right?

    As I said, i'm 100% sure there must be a very small check for something like that, I just can't remember.


  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    That won't work. hours % 1 will always return 0;

    setprecision(-1) would get you the xx output setprecision(0) gets you xx.0000

    Its bad style to do a equals comparison on a double. Remember that doubles are approximations and often never exactly equal a value. So if hours == 0 will often cause a small undetectable bug in your code if hours is derived from some operation.

    Tbh, I'd have this
    setprecision(1) 
    
    if (hours - floor (hours) > 0) {
      hours = (int) hours;
    }
    cout << "Working for " << hours;
    
    
    

    Thats caused casing. You can cast down, but never up. C++ will allow you to cast up, but if the new type is larger (as in used more bytes) you will end up overwritting memory used for something else.


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    How will hours % 1 always equal zero? a non integer value should return a non zero value. Can see the bigger error being double precision aye. Using floor is a much better way of doing it. However you can avoid casting by instead something along the lines of:
    setprecision(1) 
    
    cout << "Working for ";
    if(hours-floor(hours) ==0){setprecision(0);}
    cout << hours;
    

    which avoids casting.

    oh, and Boston, that casting won't work. Because you'll simply be assigning the integer value of hours to the double variable hours - c and c++ will still see it as a double afair.


  • Advertisement
  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    Right you are, for a second there I thought % was an integer operation. Avoiding casting is probably best, especially if you can't explain it when the demonstrator asks.

    So between a qualified computer engineer and a qualified computer scientist, we've manage to come up with an optimal solution to a first year programming problem. I shouldn't feel proud, but I do.


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    And I used get paid to come up with those. Now i'm doing it on the internet, for free, from bed.

    Oh god.


  • Closed Accounts Posts: 17,163 ✭✭✭✭Boston


    oh, and Boston, that casting won't work. Because you'll simply be assigning the integer value of hours to the double variable hours - c and c++ will still see it as a double afair.

    True,

    hmm, int hours = (int) hours. // that should produce a nice error;

    Yea, forget casting.


  • Registered Users, Registered Users 2 Posts: 1,488 ✭✭✭mathew


    Baza210 wrote: »
    1. Explain cout.setf(ios::fixed); - I got that off some website but don't understand it. It's something to do with setting the number of decimals printed rather than total numbers printed, as before I used it I was getting earnings like €310.5 instead of €310.50

    The setprecision function is what sets the the amount of decimal places.
    Instead of cout.setf(ios::fixed);, you can use cout >> fixed;
    It changes from scientific notation to decimal notation..


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    @mathew- thanks. Didn't understand any of what the other two said though.. I might understand it if I could try it out but I'm back on XP now and cba trying to compile something on this.


  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,307 Mod ✭✭✭✭Jonathan


    Baza210 wrote: »
    @mattew- thanks. Didn't understand any of what the other two said though.. I might understand it if I could try it out but I'm back on XP now and cba trying to compile something on this.
    If you want you could download cygwin (which will just emulate a unix terminal) you could compile from there using make etc?


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    It doesn't matter if you understand it or not - its optimal. OPTIMAL I TELL YOU.


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    What is floor?


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    Floor gives you the largest integral value that isn't greater than x (the c++ reference way of putting it.)

    In essence, for 12.3 the largest integral value will be 12, for -2.3 it'll be 2 etc. etc.

    Its just a quick nice and easy way of rounding down to an integer.


  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,307 Mod ✭✭✭✭Jonathan


    May i assume that a mod other than crash named this thread? :P


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    I feel for whoever did this. I mean, they must hate me.


  • Registered Users, Registered Users 2 Posts: 582 ✭✭✭TheAmateur


    It doesn't matter if you understand it or not - its optimal. OPTIMAL I TELL YOU.
    That thurr's a sig line if ever I did see one :pac:


  • Registered Users, Registered Users 2 Posts: 1,488 ✭✭✭mathew


    LOL @ the thread title :P

    And I just posted in the other thread by accident... before I realised this one existed


  • Registered Users, Registered Users 2 Posts: 11,205 ✭✭✭✭Crash


    Was talking to a guy last night, who detailed the story of a compiler he came across, the source of which was of course thousands of lines long, and contained one comment. Half way through, all it said was "...and now for the hairy bit."

    I'm such a nerd that I wet myself laughing nearly :P


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    I seem to have digressed the Physics Labs thread to programming again. Whoops.


  • Registered Users, Registered Users 2 Posts: 582 ✭✭✭TheAmateur


    Baza210 wrote: »
    cout << name << " will earn ";
        if (hours*rate<=9000){
            cout << "&#8364;" << showpoint << hours*rate << ".\n";}
        if (hours*rate>9000){
            cout << "OVER &#8364;9000 .\n";}
    
    Ahem.
    picard-facepalm.jpg


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    Page stretch =(


  • Registered Users, Registered Users 2 Posts: 582 ✭✭✭TheAmateur


    Haha yeah it does that... [size] doesn't seem to work on images. While I'm on topic, dya know if I can get a lab report any old day or do they just give them out during labs? I feckin forgot to get one last time and I dunno what to do now...


  • Registered Users, Registered Users 2 Posts: 5,699 ✭✭✭Brian


    I'd say you can pick them up any time. I know that you can grab multiple copies in one week, so it's not like they're restricting access to the report sheets.

    I have to find mine actually.. and fill it in.. pendula eh..

    Edit: loldevilpost.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 582 ✭✭✭TheAmateur


    Baza210 wrote: »
    pendula eh..

    Ouch... that means you have Hooke's Law next doesn't it.


Advertisement