Help with nested loop

I need to make a user defined size ( assume odd number) rectangle with a cross in the middle to be outputted. RIght now it just spams the screen with

. . . . .
. . . . .
. . . . .
and carries on doing this.


What i want is

. . * . .
. . * . .
*****
. . * . .
. . * . .

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>


int main()
{
int numRows;
int numCols;

cout << "Please enter the Number of Rows \n";
cin >> numRows;

cout << "Please enter the number of Columms \n";
cin >> numCols;



for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
//output row and colum positions
if (r = (numRows / 2) + 0.5 || (numCols / 2) +0.5)
{
cout << ". ";
}
else
{
cout << "* ";
}

}
cout << endl;
}

getchar();

return (0);
}
Last edited on
I'm a beginner too, however you're missing your {} on your if and else statements. Also you should only have one else statement and all the rest (after the first if) should 'else if' statements. Not sure if that is your problem but it's definitely something that needs fixed.
Doh. Thanks :) Ill see if i can get it working now
Hmm still having problems, ive adjusted the code to fix the double else and missing the {} but it still just keeps on spamming down the screen
= is assignment
== is comparison
Thank You !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	
for (int r = 0; r < numRows; r++)
	{
		for (int c = 0; c < numCols; c++)
		{
			//output row and colum positions
			if (r == numRows / 2  || c == numCols /2)
			{
				cout << ". ";
			}
			else
			{
				cout << "@ ";
			}
			
		}
		cout << endl;
		


Last edited on
Topic archived. No new replies allowed.