# Quick question on nested for loops

• 10-17-2007
GCNDoug
Quick question on nested for loops
I'm been working with this program trying to come up with this output:

I can't get the box or zeros to appear right. I got first and last row of the box seems to get messed up and when I set the counter equal to the rows (not displayed in code) it won't seem to work. Any one got a solution. Thanks!

Drawing program
Do you want to start(Y/N)? Y
How many rows/columns(5-21)? 7

0******
*0*****
**0****
***0***
****0**
*****0*
******0

Do you want to continue(Y/N)? Y
How many rows/columns(5-21)? 55
Invalid number. Range 5 - 21. Enter again: 5

0****
*0***
**0**
***0*
****0

Do you want to continue(Y/N)? N

This is the code I've been playing around around with. Anyone know what needs to be changed I'm stumped! lol

Code:

``` #include <iostream> #include <iomanip> using namespace std; int main() {         int factorial = 0;         int held_Num = 0;         char answer = 'y';         int i = 0;         int m = 0;         char star = '*';         while (answer == 'y')         {                        cout << "Please enter a number to work with: " << endl;                 cin >> held_Num;                         while (held_Num < 5 || held_Num > 21)                         {                                 cout << "Please enter a number to work with: " << endl;                                 cin >> held_Num;                         }                 for (i = 1; i <= held_Num; i++)                 {                         cout << star << endl;                         for (m = 1; m <= held_Num; m++)                         {                                 cout << star;                                }                 }                 cout << endl;                 cout << "Would you like to continue? (y/n): " <<endl;                 cin >> answer;         }         return 0;         std::cin.get(); }//END OF MAIN```
• 10-17-2007
Daved
The first thing I see is that the first thing you do in your first for loop is output a star followed by a newline. I don't think you want a newline there in the beginning, you want a newline after the entire line has been printed out. Also, do you even need a star there at all?

You're also not displaying the '0's that are shown in the example. I think that's actually a good thing for now. Get the box to display correctly and then worry about outputting a '0' instead of a star.
• 10-17-2007
matsp
If you want to print a 0 diagonally, you obviously would need to change your star into a 0 "sometimes". You obviously need to compare something to know where to put your 0 instead of star.

Other minor things:
I also think you wanted your cin.get() to be above the return, assuming that you want that to stop the application from "going away" when it finishes.

The variable "factorial" is given a value and then never used.

I think your cout << star << endl; before the "m" loop should be replaced with a cout << star; and then a cout << endl; after the "m" loop.

--
Mats
• 10-17-2007
Daved
>> I think your cout << star << endl; before the "m" loop should be replaced with a cout << star; and then a cout << endl; after the "m" loop.

I don't think that's completely right, but I won't say why so that GCNDoug can figure it out.
• 10-17-2007
GCNDoug
I came up with this after reading the info you guys had. I just need to shift the diagonal over one.

Code:

```//************************ //Doug Miller //Project 2, Problem 3 //********************** #include <iostream> #include <iomanip> using namespace std; int main() {         int factorial = 0;         int held_Num = 0;         char answer = 'y';         int i = 0;         int m = 0;         char star = '*';         while (answer == 'y')         {                        cout << "Please enter a number to work with: " << endl;                 cin >> held_Num;                         while (held_Num < 5 || held_Num > 21)                         {                                 cout << "Please enter a number to work with: " << endl;                                 cin >> held_Num;                                 cout << "\n\n";                         }                 for (i = 1; i <= held_Num; i++)                 {                         cout << star;                         for (m = 1; m <= held_Num; m++)                         {                                 if (m == i)                                 {                                         cout << "0";                                 }                                 else                                 {                                 cout << star;                                        }                         }                         cout << endl;                 }                 cout << endl;                 cout << "Would you like to continue? (y/n): " <<endl;                 cin >> answer;         }         std::cin.get();         return 0; }//END OF MAIN```
• 10-17-2007
Daved
I think it's a simple fix. Count how many columns are being output when you run the program. Does it match the number you input?

I'm guessing it is one too many. Where do you think that extra one is coming from?
• 10-17-2007
GCNDoug
got it thanks!

//************************
//Doug Miller
//Project 2, Problem 3
//**********************

Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {         int factorial = 0;         int held_Num = 0;         char answer = 'y';         int i = 0;         int m = 0;         char star = '*';         while (answer == 'y')         {                        cout << "Please enter a number to work with: " << endl;                 cin >> held_Num;                 cout << "\n";                         while (held_Num < 5 || held_Num > 21)                         {                                 cout << "Please enter a number to work with: " << endl;                                 cin >> held_Num;                                 cout << "\n";                         }                 for (i = 1; i <= held_Num; i++)                 {                                                        for (m = 1; m <= held_Num; m++)                         {                                 if (m == i)                                 {                                         cout << "0";                                 }                                 else                                 {                                         cout << star;                                        }                         }                         cout << endl;                 }                 cout << endl;                 cout << "Would you like to continue? (y/n): " <<endl;                 cin >> answer;         }         std::cin.get();         return 0; }//END OF MAIN```
• 10-17-2007
Daved
Nice job. Now that it's working, consider a few minor improvements:
• Your prompts don't exactly match the example, I don't know if that matters for the assignment or not, but you might consider getting them to match.
• Your indentation isn't exactly consistent. I would fix that up to make it easier to read the code. I think the only real problem is the second while loop.
• "0" is a string, '0' is a character. Either will work, but if you wanted to be consistent you could use '0' instead of "0".
• You declare all your variables at the top of the function. This is done by a lot of people, but it is not good practice in C++. In general you should declare and initialize your variables only when you need them and in the smallest scope possible.
• What if the user enters a letter instead of a number when you ask for the number? The program will likely go all screwy. Fixing that might be beyond the scope of your assignemnt, but if you'd like to do it search for the way to handle that or ask here. I post my favorite solution all the time so you might just be able to find it.
• 10-17-2007
dwks
Code:

`for (i = 1; i <= held_Num; i++)`
You should also be aware that standard practice in C and C++ is to loop from 0 to N-1, inclusive, or [0,N). This loops the same number of times, but many programmers are more familiar with it and so would find it easier to read.
Code:

`for (i = 0; i < held_Num; i ++)`

Code:

`std::cin.get();`
You can drop the "std::" since you have a using namespace std statement. Or, alternatively, you could add std:: everywhere else and drop the using directive. :)

Code:

```cout << "\n"; cout << endl;```
Those are pretty much the same thing. The only difference is that endl flushes the buffer as well, but this doesn't matter to you because using cin also flushes the buffer. So either one works. But you might want to consider being consistent. [/edit]
• 10-17-2007
cyberfish
Quote:

Or, alternatively, you could add std:: everywhere else and drop the using directive.
or, you could just import the things you need into the global namespace with eg.
Code:

```using std::cout; using std::endl; using std::cin; ...```
It's a bit more code, but that way you don't need to worry about name clashes, and u can reference those symbols without "std::". This is my favourite method.
• 10-17-2007
Daved
>> but that way you don't need to worry about name clashes, and u can reference those symbols without "std::".
Technically name clashes can still happen, they can just only happen with the names you are using.
• 10-17-2007
avatarofhope2
endl flushes the buffer? I did not know that. sweet :)
• 10-17-2007
cyberfish
Quote:

Technically name clashes can still happen, they can just only happen with the names you are using.
true, but a lot less likely.