How to create a SQUARE

Hi guys, so I am stuck with a problem on creating a program whose input is a single integer, which will create a file containing a "square" of that size with alternating symbols i.e * and +, so if user enter 3, then the output would be 3rows and 3 columns of alternating * and + while is empty inside the box.


I have a program, but i need help to make it compile well.

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;

int main()
{
int n;
cout << "How many columns by row do you want to create? " <<endl;
cin >> n;

cout << "Enter a file to be opened for input: ";
string inFilename;
cin >> inFilename;

ifstream inFile;
inFile.open(inFilename.c_str());

if(inFile.fail()) //file does not exist
{
cout << inFilename << " does not exist. Please check it is in the appropriate folder.\n";

}

for(int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
cout << "* ";

if ( i = row)
cout << i = 1 && i = n << "i= * "<< endl;
else
cout << " " << endl;

if (j = column)
cout << j = 1 && j = n << "j = +" << endl;
else
cout << " " << endl;

if (i + j = even)
cout << " * " << endl;
else
cout << " + " << endl;

}

system("PAUSE");
return 0;

}
Lines 14-25: What's with opening inFile? You don't use it. The problem statement does not say anything about reading from a file.
Line 32: = is the assignment operator, not the equality operator. row is not defined.
Line 33: Why do you have the && operator in your cout statement?
Line 37: = is the assignment operator, not the equality operator. column is not defined.
Line 38: Why do you have the && operator in your cout statement?
Line 42: = is the assignment operator, not the equality operator. even is not defined.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to help you.






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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;

int main()
{
int n;
cout << "How many columns by row do you want to create? " <<endl;
cin >> n;

cout << "Enter a file to be opened for input: ";
string inFilename;
cin >> inFilename;

ifstream inFile;
inFile.open(inFilename.c_str());

if(inFile.fail()) //file does not exist
{
cout << inFilename << " does not exist. Please check it is in the appropriate folder.\n";

}

for(int i = 0; i < n; ++i) 
{
for (int j = 0; j < n; ++j)
cout << "* ";

if ( i = row)
cout << i = 1 && i = n << "i= * "<< endl;
else
cout << " " << endl;

if (j = column)
cout << j = 1 && j = n << "j = +" << endl;
else
cout << " " << endl;

if (i + j = even)
cout << " * " << endl;
else
cout << " + " << endl;

}

system("PAUSE");
return 0;

}
Thanks for posting your code with code tags.
Do you have further questions?
You have not fixed the problems I pointed out in my previous reply.
how do i do that.
may u please show me please!!
I've explained what's wrong with your program in my earlier response.

I asked a question about inFile, which you've not answered.

If you don't understand the difference between asignment and equality operators, you need to go back and reread your textbook.

I don't understand what you're trying to do in your cout statement:
 
cout << i = 1 && i = n << "i= * "<< endl;

The statement makes no sense. You're outputting the result of the logical and of two assignments.



Or yes, that part of create file containing a "square" is supposed to be there.
my problem is how to fix this part


am supposed to make something like this. if the user input n = 5 then


* + * + *
+ +
* *
+ +
* + * + *

So may i get help please!!!



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
for(int i = 0; i < n; ++i) 
{
for (int j = 0; j < n; ++j)
cout << "* ";

if ( i = row)
cout << i = 1 && i = n << "i= * "<< endl;
else
cout << " " << endl;

if (j = column)
cout << j = 1 && j = n << "j = +" << endl;
else
cout << " " << endl;

if (i + j = even)
cout << " * " << endl;
else
cout << " + " << endl;

}

system("PAUSE");
return 0;

}
the square is empty inside i mean

1
2
3
4
5
6
* + * + *
+           +
*            *
+            +
* + * + *
Topic archived. No new replies allowed.