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

Beginner C++ help

  • 29-07-2002 8:06pm
    #1
    Registered Users, Registered Users 2 Posts: 1,842 ✭✭✭


    Er, a little embarrasing, but I'm having trouble doing something very simple, just getting a percentage! :)

    Relevant lines from the code:
    variable definations:
    
    	int blockcount, blocktotal;
    	double percentcopied;
    
    code:
    	percentcopied = (blockcount / blocktotal) * 100;
    	printf("Copied: %d Percent: %f\n", blockcount, percentcopied);
    
    gives the following output:
    
    Copied: 1052 Percent: 0.000000
    Copied: 1053 Percent: 0.000000
    Copied: 1054 Percent: 0.000000
    Copied: 1055 Percent: 100.000000
    Copied: 1055 Percent: 100.000000
    

    No doubt someone experienced will spot what I've screwed up on immediately... :)

    (using MS Visual Studio 6, by the way)


Comments

  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    Hi,

    Your problem is where you have "blockcount / blocktotal". Both of these variables are integers, so an integer division is performed which will result in an integer answer: 0 in you case. You need to cast to a floating-point datatype before the division. ie. use (float) or (double).

    Zab.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by phaxx
    No doubt someone experienced will spot what I've screwed up on immediately... :)

    AFAIk, the problem is because you're two initial values are of type int. The division of one int by another will produce an int answer, which is then assigned to your double.

    Change them all to double and poss add some rounding to make the output a bit cleaner.

    jc


  • Registered Users, Registered Users 2 Posts: 1,842 ✭✭✭phaxx


    Ah, thanks, but how do I deal with this:

    div_t div_result;
    div_result = div(bytestotal, blocksize);
    blocktotal = div_result.quot + 1;
    

    That gives funky results and I had to comment out that line and set blocktotal in the code to get it to work.

    Er, actually can one of you recommend a short tutorial for the impatient explaining simple things like converting from one type to another? :)


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    I presume your doing something that isn't part of the original code here....

    You don't seem to be using the remainder so a simple
    blocktotal = bytestotal / blocksize + 1;
    should get you a block total that will be able to hold all the bytes. This doesn't take into account the possibility of a perfect division though, in which case you probably won't want to add one to the blocktotal. You can check for this by either using the % operator ( which returns the remainder of an integer division ) or go back to using div() but check the rem member before adding one to the answer...

    Although I have to say that I don't know why the div() didn't work... What sort of answers was is giving?

    Zab.


  • Registered Users, Registered Users 2 Posts: 1,842 ✭✭✭phaxx


    No, actually this is all part of one little project.

    Got it working now, thanks! No doubt I'll fall over again in a few minutes, though. All good learning experience. :)


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by phaxx
    Er, actually can one of you recommend a short tutorial for the impatient explaining simple things like converting from one type to another? :)

    Given that I know SFA about C++. not really.

    However, in general, languages work off a simple principle. Perform an operation using the "lowest common denominator" type of the operators.

    Thus : x = a / b is two operations.

    "divide a by b" , followed by "assign this value to x"

    Looking at it that way, you can see that the a/b operation will completely ignore the datatype of x.

    Thus, you typically convert on the way "in", if you see what I mean. THis is done by either assigning your values to the correct datatypes initially (as per my initial suggestion), or explicitly casting them during the op.

    As an example, if a and b were ints, I could either change them to be doubles, or do something like :

    x = (double) a / (double) b

    Here, I'm explicity "typing" them as part of the operation. This now reads as :

    Treat a as a double for this operation.
    Treat b as a double for this operation.
    Operation - divide a by b
    Assign the result to x.

    Get the idea?

    As to your secondary problem.....that was caused by forgetting that your result was not an int, at a guess.

    The easiest way to deal with this would be to cast back to an int, or to find a function like "round" which will take care of the decimals for you.

    At the end of the day, its a question of looking at the order in which things occur, and reasoning what datatypes will be used for an operation. When in doubt, cast explicitly.

    jc


  • Registered Users, Registered Users 2 Posts: 1,842 ✭✭✭phaxx


    On another note, how does one position the cursor at a certain location? (talking about the console here)

    Like for updating the percentage on a certain line, and that line only.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Borland used to supply a graphics function library that gave you the use of a gotoXY() function, that let you place the cursor at any position of a DOS window. Twas pretty neat. But before go off on a hunt to find it, let me advise you that for a program that is just copying a file, don't go overkill on the information that is echoed to the terminal window. *Think*, when you're using a shell or a DOS window, and hammering in commands faster than light, do you want half you console window filled up with junk that doesn't really interest you. When copying a file, a progress bar might be nice, but not:

    x bytes copied p% complete
    x bytes copied p% complete
    .
    .
    .
    x bytes copied p% complete
    done!


    That kind of info isn't generally that useful to the user (maybe the programmer though). Anyway I write a quick file copy prorgram with a progress bar for you to play with [CHECK YOUR PM]. :)

    It's been a while since I coded in C. I could get used to this, I like that language. But keep me away from that bastard child MFC stuff. :rolleyes:

    ;-phobos-)


  • Registered Users, Registered Users 2 Posts: 4,676 ✭✭✭Gavin


    C++ manipluators might be of use to you.
    http://www.cplusplus.com/ref/iostream/iomanip/

    Gav
    mfc jock


  • Registered Users, Registered Users 2 Posts: 1,842 ✭✭✭phaxx


    Yup, I'd heard about gotoxy, was searching for an alternative.

    As for displaying the progress, well I didn't mean to leave the results like that, of course! :P (hence me wanting a gotoxy alternative, to display the results on one pretty line)

    Thanks everyone


  • Advertisement
Advertisement