0
i made a program which gives a cross pattern of symbol (*) as a n output in 5 rows and 5 columns. what's the error here?
#include <iostream> using namespace std; int main() { int i,j; for(i=1;i<=5;i++){ for(i=1;i<=5;j++){ if(i==j||(i+j==6)){ cout<<"*"; else cout<<" "; } } cout<<endl; } return 0; }
5 odpowiedzi
+ 2
Ariz imam ,
It'll work with little modifications.
See the inner for loop. It's syntactically correct but logically wrong. you was supposed to use j instead of i for initializiation,condition and increment
this line:
for(i=1;i<=5;j++)
should be
for(j=1;j<=5;j++)
next problem is mismatched if and else.
see that you have used curly braces after outermost if condition. Inside these braces you have an else clause which has no matching if. so it fails to compile.
To fix this either remove {} of outer if or match them properly as follows :
if( i==j || (i+j==6) ){
cout<<"*";
}else{
cout<<" ";
}
Keep practicing and always try your best to debug before you ask others :)
+ 3
In addition to 🇮🇳Omkar🕉 fixes, next time include a link to your code in your question. It makes it easier for us to run your code, prevents errors in our version verses yours, and lets us describe the issues with line numbers so you can see exactly what we see.
You can use the plus sign within the circle icon to insert your code or share the code, copy to clipboard, and paste the link.
+ 1
thanks 🇮🇳Omkar🕉 for finding the error of inner loop for j.. yes i mistyped it and was too dumb not to check it.. although i did check but couldn't spot that silly mistake... and parenthesis removed for if else statement making the code easier..
+ 1
ok John Wells will do it from next time..