0

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?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Agni
0

Is it because you don't have {} after the for statements in both the cases? Without the braces only the next line is part of for loop. And you will also need a

data << "\n";
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.