How to number the output lines? - Programmers Heaven

Howdy, Stranger!

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

Categories

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.