Conceptually simple & driving me insane

This is a discussion on Conceptually simple & driving me insane within the C++ Programming forums, part of the General Programming Boards category; Ok maybe I've had too much coffee today, but I can't see why this: Code: unsigned c = 0; for ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Conceptually simple & driving me insane

    Ok maybe I've had too much coffee today, but I can't see why this:

    Code:
    unsigned c = 0;
    
    for (size_t i = 0; i < sz; i++, c++) {
        cout << " " << *(b + i);
            if (c == 2) {
                cout << "\n";
                c = 0;
            }
    }
    Doesn't output how I want it to. It's a snippet from one of my debugging functions, to output the program as it appears in memory. I was fine having it all dumped on one line spaced out until I started writing larger test programs. Basically I want it in this format:

    Code:
     opcode operand1 operand2
     opcode operand1 operand2
     ...
    So that's a newline every 3 items, right? Then why do I get this output?

    Code:
    *** DEBUG [PROGRAM DISASSEMBLY] ***
    
     3 9 61442
     4 61442
     0 65535
     0 0
    
    
    *** END ***
    
    *** DEBUG [REGISTERS] ***
    
            R0: 9
            R1: 0
            PC: 4
             Z: 0
    
    *** END ***
    
    *** DEBUG [REGISTERS] ***
    
            R0: 10
            R1: 0
            PC: 65535
             Z: 0
    
    *** END ***
    Press any key to continue
    The first line is fine, then it all goes haywire. The 0 on the 3rd line should be at the end of the 2nd line and, well, you get the idea.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Because you set c to 0 inside the loop, and when the loop goes back to the top it gets incremented, so when the first item in the second is printed, c is 1 already. It doesn't happen the first time because the c increment is not done when the loop starts, only at the end of the loop.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Got it working:

    Code:
    void CVM::DebugBlock(word* b, size_t sz, bool fileDump, char* blockName)
    {
        if (! fileDump) {
            unsigned c = 0;
            bool first = true;
    
            cout << "\n*** DEBUG [" << blockName << "] ***\n\n";
            for (size_t i = 0; i < sz; i++, c++) {
                cout << " " << *(b + i);
                if (first) {
                    if (c == 2) {
                        cout << "\n";
                        c = 0;
                        first = false;
                    }
                } else {
                    if (c == 3) {
                        cout << "\n";
                        c = 0;
                    }
                }
            }
    
            cout << "\n\n*** END ***" << endl;
        } else {
            FILE* f = fopen("DebugBlock.txt", "a");
    
            for (size_t i = 0; i < sz; i++)
                fputc(*(b + i), f);
    
            fclose(f);
        }
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Or you could just start c off at 1 and print the newline at 3.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could make it a bit simpler and do something like this (note this code will print a newline when i=0, before anything is outputted):
    Code:
      for (int i=0;i<num_elements;i++)
      {
        if((i%3)==0)
          std::cout<<"\n";
        std::cout<<array[i]<<' ';
      }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    (note this code will print a newline when i=0, before anything is outputted)
    If you don't want that to happen, change this
    Code:
    if((i%3)==0)
    to
    Code:
    if((i%3)==0 && i)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple driving game
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 07-06-2004, 10:34 AM
  2. JAVA issue driving me insane
    By axon in forum Tech Board
    Replies: 0
    Last Post: 02-23-2004, 09:19 PM
  3. VC++ tutorial driving me insane!
    By Swaine777 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 08:35 AM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 02:57 PM
  5. Linked Lists - Driving me insane
    By Steven Snell in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 10:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21