0

I am writing a program that reads a line from a file, and outputs an ASCII shape based on the line in the file. For example, this "S @ 6" would mean a solid square of 6 @ by 6 @. My problem is I can read the lines from the file, but I'm not sure how to separate the characters in the file and use them as input. I have already written functions to make the shapes, I just need to pass the characters in the file as arguments.

int main()
{
    void drawSquare (char out_char, int rows, int width);
    void drawTriangle (char out_char, int rows);
    void drawRectangle (char out_char, int height, int width);

    char symbol;
    char letter;
    int fInt;
    string line;
    fstream myfile;
    myfile.open ("infile.dat");

    if (myfile.is_open())
    {
        while (getline (myfile,line))
        {
          cout << line << '\n';
        }
        myfile.close();
    }

  else cout << "Unable to open file";
  drawRectangle ('*', 5, 7);

}
gingikid
  • 197
  • 7

1 Answers1

0

If I understand correctly your input file is in below format: @

And based upon the symbol you want to call appropriate function by passing value of length.

You can achieve this by parsing the line that you read from file:

const char s[2] = " ";// assuming the tokens in line are space separated
while (getline (myfile,line))
{
    cout << line << '\n';
    char *token;
    /* get the first token */
    token = strtok(line, s); // this will be the symbol token
    switch(token)
    {
        case "s" : 
        /* walk through other tokens to get the value of length*/
        while( token != NULL )
        {
           ...
        }
        drawSquare(...);// after reading all tokens in that line call drawSquare function
        break;

        ... //similarly write cases for other functions based on symbol value
    }
}
Dipti Shiralkar
  • 362
  • 2
  • 6