Home > other >  How do you put a letter triangle in a file?
How do you put a letter triangle in a file?

Time:01-30

So this is for some homework I am working on.

I've been working on this for days now. I need to print a letter triangle,

example

a
aa
aaa

to a file I create in the code. I have gotten the triangle part down using a for loop but cannot figure out how to get it into a file. here is what I have so far.

#include <iostream> 
#include <fstream> 
using namespace std;

char loop()
{ for (int i = 1; i <=15; i  ){
    for (int j = 1; j <= i; j  ){
        cout << "a";
    }
    cout << endl;
  }
}

int main()
{
    ofstream week2 ("week2assignment.txt");
    
    if (week2.is_open()){
        char result = loop();

        {
            week2 << loop()   "\n";
        }
    }
    week2.close();
    else cout << "file wasnt created";
    return 0;
}

This is my first post on here and I am in my second week of learning C so any help would be appreciated immensely. Thank you all in advance.

CodePudding user response:

You just need to replace cout with week2 everywhere, since cout is the terminal and week2 is your file.

Given the current structure of your code, where you write to cout in a function, you'll need to pass week2 as an argument to that function to use it there.

CodePudding user response:

First of all, don't build a habit of using using namespace std

std::endl will put a linebreak, but it will also flush your stream. Put easily, outputting something is a really costly (=takes a long time) action in regard to other actions. To account for this, streams are buffered and once the buffer is full it will be flushed automatically. When you use std::endl the buffer might be flushed prematurely and this will tank performance. This is of course not really an issue in this small exercise, but I think it's still good to know. You can use \n instead. Although it might not look like it, it will provide a platform independend linebreak and should virtually always be used instead of std::endl.

Then consider what your function loop should do. Right now, you said it has return type char but it's not returning something. Therefore you should correct this and specify that this function is not returning anything, but just performing an action, hence return type void.

As suggested by John Zwinck, if you pass the stream as an argument, your function becomes more powerful and works with any kind of outgoing stream (i.e. std::ostream).

We can make the function even more generic, but I'll leave it to you to understand the code (if not, feel free to post a comment for clarification). Please note also, that loop is not a descriptive name. I have no idea what loop does. Always give your functions a name that makes it clear what they are doing.

#include <iostream> 
#include <fstream> 

void print_letter_triangle(std::ostream& out, char c = 'a', int count = 15)
{ 
    for( int i = 0; i < count; i   ){
        for( int j = 0; j < i; j   )
        {
            out << c;
        }
         out << '\n';
    }
}

int main()
{
    // for testing purposes
    print_letter_triangle(std::cout);
    print_letter_triangle(std::cout, 'b');
    print_letter_triangle(std::cout, 'c', 7);
    
    std::ofstream week2("week2assignment.txt");
    
    if( week2.is_open() )
    {
        print_letter_triangle(week2);
        week2.close();
    }
    else
    {
        std::cout << "Error: File wasn't created.\n";
    }
    return 0;
}

Lastly: Try to build an early habit of how you want to format your code, positions of curly braces, spaces around operators. Right now it was a little bit inconsistent (which might as well have been caused by putting it on StackOverflow).

There are a couple more nitpicks one can offer (for example, I changed the loop to start from 0 and used the < instead of the <= operator, you can convince yourself that this does not change the number of loop iterations. However, it is common in programming languages to start from 0 (as counter-intuitive as that might be at first). Finally, since negative counting values do not make sense in this loop, one might change int to unsigned int, but I felt that might have been a little too much. Feel free to do so on your own, if you wish).

CodePudding user response:

I suggest you use this code:

#include <iostream> 
#include <fstream> 
using namespace std;

const char* loop()
{ for (int i = 1; i <=15; i  ){
    for (int j = 1; j <= i; j  ){
        return "a";
        }
        return "\n";
    }
}

int main()
{
    ofstream week2 ("week2assignment.txt");
    week2 << loop() << "\n";
    return 0;   
}

You must return a value from your loop() function.

  •  Tags:  
  • Related