Page 3 of 6 FirstFirst 123456 LastLast
Results 31 to 45 of 88

Thread: C++ Help

  1. #31
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Since this is apparently a general C++ thread now, I'll offer some other advice. r1, r2, r3 etc. are very bad choices for variable names. If something stores the modulus of 5, call it at bare minimum "modulus_of_5". If something is a countdown variable, "countdown" is much better than "cd". If you named your variables like this, you wouldn't even need most of the comments you have in your code there. We call it "self-documenting".
    I do it, Remainder 1 etc.. the shorter the variable name, the less likely I am to type something wrong.

    Comments should never say WHAT your code is doing. If you can't tell what you're doing in the code just by reading the code, the code probably needs to be rewritten or clarified. Comments should say things like 1) Overall purpose / summary of entire blocks or functions 2) WHY you're doing something (not WHAT you're doing or HOW you're doing it), 3) Anything happening that's subtle or not immediately obvious, 4) Assumptions that are being made about input/output, which your code relies upon. Endless has good comments above. Source code is meant for human beings to read. Machines only understand binary. Good code = easy to read code.
    Our teacher wants us to comment everything.

    I can't argue with the rest, I welcome any and all advice, since I'm not good at C++, I just like doing it.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  2. #32
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    This is a toughie, we need some help. The prompt is as follows:

    "Your program will allow the user to input a "countdown" time including hours, minutes, and seconds, and will accurately count down the time until all units are zeroes. At that time you may print out an appropriate message."

    I need tu use a nested loop, and can't use pre defined functions to count. How would I do this?
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  3. #33
    Ominous Wanderer Tech Admin Samuraid's Avatar
    Join Date
    Oct 2001
    Posts
    5,522

    Default

    You cannot generate accurate time without using the kernel to supply you with signals/clock. (that's basically what the time-related functions do)

    You could try and simulate a clock with a certain number of loops delay, but it will drastically vary across machines and will also depend on how your program's execution is scheduled.

  4. #34
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Thats what we are doing. But I don't know how to start. I can fine tune it to this peice of crap machine.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  5. #35
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Code:
    while hours >= 0
       {
        while minutes >= 0
           {
            while seconds > 0
               {
                for i = 1 to whatever_is_needed_to_make_second
                   {
                    // Something to pass time here.
                   }
                seconds = seconds - 1;
               }
            seconds  = 60;
            minutes = minutes - 1;
           }
        minutes = 59;
        hours = hours -1;
       }
    cout << "Time's up!";

    And then there is Death

  6. #36
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    Or convert the time to all seconds and count down against that master number.
    Figaro Castle

  7. #37

    Default

    I think C++ has a Sleep command.
    It's Sleep(int milliseconds) - I think it's in time.h or windows.h .

  8. #38
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Quote Originally Posted by Flying Mullet
    Or convert the time to all seconds and count down against that master number.
    That would be simple, wouldn't it? Let's see what the assignment says:

    Quote Originally Posted by bbomber72000
    I need tu use a nested loop

    Quote Originally Posted by Neel With A Hat
    I think C++ has a Sleep command.
    It's Sleep(int milliseconds) - I think it's in time.h or windows.h .
    Similarly...

    Quote Originally Posted by bbomber72000
    I (...) can't use pre defined functions to count.
    That said, if you can use wait/pause/sleep, use it instead of the for {} loop I used. That's also where you would put a message that displays how much time is left if you wanted.

    And then there is Death

  9. #39
    ORANGE Dr Unne's Avatar
    Join Date
    Dec 1999
    Posts
    7,394
    Articles
    1
    Contributions
    • Former Administrator
    • Former Developer
    • Former Tech Admin

    Default

    I can't see how you could do this at all accurately without using system calls. strftime makes me happy. Even system calls aren't guaranteed to be accurate, depending on your OS's scheduler. (Though the error is likely to be so small you wouldn't notice.)

    Also note, some compilers optimze away loops that don't do anything. So if you do something like

    Code:
    for(int c=0; c<5000000000; c++)
    { }
    expecting it to take a long time, and it takes 0 seconds to run, that's probably why.

  10. #40
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    You could guesstimate a second with:
    Code:
    looptime = time()
    while time() < looptime+1000
    { i = i + 1
    }
    Then note what i was (let's call it i_max), and reuse it as:
    Code:
    while i < i_max+1 // to preserve the time used by that operation
    { i = i + 1
    }

    And then there is Death

  11. #41
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    I think what I'm doing is similiar to when you say 1 Mississippi, 2 mississippi.

    If I have nested loops, the in side loop runs first. So I make a nested loop, make it count in that one first, then go out, count 1 in the out side, and back to the inside.

    The inside loop serves to slow it down. All I need to do is tweak the number in the nested loop.

    My teacher was nice, he must have gotten laid recently.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  12. #42
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    Like so?
    tw=time waster

    EDIT: WHY WON'T THIS SoB WORK?!?
    Code:
    #include <conio.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <iostream.h>
    
    //This is the start of my program.
    int main()
    {
     //prints to the text file.
     ofstream outfile ("500.txt");
    
     //Declaring my variables.
     int hours;
     int minutes;
     int seconds;
     int tw=1000;
     cout << "This program, will function as a countdown 'clock'. Please enter your value for hours, then your value for minutes, followed by your value for seconds.\n";
     outfile << "This program, will function as a countdown 'clock'. Please enter the number you would like to countdown from.";
     cin >> hours;
     cout << "hours";
     cin >> minutes;
     cout << "minutes";
     cin >> seconds;
     cout << "seconds";
     while (hours >= 0)
       {
        while (minutes >= 0)
           {
            while (seconds > 0)
               {
                while (tw>0)
                   {
                    tw--;
    
                    if (tw==1)
                     seconds++;
                   }
                seconds = seconds - 1;
               }
            seconds=60;
            minutes=minutes - 1;
           }
        minutes = 59;
        hours = hours -1;
       }
    cout << "Time's up!";
    
     //That stuff at the end.
     getch();
     return 0;
    }
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

  13. #43
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    Because your timewaster will only run once. And if even if it ran every second, there's no need for seconds++.

    And then there is Death

  14. #44
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    You never reinitialize your time waster to 1000.

    It would probably be better to treat time waster as a constant and use a for loop and compare i against tw.
    Figaro Castle

  15. #45
    Old-Ones Studios Cruise Control's Avatar
    Join Date
    Apr 2005
    Location
    Seattle, WA
    Posts
    3,799

    Default

    XD Woops.

    EDIT: WTF I got an infinite loop.
    while (tw>0)
    {
    tw--;

    if (tw==1)
    {
    seconds--;
    cout << hours << ":" << minutes << ":" <<seconds;
    tw=tw+999;
    }
    }
    Last edited by Cruise Control; 02-28-2006 at 04:38 PM.
    Leave some shards under the belly
    Lay some grease inside my hand
    It's a sentimental jury
    And the makings of a good plan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •