You last post did not have the proper include:
#include <iostream>
so be sure to always check that the proper includes are at the top of the file.
Printable View
You last post did not have the proper include:
#include <iostream>
so be sure to always check that the proper includes are at the top of the file.
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
Dear lord, over 100 errors from a missing bracket?
You think debugging the code is the fun part?
Whoa.
I was bored and saw this thread, here is how I would have done it:
no need for extra functions when you dont have to!Code:#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
double GetDistance(double X1, double Y1, double X2, double Y2) {
double d = sqrt(pow((Y2-Y1), 2.0) + pow((X2-X1), 2.0));
return d;
}
int main()
{
double X1, Y1, X2, Y2, result;
cout << "Enter the coordinates of two points (X1,X2),(Y1,Y2)" << endl;
cin >> X1 >> X2 >> Y1 >> Y2;
result = GetDistance(X1, Y1, X2, Y2);
cout << "The distance between (" << X1 << ", " << X2 <<
") and (" << Y1 << ", " << Y2 << ") is " << result << "." << endl;
system ("PAUSE");
return 0;
}
Dag
Please familiarize yourself with the cboard homework policy:Quote:
I was bored and saw this thread, here is how I would have done it:
http://cboard.cprogramming.com/annou...ouncementid=39
Okay, I thought that was just if the person were asking others to do his homework.... Not if someone just wants to write the code. I think that is a good way to learn(for me or the person who try to write it)-- by sharing code, getting comments back.
Sorry if I did wrong.
Thanks again to everyone who helped us out. One less major source of stress to deal with.
:confused:Quote:
Okay, I thought that was just if the person were asking others to do his homework.... Not if someone just wants to write the code.