0

I have an input file named animals.dat that I need my program to read and output the files in block format. For example the file reads:

TIGER DOG CAT

It needs to output

TTTTTTTTTTTTTTTTTTTT (T would be 1x20 as it's the first character in the word and 20th letter in the alphabet)

IIIIIIIII IIIIIIIII (I 2x9 as it's the 2nd character and 9th in the alphabet)

I've tried to set up functions to do this but my output kinda goes insane, outputting just TONS of one character at a time, and I'm pretty sure not even doing the rows. What am I doing wrong?

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin;
ofstream fout;

void rectangle(char ch, int alphacount,int count) {
int height=1, width=0;
while(width <= alphacount && height <= count) {

    while(width <= alphacount) {
        fout << ch;
        cout << ch;
        width++;
    }
    cout << endl;

    if(height <= count) {
        fout << ch << endl;
        cout << ch << endl;
        height++;
    }
}
}

int main(void) {
 fin.open("animals.dat");
fout.open("out.dat");
 int count=0, alphacount=0;
 char ch, x='A';
 while(!fin.eof()) {
    fin >> ch;
    while(x!=ch) {
        x++;
        alphacount++;
    }
    rectangle(ch, alphacount, count);

    count++;
    if(ch =='\n') {
        alphacount = 0;
        count = 0;
        x = 0;
    }
}

system("pause");
}
4
  • 2
    'What am I doing wrong?' Please use a debugger to find out, before asking here. If you have particular questions about behavior you don't understand and that can't be explained from the c++ reference documentation, ask such please! Apr 7, 2014 at 20:47
  • you might start with resetting x to a reasonable value on occasion...
    – twalberg
    Apr 7, 2014 at 20:47
  • Your code is very convoluted and I'm not sure what your intent is. For example you describe making a 1x20 array of T's, but your code makes it look like you're trying to print a 20x20 square of T's. Take it in small steps. First try printing out the number that you want for each letter, see if you've got that right. If you want the number '20' for T, you don't need to use a loop, just arithmetic, like 'T'-'A'+1 maybe with some appropriate casting. Then do a separate function that prints out 20 'T''s, or 9 'I''s and get that working before combining it with your other code. Apr 7, 2014 at 20:52
  • Thanks guys, I apologize for the imprecise way of asking. Just kinda needing some direction here.
    – Skathix
    Apr 7, 2014 at 21:08

2 Answers 2

1

Things that I see:

  1. The function rectangle can be easily simplified. You just need two for loops.

    void rectangle(char ch, int alphacount,int count)
    {
       for ( int height = 0; height < count; ++height )
       {
          for ( int width = 0; width < alphacount; ++width )
          {
             fout << ch;
             cout << ch;
          }
          cout << endl;
       }
    }
    
  2. You don't need x at all because you can compute alphacount directly using arithmetic.

  3. You can move alphacount inside the while loop.

  4. The code inside the while loop can be simplified to:

    while(!fin.eof())
    {
       int alphacount = 0;
       count++;
       char ch;
       fin >> ch;
       if ( isalpha(ch) )
       {
          if ( ch > 'Z' )
          {
             // It's a lower case letter.
             alphacount = ch - 'a' + 1;
          }
          else
          {
             // It's an upper case letter.
             alphacount = ch - 'A' + 1;
          }
          rectangle(ch, alphacount, count);
       }
    
       if(ch =='\n')
       {
          count = 0;
       }
    }
    
7
  • First off, thank you very much, this is much more clear and elegant. However the output is giving me 5 rows of I instead of 2, any reasons why? I edited the file to where it's only: TI
    – Skathix
    Apr 7, 2014 at 21:16
  • @Skathix I am not sure why. When I ran the program I got two rows of 'I's.
    – R Sahu
    Apr 7, 2014 at 21:18
  • The while (!fin.eof()) should be changed to while (fin >> ch). This can be found by searching StackOverflow. Apr 7, 2014 at 21:44
  • What is the difference in these two statements?
    – Skathix
    Apr 7, 2014 at 21:53
  • also while (fin >> ch) screws the output up.
    – Skathix
    Apr 7, 2014 at 21:55
1

You're not reinitializing x and alphacount within the outer loop. Your code should look like this:

while(!fin.eof())
{
    int alphacount=0;
    char ch, x='A';

    fin >> ch;
    .
    .
    .

A debugger would have found this problem for you in much less time than it took to write your question.

4
  • Sorry, I'm extremely new and unfamiliar with debugging, do you have any advice on how to learn this?
    – Skathix
    Apr 7, 2014 at 20:59
  • 1
    @Skathix I'm pretty sure sure you'll find tons of comprehensive tutorials 'How to debug' for your particular toolchain/IDE just googling for it. Apr 7, 2014 at 21:01
  • @Skathix what platform are you working on? If you're using linux I highly recommend playing around with GDB Apr 7, 2014 at 21:01
  • @πάντα ῥεῖ That's a great call, I don't know why my head shuts down when I start asking for advice. Thank you.
    – Skathix
    Apr 7, 2014 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.