Simple program problem.

This is a discussion on Simple program problem. within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a hollow box. I have searched the forums and found the answers. But the answers ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Simple program problem.

    I am trying to make a hollow box. I have searched the forums and found the answers. But the answers do not help me understand how it works. I need help in the form of hints on how to make hollow squares. I am starting to learn c++ and I can't seem to get by the basics. Anywho....here is what I have so far.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
    
          int row,col,rowcounter,colcounter,x,z;
          cout <<"How many rows?" <<endl;
          cin >>row;
          cout <<"How many columns?"<<endl;
          cin >>col;
    
          for (rowcounter=1; rowcounter <= row; ++rowcounter)
              {
              cout <<"* ";
              }
              cout <<endl;
              for (colcounter=2; colcounter <= col; ++colcounter)
              {
              cout <<"*" <<endl;
              }
    
              cout <<endl;
    
    
    
          cin >> z;
          return 0;
    }
    I think I am on the right track but I am missing something. I am re-reading the chapter (C++ How to Program: Deitel) and gonna keep trying to do this on my own.....Please don't just post an answer.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    think about it fusion...you need to make a hollow box yet you're not printing any ('' ") empty spaces.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If I understand you correctly, you want the box to be hollow. So just the border should have *'s. I think also, you have rows and columns backwards.

    Think about the problem. There are two common approaches to this. Iterate through the rows performing some action, or iterate through the columns performing some action. Let's say you go through it row by row. The first row is going to have asterik's all the way down because it is the top of your box. That was easy. The second row will have an asterik in first position (the left side) and in position of the last column (the right side). So there lies the problem. You need to print out an asterik, skip some spaces, then print out another. You said not to give you the answer so I'll let you look for how to do that on your own. The bottom of the box is identical the top of the box. Good luck and let us know if you have any problems.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Ken gives a decent explanation here: Box Drawing Program

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    4
    Heh completely unrelated but I saw your location axon. My dad's office is in Mt. Prospect

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253

    Another problem

    Well I come across another problem. Here is what I have:
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
          int x, y, row, col, end;
    
          cout <<"Enter amount of rows.\n";
          cin >>row;
          cout <<"Enter amount of coloumns.\n";
          cin >>col;
    
          for (x=1; x <=row; ++x)
          {
          cout <<"* ";
          }
          cout <<endl;
    
    
          for (y=1; y <=col; ++y)
          {
          cout <<"*";
               for (x=0; x<=row; ++x)       // Middle rows
               {
               cout <<" ";
               }
               cout <<" *" <<endl;
           }
    
           for (y=1;y<=row; ++y)
           {
           cout <<"* ";                          // Bottom row
           }
          cin >>end;
          return 0;
    }
    I am off by 2 spaces. Why?
    Last edited by RealityFusion; 07-20-2004 at 12:16 AM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because you're printing "* " and " *" when you should only be printing the "*".

    While I'm here, what's up with your for loops? Noone starts at 1. That's just ugly.

    [edit]
    Code:
    // top row
    for( x = 0; x < width; x++ )
        cout << "*";
    cout << endl;
    for( y = 0; y < rows; y++ )
    {
        cout << "*"; // left edge
        for( x = 0; x < width-2; x++ );
            cout << " "; // filler
        cout << "*" << endl; //end the line
    }
    // bottom row
    for( x = 0; x < width; x++ )
        cout << "*";
    cout << endl;
    Is that what you were trying for?
    [/edit]

    Quzah.
    Last edited by quzah; 07-20-2004 at 12:48 AM.
    Hope is the first step on the road to disappointment.

  8. #8
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok, its done and works like its supposed to now . I am glad that, not only did I figure out why my code was wrong, but I figured out how to make it work. It took me a while to realize how nested for loops work. It all makes sense now. When for loops are nested it completed the entire loop inside the loop.

    So when I was wanting to make the middle I had to realize that I needed the for loop for the spaces to run inside the loop that made the colunms to fill in the spaces. Thanks for everyone that helped.
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 03:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 06:57 PM

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