Construct a Square


How to make it run?
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
55
56
57
58
59
60
61
62
// A program whose input is a single integer, and which will create a file containing a " square"
// The square with alternating symbols * and + .


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

int main()
{
    int n;
    int column;
    int row;
    int even = n % 2 == 0;
    
    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 << " * ";
			cout << endl;
            
            if ( i = row)
		     cout << i = 1 && i = n << " * "<< endl;
		     else
		     cout << " + " << endl;
		     
		if (j = column)
		  cout << j = 1 && j = n <<  "+" << endl;
		  else
		  cout << " * " << endl;
		  
		if (i + j == even)
		 cout << " * " << endl;
		 else
		 cout << " + " << endl;	
		  	
   }
   
        system("PAUSE");
        return 0;
    
}   
   
This is the same code you posted here:
http://www.cplusplus.com/forum/general/83638/
It still has exactly the same problems that I pointed out to you in that thread.

Problem 1 (lines 23-33):
1
2
3
4
5
6
7
8
9
10
11
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";

    }

Why are you opening inFile? You're not using it. This code should be removed.

Problem 2 (lines 41, 46):
 
if ( i = row)

Learn the difference between an assignment operator and the equality operator.
That statement assigns the value of row to i. The result of the if will always be true.
What you want is to test for equality.
 
if ( i == row)


Problem 3 (lines 42 & 47):
 
cout << i = 1 && i = n << " * "<< endl;

What is that line supposed to do? It makes no sense. As best I can tell, you assigning 1 to i, then assigning n to i, then logically anding the two assignments and outputting a boolean values.




line 23-33, is valid
my problem is how to show rows, or column because it is a square of symbols

Problem 3 (lines 42 & 47):

cout << i = 1 && i = n << " * "<< endl;

thus just my idea if one says n = 5
then
* + * + *
+ +
* *
+ +
* + * + *

how can i close the first and the last column & row without touching the middle ??
Topic archived. No new replies allowed.