Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. numbers with boxes

numbers with boxes

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
19 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kbury

    I have this code and cannot seem to ge the numbers to print out to 20 within the boxes. Any help would be appreciated. Thanks Suppose to be this: +---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+ | 6 | 7 | 8 | 9 | 10| +---+---+---+---+---+ | 11| 12| 13| 14| 15| +---+---+---+---+---+ | 16| 17| 18| 19| 20| +---+---+---+---+---+ Here is what I get: How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20

    <#define mbxhgt 6
    #define mbxwid 4

    int main (void)
    {
    int rows,
    cols,
    r = 1,
    c = 1,
    boxnum = 1,
    unit_height,
    unit_width;

    system("cls");

    printf ("Mailbox Layout Program\n\n");

    printf ("How many rows of mailboxes will there be?");
    scanf ("%d", &rows);

    printf ("\nHow many columns of mailboxes will there be?");
    scanf ("%d", &cols);

    printf ("\n\nThe numbering layout will be:\n");
    printf("+---+---+---+---+---+\n|");

    while (c <= cols)

    {
    c = c + 1;
    printf (" %d |", boxnum);
    boxnum = boxnum + 1;
    c = c++;
    }

    while (r <= rows)
    {
    r = r + 1;
    printf ("\n \n");
    boxnum = boxnum + 1;
    r = r++;
    }

    unit_height = rows * mbxhgt;
    unit_width = cols * mbxwid;

    printf ("\nOverall height of mailbox unit: %d", unit_height);

    printf ("\nOverall width of mailbox unit: %d\n", unit_width);

    return (0);
    /pre>

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    See here. The two while() loops are separate. One needs to be nested inside the other, like:

    int number = 1;

    printf("+---+---+---+---+---+\n");

    for (int row = 0; ...)
    {
    for (int col = 0; ...)
    {
    printf("| %2d", number);
    number++:
    }

    printf("|\\n+---+---+---+---+---+\\n");
    

    }

    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    K 1 Reply Last reply
    0
    • D David Crow

      See here. The two while() loops are separate. One needs to be nested inside the other, like:

      int number = 1;

      printf("+---+---+---+---+---+\n");

      for (int row = 0; ...)
      {
      for (int col = 0; ...)
      {
      printf("| %2d", number);
      number++:
      }

      printf("|\\n+---+---+---+---+---+\\n");
      

      }

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      K Offline
      K Offline
      kbury
      wrote on last edited by
      #3

      I do not understand. I tried that but got error messages.

      D 1 Reply Last reply
      0
      • K kbury

        I do not understand. I tried that but got error messages.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        kbury wrote:

        I tried that...

        Tried what?

        kbury wrote:

        ...but got error messages.

        Which ones?

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        K 1 Reply Last reply
        0
        • D David Crow

          kbury wrote:

          I tried that...

          Tried what?

          kbury wrote:

          ...but got error messages.

          Which ones?

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          K Offline
          K Offline
          kbury
          wrote on last edited by
          #5

          nesting, but maybe i did it wrong, because it came out with this Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20

          D 1 Reply Last reply
          0
          • K kbury

            nesting, but maybe i did it wrong, because it came out with this Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            Your while() loops are backwards. Think about drawing this figure on paper. You start with the first column of the first row, then the second column of the first row, etc. After the last column, you go to the next row (i.e., first column of the second row). You've not changed the inner printf() statement to use a width specifier. Did you not read the link I offered? It should look like:

            printf("| %2d", number);

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            K 1 Reply Last reply
            0
            • D David Crow

              Your while() loops are backwards. Think about drawing this figure on paper. You start with the first column of the first row, then the second column of the first row, etc. After the last column, you go to the next row (i.e., first column of the second row). You've not changed the inner printf() statement to use a width specifier. Did you not read the link I offered? It should look like:

              printf("| %2d", number);

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              K Offline
              K Offline
              kbury
              wrote on last edited by
              #7

              I just cannot seem to get my head wrapped around this for some reason. here is what I changed.

              {
              for (int r = 0;r <= rows; r++);

              {
              r = r + 1;
              printf (" \n");
              boxnum = boxnum + 1;
              r=r++;
              for (int c=0; c <= cols; c++);
              {

              c = c + 1;
              printf (" %2d|", boxnum);
              boxnum = boxnum + 1;
              c = c++;

              }

              unit_height = rows * mbxhgt;
              unit_width = cols * mbxwid;

              printf ("\nOverall height of mailbox unit: %d", unit_height);

              printf ("\nOverall width of mailbox unit: %d\n", unit_width);

              return (0);
              }

              }}

              D 1 Reply Last reply
              0
              • K kbury

                I just cannot seem to get my head wrapped around this for some reason. here is what I changed.

                {
                for (int r = 0;r <= rows; r++);

                {
                r = r + 1;
                printf (" \n");
                boxnum = boxnum + 1;
                r=r++;
                for (int c=0; c <= cols; c++);
                {

                c = c + 1;
                printf (" %2d|", boxnum);
                boxnum = boxnum + 1;
                c = c++;

                }

                unit_height = rows * mbxhgt;
                unit_width = cols * mbxwid;

                printf ("\nOverall height of mailbox unit: %d", unit_height);

                printf ("\nOverall width of mailbox unit: %d\n", unit_width);

                return (0);
                }

                }}

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                kbury wrote:

                for (int r = 0;r <= rows; r++); { r = r + 1; ... r=r++;

                How many times does r (and c) need to be incremented? Also, that rogue semicolon is sure to cause you grief. There's a problem with boxnum, but it does not affect the box-drawing. I'll withold the answer until you've studied it more.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                K 1 Reply Last reply
                0
                • D David Crow

                  kbury wrote:

                  for (int r = 0;r <= rows; r++); { r = r + 1; ... r=r++;

                  How many times does r (and c) need to be incremented? Also, that rogue semicolon is sure to cause you grief. There's a problem with boxnum, but it does not affect the box-drawing. I'll withold the answer until you've studied it more.

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  K Offline
                  K Offline
                  kbury
                  wrote on last edited by
                  #9

                  changed code to this

                  <#include <stdio.h> /*Include header*/
                  #include <stdlib.h>
                  #include <assert.h>

                  #define mbxhgt 6
                  #define mbxwid 4

                  int main (void)
                  {
                  int rows,
                  cols,
                  r = 1,
                  c = 1,
                  boxnum = 1,
                  unit_height,
                  unit_width;

                  system("cls");

                  printf ("Mailbox Layout Program\n\n");

                  printf ("How many rows of mailboxes will there be?");
                  scanf ("%d", &rows);

                  printf ("\nHow many columns of mailboxes will there be?");
                  scanf ("%d", &cols);

                  printf ("\n\nThe numbering layout will be:\n");
                  printf("+---+---+---+---+---+\n|");

                  while (c <= cols)

                  {c = c + 1;
                  
                  printf (" %d |", boxnum);
                  boxnum = boxnum + 1;
                  }
                  

                  while (r <= rows)
                  {r = r + 1;

                  printf ("\\n \\n");
                  

                  if (boxnum <= 1);
                  printf("| %d | ", boxnum++);}

                  unit_height = rows * mbxhgt;
                  unit_width = cols * mbxwid;

                  printf ("\nOverall height of mailbox unit: %d", unit_height);
                  printf ("\nOverall width of mailbox unit: %d\n", unit_width);

                  return (0);
                  }/pre>

                  now looks like this:

                  How many rows of mailboxes will there be?4

                  How many columns of mailboxes will there be?5

                  The numbering layout will be:
                  +---+---+---+---+---+
                  | 1 | 2 | 3 | 4 | 5 |

                  | 6 |

                  | 7 |

                  | 8 |

                  | 9 |
                  Overall height of mailbox unit: 24
                  Overall width of mailbox unit: 20

                  D 1 Reply Last reply
                  0
                  • K kbury

                    changed code to this

                    <#include <stdio.h> /*Include header*/
                    #include <stdlib.h>
                    #include <assert.h>

                    #define mbxhgt 6
                    #define mbxwid 4

                    int main (void)
                    {
                    int rows,
                    cols,
                    r = 1,
                    c = 1,
                    boxnum = 1,
                    unit_height,
                    unit_width;

                    system("cls");

                    printf ("Mailbox Layout Program\n\n");

                    printf ("How many rows of mailboxes will there be?");
                    scanf ("%d", &rows);

                    printf ("\nHow many columns of mailboxes will there be?");
                    scanf ("%d", &cols);

                    printf ("\n\nThe numbering layout will be:\n");
                    printf("+---+---+---+---+---+\n|");

                    while (c <= cols)

                    {c = c + 1;
                    
                    printf (" %d |", boxnum);
                    boxnum = boxnum + 1;
                    }
                    

                    while (r <= rows)
                    {r = r + 1;

                    printf ("\\n \\n");
                    

                    if (boxnum <= 1);
                    printf("| %d | ", boxnum++);}

                    unit_height = rows * mbxhgt;
                    unit_width = cols * mbxwid;

                    printf ("\nOverall height of mailbox unit: %d", unit_height);
                    printf ("\nOverall width of mailbox unit: %d\n", unit_width);

                    return (0);
                    }/pre>

                    now looks like this:

                    How many rows of mailboxes will there be?4

                    How many columns of mailboxes will there be?5

                    The numbering layout will be:
                    +---+---+---+---+---+
                    | 1 | 2 | 3 | 4 | 5 |

                    | 6 |

                    | 7 |

                    | 8 |

                    | 9 |
                    Overall height of mailbox unit: 24
                    Overall width of mailbox unit: 20

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.

                    kbury wrote:

                    if (boxnum <= 1);

                    Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    K 2 Replies Last reply
                    0
                    • D David Crow

                      You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.

                      kbury wrote:

                      if (boxnum <= 1);

                      Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      K Offline
                      K Offline
                      kbury
                      wrote on last edited by
                      #11

                      I guess a gluton for punishment. I am new at this and am trying to learn from a online class, which seems to make this more difficult than I had expected.

                      D 1 Reply Last reply
                      0
                      • K kbury

                        I guess a gluton for punishment. I am new at this and am trying to learn from a online class, which seems to make this more difficult than I had expected.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        What's difficult about comparing your code to mine? :confused:

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        1 Reply Last reply
                        0
                        • D David Crow

                          You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.

                          kbury wrote:

                          if (boxnum <= 1);

                          Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.

                          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          K Offline
                          K Offline
                          kbury
                          wrote on last edited by
                          #13

                          I am using the LCC Win 32 compiler and when I remove the semicolon away from if (boxnum <= 1); It does not print out the 6, 7, 8, 9 at all.

                          K 1 Reply Last reply
                          0
                          • K kbury

                            I am using the LCC Win 32 compiler and when I remove the semicolon away from if (boxnum <= 1); It does not print out the 6, 7, 8, 9 at all.

                            K Offline
                            K Offline
                            kbury
                            wrote on last edited by
                            #14

                            anyways I changed the code and now it does this. Do i need to change the if boxnum statement?

                            printf ("\n\nThe numbering layout will be:\n");

                            while (r <= rows)

                            	{
                            
                            	r = r + 1;
                            	printf ("\\n \\n");
                            

                            while (c <= cols)

                            c = c + 1;
                            printf (" %d |", boxnum);
                            boxnum = boxnum + 1;
                            
                            
                            if (boxnum <= 1);
                            
                            printf(" %d | ", boxnum++);
                            
                            }
                            
                            unit\_height = rows \* mbxhgt;
                            unit\_width = cols \* mbxwid;
                            
                            printf ("\\nOverall height of mailbox unit: %d", unit\_height);
                            printf ("\\nOverall width of mailbox unit: %d\\n", unit\_width);
                            

                            return (0);
                            }

                            Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20

                            D 1 Reply Last reply
                            0
                            • K kbury

                              anyways I changed the code and now it does this. Do i need to change the if boxnum statement?

                              printf ("\n\nThe numbering layout will be:\n");

                              while (r <= rows)

                              	{
                              
                              	r = r + 1;
                              	printf ("\\n \\n");
                              

                              while (c <= cols)

                              c = c + 1;
                              printf (" %d |", boxnum);
                              boxnum = boxnum + 1;
                              
                              
                              if (boxnum <= 1);
                              
                              printf(" %d | ", boxnum++);
                              
                              }
                              
                              unit\_height = rows \* mbxhgt;
                              unit\_width = cols \* mbxwid;
                              
                              printf ("\\nOverall height of mailbox unit: %d", unit\_height);
                              printf ("\\nOverall width of mailbox unit: %d\\n", unit\_width);
                              

                              return (0);
                              }

                              Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #15

                              kbury wrote:

                              while (c <= cols)

                              For all but the first row, when will c ever be LE cols again? Furthermore, without braces for the inner while() loop, what statements do you expect will be executed?

                              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              K 1 Reply Last reply
                              0
                              • D David Crow

                                kbury wrote:

                                while (c <= cols)

                                For all but the first row, when will c ever be LE cols again? Furthermore, without braces for the inner while() loop, what statements do you expect will be executed?

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                K Offline
                                K Offline
                                kbury
                                wrote on last edited by
                                #16

                                I am trying to stick to this flowchart given to me. Maybe I just do not understand it. In the flowchart it is only called where I have it in the code and after boxnum it is suppose to go back to col.

                                D 1 Reply Last reply
                                0
                                • K kbury

                                  I am trying to stick to this flowchart given to me. Maybe I just do not understand it. In the flowchart it is only called where I have it in the code and after boxnum it is suppose to go back to col.

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #17

                                  Counting this one, I've posted 8 times to this thread, which was 7 times too many. I showed you the answer in my first post. You continue to use your code, which I do not have a problem with, but you still refuse to make the necessary changes or even compare the two to see why they behave differently. I'm confused as to why a person would do this.

                                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                  K 1 Reply Last reply
                                  0
                                  • D David Crow

                                    Counting this one, I've posted 8 times to this thread, which was 7 times too many. I showed you the answer in my first post. You continue to use your code, which I do not have a problem with, but you still refuse to make the necessary changes or even compare the two to see why they behave differently. I'm confused as to why a person would do this.

                                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                    K Offline
                                    K Offline
                                    kbury
                                    wrote on last edited by
                                    #18

                                    Thanks for your help. I tried to code it just like you had in the 1st answer, but my compiler would not compile it.

                                    D 1 Reply Last reply
                                    0
                                    • K kbury

                                      Thanks for your help. I tried to code it just like you had in the 1st answer, but my compiler would not compile it.

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #19

                                      kbury wrote:

                                      I tried to code it just like you had in the 1st answer...

                                      Using copy & paste?

                                      kbury wrote:

                                      ...but my compiler would not compile it.

                                      It was standard C code. What error(s) did you receive?

                                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups