Search code examples
c++bgi

expected ')' error in a c++ code


This is a Bresenham algorithm for a line in the first positive octant. The code is almost from the http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html. but it doesnt work and the turbo c++ says there is an error in the line 4, ") EXPECTED". I really don't know how to solve it. I would be happy if there is any help.

#include <iostream.h>
#include <graphics.h>
#include <conio.h>
void LINE(int x1, int y1, int x2, int y2)
{
    int dx = x2 - x1;
    int dy = y2 - y1;
    int y = y1;
    int e = 0;
    for (int x = x1; x <= x2; x++)
    {

        putpixel(x, y, color);
        e += dy;
    }
}
 int main()
{
    int x1, x2, y1, y2, color, gd = DETECT, gm;
    initgraph(&gd, &gm, "..\\bgi");
    cout << "\n Enter Start Point:";
    cin >> x1 >> y1;
    cout << "Enter End Point:";
    cin >> x2 >> y2;
    cout << "Enter your Favorite Color:";
    cin >> color;
    line(x1, y1, x2, y2, color);
    getch();
    closegraph();
    return 0;
}

Solution

  • void Bresenham(int x1,int y1, int x2, int y2, colour)
                                                  ^ you forgot the type here
    

    It should be

    void Bresenham(int x1,int y1, int x2, int y2, int colour)
    

    ( But you are not even using this function in your code )

    You should also use int main() over void main()

    int main()
      {
         // your code
        return 0;
      }
    

    You also have an error here

    cout>>"Enter your Favorite Colour:";
         ^
         here
    

    it should be

    cout << "Enter your Favorite Colour:";
    

    and

    cin<<colour;
    

    should be

    cin >> colour;
    

    You also have an extra parameter in your call yo line(). Remove that colour from there . It should be

    line( x1 , y1 , x2 , y2 );
    

    If you want to set the color, use

    setcolor( /* color code */ );
    

    These are the color codes

     0   BLACK
     1   BLUE
     2   GREEN
     3   CYAN
     4   RED
     5   MAGENTA
     6   BROWN
     7   LIGHTGRAY
     8   DARKGRAY
     9   LIGHTBLUE
     10  LIGHTGREEN
     11  LIGHTCYAN
     12  LIGHTRED
     13  LIGHTMAGENTA
     14  YELLOW
     15  WHITE
    

    Example, to get RED, you use

    setcolor( 4 );