dimanche 19 juin 2016

Why won't this class function work?


#include <iostream>

using namespace std;

class chart
{
  public:

    int width;
    int height;
    char grid[100][100];
    chart(int w,int h)
    {
        if (w>100)
        {
            width=100;
        }
        else 
           if (w<1)
           {
              width=1;
           }
           else
           {
              width=w;
           }

        if (h>100)
        {
          height=100;
        }
        else 
            if (h<1)
            {
               height=1;
            }
            else
            {
               height=h;
            }
        for (int y=0;y<height;y++)
            {
                for (int x=0;x<width;x++)
                {
                   grid[x][y]='.';
                }
            }
        }

    void draw_grid()
    {
        cout << endl;
        for (int y=0;y<height;y++)
        {
            for (int x=0;x<width;x++)
            {
               cout << grid[x][y];
            }

            cout << endl;

         }
    }

    char point(int a, int b)
    {
        if ((a<1 or a>width) or (b<1 or b>height))
        {
            cout << endl << "Invalid Point.";
            return ' ';
        }
        else
        {
          return grid[a-1][b-1];
        }
    }

    void pointchange(int a, int b, char c)
    {
        if ((a<1 or a>width) or (b<1 or b>height))
        {
           cout << endl << "Invalid Point.";
        }
        else
        {
            grid[a-1][b-1]=c;
        }
    }

    void linechange(int a1, int a2, int b1, int b2, char c)
    {
        if (((a1<1 or a1>width) or (a2<1 or a2>width)) or ((b1<1 or b1>height) or (b2<1 or b2>height)))
        {
            cout << endl << "Invalid Point.";
        }
        else
        {
            int b=b1-1;
            float counter;
            for (int a=a1;a<=a2;a++)
            {
                counter+=(float(b2)-float(b1))/(float(a2)-float(a1));
                if (counter>=1)
                {
                    while (counter>=1)
                    {
                        b+=1;
                        counter-=1;
                        grid[a-1][b-1]=c;
                    }
                }
                else
                {
                    grid[a-1][b-1]=c;
                }
            }
        }
    }
};

int main()
{
    chart a(10,10);
    a.linechange(1,10,1,9,'#');
    a.draw_grid();
    cout << endl << a.point(8,8);
}

When change in b1 to b2 is higher than or equal to change in a1 to a2, it makes the line fine, but when it's lower it displays tons of garbage text in a never ending loop.

I tried looking for why that would be, but I couldn't find it. Can someone tell what is wrong with this?


Aucun commentaire:

Enregistrer un commentaire