Logical Errors - Functions

I am attempting to write a program with modular structure that asks the user for two points on a line, calculates the slope of the line, and determines whether the line is horizontal, vertical, falling, or rising. My program should consist of two functions: one for calculating slope and another that determines the line direction. My code (as far as I know) is correct, however it consists of logical errors in which I cannot figure out.

If anybody could point out these logical errors I would greatly appreciate it!

This is what I have:
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
#include <iostream.h>
#include <math.h>

   void LD (int, int, int);
   int SOL(int, int, int, int);

           
   void main()
           
   {
      double slope;
      float x1, y1, x2, y2;
      char ans;
      ans='y';
   
      while (ans=='y' || ans=='Y')
      {
         cout << "\nEnter X1: ";
         cin >> x1;
      
         cout << "\nEnter Y1: ";
         cin >> y1;
      
         cout << "\nEnter X2: ";
         cin >> x2;
      
         cout << "\nEnter Y2: ";
         cin >> y2;
      
         SOL (x1, x2, y1, y2);
         LD (x1, x2, slope);
      
         cout<<"\n\nDo you wish to continue? (y/n): ";
         cin>>ans;
      }
   }

           
   int SOL (int x1, int x2, int y1, int y2)
           
   {
      if (x1-x2 != 0)
      {
         cout<<"\nSlope is: "<<(y1-y2)/(x1-x2)<<endl;
         return (y1-y2)/(x1-x2);
      }
      else
      {
         cout<<"\nSlope is undefined.";
         return 0;
      }
   }

           
   void LD (int x1, int x2, int slope)
           
   {
      if (x1-x2 != 0)
      {
         if (slope > 0){
            cout<<"\nThe line is: Rising";
         }
         else if (slope < 0){
            cout<<"\nThe line is: Falling";
         }
         else if (slope == 0){
            cout<<"\nThe line is: Horizontal";
         }
         else {
            cout<<"\nThe line is: Vertical";
         }
      }
   }
Last edited on
in LD()

1
2
3
4
5
6
7
8
9
10
11
12
       if (slope > 0){
            cout<<"\nThe line is: Rising";
         }
         else if (slope < 0){
            cout<<"\nThe line is: Falling";
         }
         else if (slope == 0){
            cout<<"\nThe line is: Horizontal";
         }
         else {
            cout<<"\nThe line is: Vertical";
         }


the last else can never be entered, slope will always match one of the 3 previous tests.

In main()
slope is never assigned a value, you just use it. You also declare it as double but use it as int.


That should get you started :)

In line 30 you call the SOL function and return the calculated slope if it's defined, but you don't save the returned result anywhere in main. So when you call the LD function, the slope variable has an uninitialized value.

Edit: to add on to a point Jaybob66 made - you've declared x1,x2,y1,y2 as floats, but use them as ints in the functions.

Line 50 - you return 0 as the slope if the slope is undefined. (x values are the same). But in the LD function, the value of 0 in slope indicates a horizontal line. If the x values are actually the same and just the y values are different, seems that would be a vertical line.
Last edited on
I also think you should be using floats or doubles for your slope calculations, those divides will soon mess your answers up.
SOL (x1, x2, y1, y2); return (y1-y2)/(x1-x2);

You appear to be returning to nothing, and I don't see slope variable ever getting initialized.
I am assuming this is what you mean. slope = SOL (x1, x2, y1, y2);, if this is the case you should probably return a double instead of an int if you don't wish your value to get truncated.
Last edited on
@CodeGoggles

I don't see slope variable ever getting initialized.

Whenever I've tried to initialize slope i.e. slope = SOL (x1, x2, y1, y2); I receive an error stating
Not an allowed type in function main()
Are you trying to do that on line 11 or line 30?

1
2
3
double slope;//line 11 declare variable, could initialize to a default value, e.g. 0

slope = SOL (x1, x2, y1, y2);//line 30 - store result of function in slope variable 
Last edited on
Are you trying to do that on line 11 or line 30?


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
void main()
           
   {
      double slope=0;
      int x1, y1, x2, y2;
      char ans;
      ans='y';
   
      while (ans=='y' || ans=='Y')
      {
         cout << "\nEnter X1: ";
         cin >> x1;
      
         cout << "\nEnter Y1: ";
         cin >> y1;
      
         cout << "\nEnter X2: ";
         cin >> x2;
      
         cout << "\nEnter Y2: ";
         cin >> y2;
      
         slope = SOL(x1, x2, y1, y2);
         LD (x1, x2, slope);
      
         cout<<"\n\nDo you wish to continue? (y/n): ";
         cin>>ans;
      }
   }


This is what I attempted to do, and it's still displaying the same error:

Not an allowed type in function main()

referring to line: slope = SOL(x1, x2, y1, y2);
Last edited on
Ha ha I cant believe everyone missed it. Including myself the first time around.

void main()

main always has a return value of int.

1
2
 int main()
{}


That would be why you are getting the error
Not an allowed type in function main()
Last edited on
@Code,

So.. I'm suppose to change void main() to int main()?

I have no idea what I'm suppose to do.
Last edited on
Yes main always has a return value of int and I do mean always
Alright, well I changed it to int main() and I still have the error...
Ok gimme a sec ill copy into my IDE and revise. Also just noticed #include <iostream.h> standard libraries dont have a .h at end just <iostream>
Last edited on
My compiler requires the .h, I know it's outdated, it's what is used for my class.
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
#include <iostream>
#include <math.h>

using namespace std; // <-- use this or put a prefix std:: infront of all your standard library stuff ie std::cout, std::cin >>

void LD(int, int, int);
int SOL(int, int, int, int);


int main()

{
	double slope;
	float x1, y1, x2, y2;
	char ans;
	ans = 'y';

	while (ans == 'y' || ans == 'Y')
	{
		cout << "\nEnter X1: ";
		cin >> x1;

		cout << "\nEnter Y1: ";
		cin >> y1;

		cout << "\nEnter X2: ";
		cin >> x2;

		cout << "\nEnter Y2: ";
		cin >> y2;

		slope = SOL(x1, x2, y1, y2);
		LD(x1, x2, slope);

		cout << "\n\nDo you wish to continue? (y/n): ";
		cin >> ans;
	}
}


int SOL(int x1, int x2, int y1, int y2)

{
	if (x1 - x2 != 0)
	{
		cout << "\nSlope is: " << (y1 - y2) / (x1 - x2) << endl;
		return (y1 - y2) / (x1 - x2);
	}
	else
	{
		cout << "\nSlope is undefined.";
		return 0;
	}
}


It works now with the latest fixes I suggested, but you should also go through and take note of all the other fixes people pointed out.
On a side note I would suggest you get an IDE with syntax highlighting this will point out obvious flaws for you and you will progress much quicker instead of running into these roadblocks.
Last edited on
My compiler requires the .h, I know it's outdated, it's what is used for my class.

I'd highly recommend getting a newer one then, a good starter IDE is codeblocks you can find at http://www.codeblocks.org/downloads. get the one with the mingw compiler included.
Last edited on
@CodeGoggles,

Thank you very much. What IDE do you suggest I use when I am done with this course and learning on my own?

EDIT: You answered my question on your post. :)

Thank you everyone for your help!
Last edited on
It really depends on what you like and your operating system. Codeblocks is a good starter. I personally use visual studio express 2013 which is also free from the microsoft site. It will also save you a ton of typing. Did you get it working on your compiler?

EDIT: Glad to of helped :) happy coding.
Last edited on
Topic archived. No new replies allowed.