Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to number the output lines?

tunefoot3tunefoot3 Posts: 2Member
[b][red]This message was edited by tunefoot3 at 2007-3-8 10:24:39[/red][/b][hr]
Hello,
I have a project due in my C++ class and I can not figure out how to number the outputs. Maybe somebody can help me?
The output should just list each "step" (as in the case of my program) from 1 to like 1000. I'm sure it's a fairly simple code, but I can't find any info on it.

It *should* look something like this ~ (example)

(step)1 - Move one from box one to box two
(step)2 - Move one from box two to box one
(step)3 - Move two from box one to box two
(step)4 - Move two from box two to box one
etc....

Thanks in advance for any help!
---------------------------------------------------------


Comments

  • MT2002MT2002 Posts: 1,444Member
    : Hello,
    : I have a project due in my C++ class and I can not figure out how to number the outputs. Maybe somebody can help me?
    : The output should just list each "step" (as in the case of my program) from 1 to like 1000. I'm sure it's a fairly simple code, but I can't find any info on it.
    :
    : It *should* look something like this ~ (example)
    :
    : (step)1 - Move one from box one to box two
    : (step)2 - Move one from box two to box one
    : (step)3 - Move two from box one to box two
    : (step)4 - Move two from box two to box one
    : etc....
    :
    : Here is the code if that wasn't a good enough explanation. Thanks in advance for any help!
    : ---------------------------------------------------------
    :
    : #include
    :
    : void Hanoi(int, int, int, int);
    :
    : void main()
    : {
    : int NumberOfDisks;
    :
    : cout << "How many disks?" << endl;
    : cin >> NumberOfDisks;
    :
    : Hanoi(1, 2, 3, NumberOfDisks);
    :
    : int a;
    : cin >> a;
    : }
    : void Hanoi(int fromPost, int toPost, int extraPost, int NumOfDisks)
    : {
    : if(NumOfDisks == 1)
    : cout << "Move a disk from post " << fromPost
    : << " to post " << toPost << endl;
    : else
    : {
    : Hanoi(fromPost, extraPost, toPost, NumOfDisks - 1);
    : cout << "Move a disk from post " << fromPost
    : << " to post " << toPost << endl;
    : Hanoi(extraPost, toPost, fromPost, NumOfDisks - 1);
    : }
    : }
    :
    [blue]
    I made some changes to your code to make it more ANSI compilent
    with C++ standards. (Included comments).

    Im assuming you want it like this?:[/blue][code]
    //#include <iostream.h> // old iostream header--dont use
    #include //<<Use this header instead
    using namespace std; // cout, cin, etc are part of std namespace

    void Hanoi(int, int, int, int);


    //void main() // For applications, ANSI states main must return int
    int main ()
    {
    int NumberOfDisks;

    cout << "How many disks?" << endl;
    cin >> NumberOfDisks;

    Hanoi(1, 2, 3, NumberOfDisks);

    int a=cin.get(); //cleaner method imo :)
    cin.get();

    //cin >> a;


    return 0; //return error code back to OS
    }

    void Hanoi(int fromPost, int toPost, int extraPost, int NumOfDisks)
    {
    static int iCurStep=1;

    if(NumOfDisks == 1)
    cout << "(step)" << iCurStep++ << " -Move a disk from post " << fromPost
    << " to post " << toPost << endl;
    else
    {
    Hanoi(fromPost, extraPost, toPost, NumOfDisks - 1);
    cout << "(step)" << iCurStep++ << " -Move a disk from post " << fromPost
    << " to post " << toPost << endl;
    Hanoi(extraPost, toPost, fromPost, NumOfDisks - 1);

    //..Should check and insure iCurStep does not overflow by comparing with max int value possible in system
    // It wouldnt cause problems, itll just wrap back to 0 if it overflows. ...Cleaner design.
    }
    }
    [/code]
  • tunefoot3tunefoot3 Posts: 2Member
    It works great! Thanks, I really appreciate it!
Sign In or Register to comment.