We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,623 Members — Technology Publication meets Social Media

# c++ n*n grid woes

good afternoon gents, first semester cs student here. i'm having a world of confusion to create an X having the left formed with "+", the right side formed with "X" and the center with an "*". with an integer of 7, the output should look like:
+-----X
-+---X-
--+-X--
---^---
--+-X--
-+---X-
+-----X

my attempts thusfar outputs the x, but i cannot get the right symbols in the right place

``````#include<iostream>
using namespace std;
int main(){
int x;
cout<<"enter an odd positive integer ";
cin>>x;
while(x%2==0||x<0){
cout<<"enter an odd positive integer ";
cin>>x;
}
for(int i=0;i<x;i++){
for(int j=0;j<x;j++)
if(i==j&&(i+j<=x/2+1))
cout<<"+";
else if(i==j&&(i+j>x))
cout<<"X";
else if(i+j==x-1&&((i+j))
cout<<"X";
else if(i+j==x-1&&(i+j<x/2))
cout<<"+";
else if(i==x/2&&j==x/2)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
return 0;
}
``````

thank you gentlemen.

2
Contributors
5
Replies
1 Hour
Discussion Span
8 Months Ago
Last Updated
8
Views
shawn.widjaja
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Skill Endorsements: 0

You could certainly do it that way, but it's excessively complicated. The pattern adjusts predictably for each row, so just keep it simple:

``````#include <iomanip>
#include <iostream>

using namespace std;

int main(void)
{
int distance = 7;
int i = -1;

while (++i < distance / 2)
cout << setw(i) << "" << '+' << setw(distance - i * 2 - 2) << "" << 'X' << endl;

cout << setw(i) << "" << '*' << endl;

while (--i >= 0)
cout << setw(i) << "" << '+' << setw(distance - i * 2 - 2) << "" << 'X' << endl;
}
``````
deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 58

thank you sir. i think the reason the way i attempted to do it is overly complicated is because i'm not familiar with the common (more advanced methods) yet.

i've tried putting the code that you posted into the compiler just to get that little relief and see something that works, however im getting an error message on line 12:
invalid operands of types âintâ and âconst char [2]â to binary âoperato<<â

i put your code word for word into the compiler, even down to putting whitespace where you do. thoughts?

shawn.widjaja
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Skill Endorsements: 0

is because i'm not familiar with the common (more advanced methods) yet.

The method I suggested is simpler and easier for a beginner to understand and/or figure out.

i put your code word for word into the compiler, even down to putting whitespace where you do. thoughts?

What compiler is it?

deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822
Skill Endorsements: 58

The method I suggested is simpler and easier for a beginner to understand and/or figure out.

i see, thank you. i'm going to try to wrap my head around your method and hopefully understand how it works.

What compiler is it?

i'm using ssh to access my school's venus account, and am doing my work directly within ssh. is that my problem?

shawn.widjaja
Newbie Poster
3 posts since Oct 2012
Reputation Points: 0
Skill Endorsements: 0

i'm using ssh to access my school's venus account, and am doing my work directly within ssh. is that my problem?

I have no idea how to interpret that into an answer to my question. Regardless, the code is correct; of that I'm quite sure. If your school's compiler doesn't accept it then the compiler is either wrong (unlikely unless it's a pre-standard compiler), or there's some kind of issue in passing the text to the compiler for processing (more likely). Maybe there's an issue with newline conversions in the source code, or control characters, or whatnot.

deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822