Dec 29, 2018 at 9:25pm Dec 29, 2018 at 9:25pm UTC
I am writing some code such that:
1) you input the length of the side of a square and what you want to repeat
2) it then outputs the border of the square
my problem is that this code only outputs the first and last row but no the middle. I know that this might be because it is existing the for loop after the if condition or it just never gets to the else ?
I would appreciate any help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <string>
int main(){
int n;
std::string c;
std::cout << " please enter a number and a character" << std::endl;
std::cin >> n >> c;
for (int i=0; i<n; i++){
if (i==0 || i==(n-1)){
for (int j = 0; j<n; j++){
std::cout << c << " " ;
}
std::cout << std::endl;
}
else {
for (int j=0; j==0 && j==(n-1); j++){
std::cout << c << " " ;
}
std::cout << std::endl;
}
}
return 0;
}
Last edited on Dec 29, 2018 at 9:43pm Dec 29, 2018 at 9:43pm UTC
Dec 29, 2018 at 10:31pm Dec 29, 2018 at 10:31pm UTC
You need to have a long hard look at the things you expect j to be equal to (simultaneously) on line 24.
When that is fixed, you need to think how you will output one c, followed by n-2 blanks, followed by another c in this section.
Dec 30, 2018 at 7:24am Dec 30, 2018 at 7:24am UTC
Perhaps if you renamed 'i' to be 'row' and 'j' to be 'col', things would be a lot less opaque.