Need help

This is a discussion on Need help within the C++ Programming forums, part of the General Programming Boards category; I need to write a program that inputs from the user a positive integer, say n, and any character, say ...

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

    Need help

    I need to write a program that inputs from the user a positive integer, say n, and any character, say c, and outputs to a file called "ass2.run" a solid isosceles triangle constructed with the character c, with its sides of size n and its right angle at the lower right corner. For example, if the inputs were 6 and the character "*" , the output file would look like:

    Triangle: n=6, character =*
    *
    **
    ***
    ****
    *****
    ******
    It needs to be right justified. Basically the opposite of what it looks like.

    This part is for entering the number and character.
    So far I have
    Set up computer
    void prompt ();
    Main ()
    int n;
    char c;
    prompt ();
    cin>>n;
    cout<< Enter a character:";
    cin >> 'c';

    This part is for the for loops. I need a total of 3 for loops.

    for(int i=1; i<=n; i++)
    {
    for(int j=1; j<=n; j++)

    I can't figure out the for loops for the spaces before the * symbol.

    Finally, I cant figure out how to send the program to the ass2run file. Thanks for any help.








    [CODE]

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219

    This is what you want for file output...

    You need to construct an object of ofstream.
    Code:
    ofstream fout("ass2.run"); // create with default read write abilities
    // use like cout to write to file
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    And for the alignment:
    Code:
    cout << setf(ios::right, ios::adjustfield) << setw(n);
    Should do it.

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    To do it with loops this is what you want.
    Code:
    	for (int i = 1; i <= n; i++)
    	{
    		for (int j = n - i;j > 0; j--)
    			cout << " ";
    		for (int k = 1; k <= i; k++)
    			cout << ch;
    		cout << endl;
    	}
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

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

    Thanks for the help

    As far as putting this program to the ass2.run file. He taught us to use infile(), outfile(), commands.

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    What OS are you using? Which compiler? That is not used with Microsoft's Visual C++ 6.0 or 7.0 (.Net). Or perhaps it is a member of a class?
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

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

    Program I am using

    We are ysing a Programmer's File Editor (version 1.01). Its called PFE for C++.

  8. #8
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    I don't know anything about infile or outfile they are not a part of the C++ standard. You should tell your professor that if he is to teach C++ he should teach the ANSI/ISO C++ standard and not some non standard extension.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

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