Hello everyone, I'm trying to solve a problem in C++ using visual studio. The problem asks to write a program that asks the user for a positive integer no greater than 15, and that the program should then display a square on the screen using the character "X", and that the number entered by the user will be the length of each side of the square. It shows an example: when the user enters 5, the program should display:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX.

Here's what I have so far:

// This program will ask the user for a positive integer no greater than 15 and
will display a square on the screen using the character "X" with a length the
size of the number entered.

#include <iostream>
using namespace std;

int main ()
{
int number;
for (number=1;number<=15;number++)
         cout << "{X}" <<endl;

system("pause");
return 0;
}

That's a rough guess on my part as to how to even go about this.. I'm not that experienced in this class I'm taking as I'd like to be, so any help will be appreciated. Tried asking the teacher and my teacher of course doesn't even reply after 3 days and counting >.<

Recommended Answers

All 7 Replies

Try something like:

This is just a start for your code and a few fixes like letting them enter the number.. But you should do something like:
Make a MULTI-DIMENSIONAL DYNAMIC ARRAY
These represent tables.. Make it x by x. Print the array to the screen. that way how ever many digits they enter, it will do x across and x down.

PLEASE NOTE: I left the input as Integers.. so it will only output a Number.. you can figure out how to change it and make it multidimensional aswell.

/*This program will ask the user for a positive integer no greater than 15 and
will display a square on the screen using the character "X" with a length the
size of the number entered.*/

#include <iostream>
#include <new>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "Type A Number: ";
  cin >> number;
  p= new (nothrow) int[number];
  if (p == 0)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout<<p[n] + "\n";
    }
    delete[] p;
  }
  return 0;
}

P.S. Your gunna need more than one LOOP.. This should almost work.. u can do the rest.

Example:

int number;
int number2;
int number3;
cout<<"bldfgsgds ";
cin>>number;
//U MUST DO SOME CHECKS HERE TO MAKE SURE IT IS A NUMBER!!!!!!!!!!!
number2 = number;
number3 = number;
number = 1;
do{
   for(number=1;number2;number++)
   {
        cout<<"X";
   }
   cout<<"\n";
   number++;
}while(number < number2);

I know you already made the program but I just wrote a completely new one because I was bored.

#include <iostream>

using namespace std;

int main() {
int number;
cout << "enter number: ";
cin >> number;
int i = 0;

char xs[number];
while(i<number) {
xs[i] = 'x';
i++;
}

i = 0;
while(i<number) {
    cout << xs[i];
    i++;

}
}

Sorry, I didn't read directions correctly, wrong program lmao.

Or you could use double loops
this code is simpler and easy...

// This program will ask the user for a positive integer no greater than 15 and
will display a square on the screen using the character "X" with a length the
size of the number entered.

#include <iostream>
using namespace std;

int main ()
{
int number;
cin >> number;
if(number > 15){
   cout <<"Your input must be less than 15!"; }
else {
for (int j=0; j < number; j++){
     for(int i=0; i < number; i++){
         cout << "X";
         }
         cout << endl;
      }

system("pause");
return 0;
}

Hope this is helpful.

Now it should work lol. I modified it.

#include <iostream>

using namespace std;

int main() {
int number;
cout << "enter number: ";
cin >> number;
int i = 0;

char xs[number];
while(i<number) {
xs[i] = 'x';
i++;
}

int f = 0;
i = 0;

while(i<number) {

while(f<number) {
    cout << xs[f];
    f++;
}
cout << "\n";
    i++;
    f = 0;

}

}

Crap, less then 15, I have to redo it again :(

#include <iostream>

using namespace std;

int main() {
int number = 55;
while(number>15) {
cout << "enter number: ";
cin >> number;
}
int i = 0;

char xs[number];
while(i<number) {
xs[i] = 'x';
i++;
}

int f = 0;
i = 0;

while(i<number) {

while(f<number) {
    cout << xs[f];
    f++;
}
cout << "\n";
    i++;
    f = 0;

}

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