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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Something to do with programming [split thread]

  • 31-10-2008 11:58am
    #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.


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,198 ✭✭✭✭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,198 ✭✭✭✭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,198 ✭✭✭✭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,198 ✭✭✭✭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,198 ✭✭✭✭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,259 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,198 ✭✭✭✭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,198 ✭✭✭✭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,259 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,198 ✭✭✭✭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.


  • Registered Users, Registered Users 2 Posts: 784 ✭✭✭Peleus


    Amateur, I have a spare one. If you can't get one on monday, just let me know and i can give you my spare. I think the best thing to do is to just take a load of them during the labs and keep them at home.


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


    Peleus wrote: »
    Amateur, I have a spare one. If you can't get one on monday, just let me know and i can give you my spare. I think the best thing to do is to just take a load of them during the labs and keep them at home.
    Thanks man, and yeah you're right, I'm just gonna take three next time. Did you do DC Circuits last week too?


  • Registered Users, Registered Users 2 Posts: 784 ✭✭✭Peleus


    TheAmateur wrote: »
    Thanks man, and yeah you're right, I'm just gonna take three next time. Did you do DC Circuits last week too?

    no i did hooke's law. I think I'm doing DC Circuits next.

    In Dev C++, when i run the C++ application it runs fine, but the Command Prompt window dissappears immediately after the result is displayed. How do i stop it from doing this? I tried sticking pause in the c++ script but it didnt work.

    oh btw, i PMd you amateur


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


    Peleus wrote: »
    no i did hooke's law. I think I'm doing DC Circuits next.
    Ah right. The timetable they gave us in that red book was really useless wasn't it?
    Peleus wrote: »
    In Dev C++, when i run the C++ application it runs fine, but the Command Prompt window dissappears immediately after the result is displayed. How do i stop it from doing this? I tried sticking pause in the c++ script but it didnt work.
    Heh I just asked the same thing in the other thread. Damn windows...


  • Registered Users, Registered Users 2 Posts: 784 ✭✭✭Peleus


    TheAmateur wrote: »

    Heh I just asked the same thing in the other thread. Damn windows...

    found the answer online: just add system("pause");

    to the end of the script (but inside int main )


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,119 ✭✭✭Tails142


    run your program from the command prompt to begin with

    press windows button and r to bring up run window.

    type 'command' or sometimes 'cmd' and hit enter to open up command prompt

    find your directory

    use the command 'cd' to change directory

    'cd\' brings you back to root directory

    ^just incase you werent aware of the commands


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


    Peleus wrote: »
    found the answer online: just add system("pause");

    to the end of the script (but inside int main )
    Is system a function or something? What library would I have to declare at the top? Sorry for the probably stupid question:o


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    if my memory serves me right its part of conio.h - so #include "conio.h"
    bare in mind any time you use system, you are making your program none portable. Not standard.
    You could simply just have a cin >> at last line.


  • Registered Users, Registered Users 2 Posts: 784 ✭✭✭Peleus


    TheAmateur wrote: »
    Is system a function or something? What library would I have to declare at the top? Sorry for the probably stupid question:o

    i dunno, probably iostream because thats the only library in my prorgram and it works fine now.


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


    It says what to do at the top of lucys page..


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


    mathew wrote: »
    It says what to do at the top of lucys page..
    Lucy's page?


  • Registered Users, Registered Users 2 Posts: 784 ✭✭✭Peleus


    lucy hederman's page on TCD. just read it there. it says add the system pause command.


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


    Lucy's Page

    its under software


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


    Peleus wrote: »
    lucy hederman's page on TCD. just read it there. it says add the system pause command.
    mathew wrote: »
    Lucy's Page

    its under software
    Got it! Thanks :D


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


    TheAmateur wrote: »
    Ouch... that means you have Hooke's Law next doesn't it.

    Yeah. Damn easy one week and evil the next. Boring both times.


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


    Feck sake...

    I just answered that system pause question over on the other thread..

    Merge?


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


    jmccrohan wrote: »
    Feck sake...

    I just answered that system pause question over on the other thread..

    Merge?
    If the thread doesn't drunkenly lurch from one topic to another, it's not an engineering thread. In that regard, you have made this one beautiful.


Advertisement