I know you cant do my work for me but could someone give me some guidance as to how I should approach this to get me going. Im going to need the For loop but Im not sure how?

Question-
Write a function that displays at the left margin of the screen a solid square of asterisks whose
side is specified in integer parameter side. For example, if side is 4, the function displays:
Sample Screen Display
****
****
****
****

Recommended Answers

All 7 Replies

Use a loop within a loop----a process often called nested loops. The outer loop controls the row and the inner loop controls the column. The body of the inner loop is where the output statement occurs.

Thankyou

At the moment I am getting

*
*
*
*

Will the for statement that makes that be in the inner loop then?

Havent a clue what to put in my outer loop

Well, what does your code look like as of right now?

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
 
int main ()
{

int size;
    cout<<"Please enter the size of the width of a square: " <<endl;
    cin >> size;

	for(int i=0;i<size;i++)
    
	{

    cout<<'*'<<endl;
    
	}
}

Write code that will put 4 asterisks on the same line. (Almost there.)

Then write code that will put any requested number of asterisks on the same line. (Should be able to use current code with above correction if you want).

Then write code that will put any given number of asterisks on the same line over and over again, as many times as it's told.

Blahh thats confused me more. Ill knock it on the head and leave it, can't get me head round c++

If it were easy, anyone could do it. It's not that hard if you follow it step by step as previously indicated.

//This says put an asterix on a line size times
 for(int i=0; i<size; i++)
 {
  cout<< '*' << endl;
 }
} 

//This says put size number of asterixes on one line
for(int i=0; i<size; i++)
 {
  cout<<'*';
 }
 cout << endl;
} 

//If you complete the blank line in the following  snippet correctly it will put size asterixes on a line and repeat it for size lines

/*put code in the blank line immediately below this and before the first { to control how many lines are written*/

 {
     //this line controls how many asterixes per line
     for(in i = 0; i < size; ++i) 
    {
       cout<<'*';
    }
    //this starts a new line
    cout << endl;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.