Need Help with Homework

Hi, i have an homework about writing in Dev c++ a program where you put two points coordinates(x1,y1) and (x2,y2) and the program must use gotoxy to draw a "*" line between the two points on the screen.
I spent hours on it but still can't make it work, here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
  #include <windows.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void gotoxy(int x, int y) 
{ 
    COORD coord;
    coord.X = x; 
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main(int argc, char** argv) {
	int x1,x2,y1,y2,xa=1,ya=1;
	do{cout<<"Enter the x coordinate of the first point"<<endl;
	cin>>x1;}while(x1>=0);
	do{cout<<"Enter the y coordinate of the first point"<<endl;
	cin>>y1;}while(y1>=0);
	do{cout<<"Enter the x coordinate of the second point"<<endl;
	cin>>x2;}while(x2>=0);
	do{cout<<"Enter the y coordinate of the second point"<<endl;
	cin>>y2;}while(y2>=0);
	
	xa=x1;
	ya=y1;
	//case of (1,1) and (15,15)
	if ((x1==y1) and (x2==y2)){
		for (int i=xa;i<=y2;i++){
			if ((x2>x1)&&(y2>y1)){
			gotoxy(xa,ya);
			cout<<"*"<<endl;
			xa=xa+1;
			ya=ya+1;
		}
		//case of (15,15) and (1,1)
		else if ((x1>x2)&&(y1>y2)){
			gotoxy(xa,ya);
			cout<<"*"<<endl;
			xa=xa-1;
			ya=ya-1;
		}
		}
	}
	//case of (1,5) and (1,10)
	else if ((x1==x2)&&(y1!=y2)){
		if (y2>y1){
		for(int i=ya;i<=y2;i++){
		    gotoxy(xa,ya);
		    cout<<"*"<<endl;
		    ya=ya+1;
		  }
		}
		//case of (1,15) and (1,1)
		else if(y1>y2){
			for (int i=y2;y2<=y1;i--)
			gotoxy(xa,ya);
		    cout<<"*"<<endl;
		    ya=i;	
		    }
	    }
	}
	//case of (1,1) and (15,1)
	else if ((x1!=x2)&&(y1==y2)){
		if (x2>x1){
		for(int i=xa;i<=x2;i++){
		    gotoxy(xa,ya);
		    cout<<"*"<<endl;
		    xa=xa+1;
		  }
		}
		//case of (15,1) and (1,1)
		else if(y1>y2){
			for(int i=xa;i>=x2;i--)
			gotoxy(xa,ya);
		    cout<<"*"<<endl;
		    ya=i;	
		}
	}
	
	
	
	
	
	return 0;
}
First of all you need the slope of the two points. With this slope you can calculate in a loop for each x the appropriate y.

The next thing is the direction (i.e. the sign of x/y). For instance:

x2 < x1 -> -x
y2 < y1 -> -y

otherwise the according values remains positive.
Topic archived. No new replies allowed.