-
Need help
I've got the majority of this program finished. I need to out put the right sided traingle with a character to a file called ass2.run. It is creating a new file but their are no characters in the file that was created. Any help is appreciated.
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
void prompt();
main ()
{
int n;
char c;
prompt ();
cin>>n;
cout<< "Enter a character:"; // enters a character
cin>> c;
for (int i = 1; i <= n; i++) // for rows
{
for (int j = n - i;j > 0; j--) // for spaces
cout << " ";
for (int k = 1; k <= i; k++) // for characters
cout << c;
cout << endl;
}
ofstream fout("ass2.run");
return 0;
}
void prompt ()
{
cout<< "Enter a number:";
}[CODE]
-
Poor post - useless title and no code formatting
Try again, and use a better title (ie more informative than "help"), and make sure the code tags surroung your code
-
You were very close...
Just move fout("ass2.run") to the top of your code (between main and int n), and replace all of your couts with fout. This will write your triangle to the file.
-
Thanks
Sorry for the bad post, I was in a hurry to get out of school. Our teacher taught us ofstream and ifstream. Alsto infile and outfile. Anybody have a clue.
-
ifstream and ofstream are classes, just like istream and ostream. cin is a built in istream object. cout is a built in ostream object. However, there are no ifstream or ofstream objects built into the compiler. Therefore you have to declare them. You can call them (just about) anything you want of course, just like with any object declaration, but infile is a common name for an ifstream object and outfile is a common name for an ofstream object. Other common names are fin and fout, which are short hand for file in and file out respectively, similar to cin and cout which stand for console in and console out respectively.