Thread: Quick question on nested for loops

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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?

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    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

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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 ++)
    [edit]
    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]
    Last edited by dwks; 10-17-2007 at 02:34 PM.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    23
    endl flushes the buffer? I did not know that. sweet

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Technically name clashes can still happen, they can just only happen with the names you are using.
    true, but a lot less likely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM