For Loops (Making Stars)

This is a discussion on For Loops (Making Stars) within the C++ Programming forums, part of the General Programming Boards category; I'm working on this program for school and I need the output to look like this * ** *** **** ...

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

    For Loops (Making Stars)

    I'm working on this program for school and I need the output to look like this

    *
    **
    ***
    ****
    *****

    This is the code I have so far. I can't seem to make it work and honestly I can't understand the logic of the for statement for this. Could someone please walk me through the for statement?

    Code:
     //************************
    //Doug Miller
    //Lab 6_1
    //Shape Program
    //**********************
    
    #include <iostream>
    using namespace std;
    
    
    int main ()
    {
    	int row_Col = 0;
    	int i = 0;
    	int m = 0;
    	int n = 0;
    	bool start = 0;
    	char answer;
    
    	const char star = '*';
    
    	cout << "Drawing hallow boxes program " << endl;
    	cout << "\n\n";
    	
    	cout << "Do you want to start (y/n): " << endl;
    	cin >> answer;
    	cout << "\n\n";
    
    	
    	if (answer == 'y' || answer == 'Y')
    	{
    		start = true;
    	}
    	else if (answer == 'n' || answer == 'N')
    	{	
    		start = false;
    	}
    	else 
    	{
    		cout << "You entered an invalid character" <<endl;
    	}
    
    	while (start == true)	
    	{	
    
    		cout << "How many rows/columns would you like to work with? " << endl;
    		cin >> row_Col;
    		cout << "\n\n";
    
    		while (row_Col < 5 || row_Col > 21)
    		{
    			cout << "Out of range!  Reenter: " << endl;
    			cout << "\n\n";
    			cin >> row_Col;
    		}
    
    		for (i = 1; i <= row_Col; i++)
    		{
    			for(m = 0; m < row_Col; m=m++)
    			{
    				cout << star;
    			}
    
    		}
    			
    
    		cout << "Do you want to start (y/n): " << endl;
    		cin >> answer;
    		cout << "\n\n";
    		
    		if (answer == 'n' || answer == 'N')
    		{
    			start = false;
    		}
    	}
    
    
    
    	return 0;
    	std::cin.get();
    
    }// End of program!

  2. #2
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    The first for statement seems right, that's looping through all the rows, right? The second, for(m = 0; m < row_Col; m=m++), why do you always go from 0 to row_Col? Do you want to create a square? Every row will get equally many stars. And another thing, you got to output line breaks somewhere, else evething will be written on a single line. If you haven't noticed already.

    -Kristofer
    Come on, you can do it! b( ~_')

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Just a suggestion - create a string and appent a '*' to it every iteration of the for loop -

    Code:
    #include <string>
    
    /* etc */
    
    int main( void ) {
      std::string stars; // Or you could use a char* and strcat if you can't use this type
    
      for ( int i=0; i<4; i++ ) {
        stars += "*";
        std::cout<< stars << "\n";
      }
    
      return 0;
    }
    Anyways. Consider the loop above there. What that does is it creates an integer variable called i. i is assigned the value 0 at the start and the for loop will execute while the value of i is less than four (i<4), and every time what's between the { ... }'s executes the value of i is incremented (one is added to it). You can think of it like:

    for ( VALUE; CONDITION; OPERATOR )

    where operator can increment, decrement or really do anything you want.
    Last edited by twomers; 10-04-2007 at 05:01 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I understand how the for statement works I guess I'm just having problems because the nesting is confusing me. I can't use your solution because it has to be solved with a nested for loop. I don't think I can use that string thing. That seems too easy lol.

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by GCNDoug View Post
    I understand how the for statement works I guess I'm just having problems because the nesting is confusing me. I can't use your solution because it has to be solved with a nested for loop. I don't think I can use that string thing. That seems too easy lol.
    The outer for loop runs, for each loop it does everything that is inside the loop which is (among other when your program looks correct) running the inner for loop. And the inner for loop will loop and run until it has finished writing the current row. When it has, the the outer for loop will make a loop and the inner for loop is run again. The inner for loop writes the current row that's why you got to make a line break after each time the inner for loop is run.
    Come on, you can do it! b( ~_')

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > for(m = 0; m < row_Col; m=m++)
    Just m++ (like you do i++ in the other loop).
    The m= will mess you up at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to make stars mostly done, need help?
    By patso in forum C Programming
    Replies: 4
    Last Post: 04-07-2008, 11:45 PM
  2. Making a pyramid of stars help
    By g1bber in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2006, 09:03 PM
  3. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  4. Replies: 2
    Last Post: 01-13-2003, 01:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 08:37 AM

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