Errors that make no sense...
Everything I touch in programming gets ruined. Errors all over the place.
It's another group project, but this time we have to present it to the class and it refuses to compile. We don't even understand the errors. The program is supposed to return the distance between two input coordinates.
Here's what we have so far:
Code:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int GetCoordinate ();
double GetDistance (double X1, double Y1, double X2, double Y2);
void PrintResults (/*double d, double X1, double Y1, double X2, double Y2*/);
int main(int argc, char *argv[])
{
double X1=0;
double Y1=0;
double X2=0;
double Y2=0;
double d;
GetCoordinate();
d = GetDistance (double X1, double Y1, double X2, double Y2);
PrintResults();
system ("PAUSE");
return 0;
}
int GetCoordinate ();
{
cout << "Enter the coordinates of two points (X1,X2),(Y1,Y2)" << endl;
cin >>X1>>X2>>Y1>>Y2;
}
double GetDistance /*(double X1, double Y1, double X2, double Y2, double d)*/;
{
double d = sqrt(pow((Y2-Y1), 2.0) + pow((X2-X1), 2.0));
}
void PrintResults (double d, int X1, int Y1, int X2, int Y2);
{
cout << "The distance between ("<<X1<<", "<<X2<<") and" << endl;
cout<< "("<<Y1<<", "<<Y2<<") is "<<d<<"." << endl;
}
Here is our collection of errors that we cannot sort out.
- N:\distancecode.cpp In function `int main(int, char**)':
- 18 N:\distancecode.cpp syntax error before `,' token
- distancecode.cpp N:\distancecode.cpp At global scope:
- 25 N:\distancecode.cpp syntax error before `{' token
- 27 N:\distancecode.cpp syntax error before `>>' token
- 30 N:\distancecode.cpp `double GetDistance' redeclared as different kind of symbol
- 7 N:\distancecode.cpp previous declaration of `double GetDistance(double, double, double, double)'
- 31 N:\distancecode.cpp syntax error before `{' token
- 36 N:\distancecode.cpp syntax error before `{' token
- 38 N:\distancecode.cpp syntax error before `<<' token
- [Linker error] undefined reference to `GetDistance(double, double, double, double)'
Maybe these are really easy to fix but we're too inexperienced to figure out what to do. If someone would please shed some light on these errors we'd be much obliged.
Some general advice, applescruff...
Read this when you're no longer in frustration-mode... or is it panic-mode? :) Maybe after you get your program working.
Develop your code a few lines at a time.
Someday I'm going to write an essay on this topic. It seems like a lot of instructors don't really teach how to approach a programming problem. This result in lots of frustrated beginners & students. Nobody... not even experienced programmers write the whole program before trying it out!
1- Start-out with a 'Hello World" type program. You can substitute "Hello World" with some better descriptiion of the program. :)
2- Add one or two lines of code at a time. Compile, test-run, and debug before adding more lines.
3- Add some extra cout statements so you can "see" what the program is doing, and confirm that it's working so-far. (You can take them out, or coment them out later.)
4- When you create a function, make an empty (do-nothing) function first. Make the function prototype, the empty function definition, and the function call. Then, test-compile. If your function needs to return a value, just make it return a constant (i.e. return 10; ). It's also helpful to put a cout statement in your do-nothing function, like: cout << "In GetDistance() function \n"; .
Now, this isn't as easy as it sounds, because you can't just write line one, then line two, then line three, of your code. (It won't compile if you do that.) The trick (the fun part, actually) is figuring-out which part of the code makes sense to develop next.
Compilers can easily get confused when there's an error (or two). Sometimes they will point to the wrong line. Sometimes one error will cause the compiler to report several errors. Link errors are even trickier to track-down.
So, it really helps to test-run and test-compile after every one or two lines. As you gain experience, you can write whole functions before testing. But, as you gain experience your programs generally will get longer and more complex, so you still won't be writing the whole program before testing & debugging.
I once had a misplaced bracket (after a cut-and-past), and the comipler reported over one hundred errors! ... from one tiny little misplaced bracket! :D