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.

Recommended Answers

All 5 Replies

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;
}

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?

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?

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?

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.

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.