Need help

This is a discussion on Need help within the C++ Programming forums, part of the General Programming Boards category; I've got the majority of this program finished. I need to out put the right sided traingle with a character ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    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]

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    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.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    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.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21