// 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>
usingnamespace 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;
}
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.