I am required to make a code that makes a pyramid with number of input lines "n" to a text file. What i figured i would do i make the program so that it makes a pyramid on the screen and then write it to a text file. This is my code thus far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream data;
data.open("pyramid.txt");
double n;
int i, k, j;
cout << "Input the number of lines in the pyramid as a WHOLE NUMBER: " << endl;
cin >> n;
if ( (int) n/ (double) n != 1){
do
{
cout << "Please Input a WHOLE NUMBER" << endl;
cin >> n;
}while ( (int) n/ (double) n != 1);
}
for (int i(1); i <= n; ++i)
{
for ( j = n-i; j>0; j--)
cout << " ";
data << " ";
for(k=1;k<2*i;k++)
cout << "+";
data << "+";
cout << endl;
}
data.close();
system("PAUSE");
return 0;
}
It makes the pyramid fine on the screen, my problem is writing it to the text file. What i tried was to input the data << after the " " and the "+" but that only puts the nth line into the text file correctly. Any help or suggestions?