# Thread: Double Hops but why ???

1. ## Double Hops but why ???

This code is pretty much all working but for some reason. It calculates the root twice before prompting the user if he wants to continue, Very strange I have looked at it a few times can't figure out why its doing this. Can someone help me w/ this problem. Maybe even show how to get it to stop doing this. Anyhow here is the code:

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define STOP 0.001

double func1 (double x);
double func2 (double x);

char q, blah,f;
double Xro, Xrn, EPS, fXro, fprimeXro;
int Iter;

void main(void)
{
FILE *outptr;

outptr=fopen("output.txt","wt");

label3: printf("\nPLease enter a value for Xro");
scanf("%lf",&Xro);

printf("\nYou entered Xro= %lf", Xro);
fprintf(outptr, "\nThe intial guess is Xro= %lf ", Xro);
Iter=1;
fprintf(outptr,"\nIter Xro Xrn error");
label1: fXro = func1(Xro);
fprimeXro = func2(Xro);
if (fprimeXro == 0.0) {
printf ("\nThe slope of a function at iteratuin number =%4d\
\nis equal to zero. Newton-Raphson Method fails. Program is\
\ngoing to prompt you to start a new initial guess!",Iter);
goto label3;
}
Xrn = Xro -fXro/fprimeXro;
printf("\nThe new estimate of the root is = %lf", Xrn);
EPS = fabs((( Xrn - Xro)/Xro)*100.);
fprintf (outptr,"\n%4d %12.9f %12.9f %12.9f\
",Iter,Xro,Xrn,EPS);
Iter = Iter + 1;
printf("Do you wish to exit, if so enter Y");
scanf("%c", &q);
if (q=='y' || q=='Y') {
return;
}
if ( EPS <= STOP ) {
printf ("\nRoot = %lf",Xrn);
fprintf (outptr,"\nRoot is = %lf", Xrn);
goto label2;
}
else {
Xro = Xrn;
goto label1;
}
label2:
fclose(outptr);

printf ("\nAre you finished using the Newton Raphson's Root finder ?");
scanf("%c", &f);
if (f=='y' || f=='Y')
return;

if (f=='n' || f=='N')
goto label3;

getch();
}
double func1(double x)
{
return( pow(x,2.0) - x -12. );
}
double func2(double x)
{
return( 2.0*x - 1.0);
}

2. I refuse to read code filled with "goto"s.

Quzah.

3. Dude I am a newb trying to learn C Cut me some slack plz...

Also whats so bad about goto ?

4. > Dude I am a newb trying to learn C Cut me some slack plz...
>
> Also whats so bad about goto ?

Well you've just learned your first lesson. goto sucks. Additionally, anything you can do with a goto, you can do without.

Here is your second lesson.

In a nutshell, goto makes your program very hard to follow. You want your programs to have a nice logical flow so that they are very easily red by other people. If you design with this in mind, your projects tend to turn out much better.

Making nice, clean, readable code is one of the best things you can learn.

Quzah.

5. Lesson #3: void main is wrong. For more information pick a thread that I've replied to at random.

-Prelude

6. Flow Ehhhh ? Anyways how could Void Main be wrong thats what it says in my book.

7. >Cut me some slack plz...
What makes you think we aren't? We're teaching you the correct way to program as early as possible, if you stick around for a while then you'll learn a great deal simply by not being offended at our replies and absorbing the information.

>Also whats so bad about goto ?
If used correctly, nothing technically. But local jumping in your code defeats the purpose of procedural programming. There are some that argue in favor of goto because loops are local jumps as well, but goto tends to confuse people enough so that they use it improperly. Loops are difficult to mess up once you understand the basic concept, but goto's just have too many uses for the intention to be easily understood in code.

My recommendation is to learn all you can about goto, then learn how to deal without it. You'll probably come to the same conclusion that the rest of us did, goto really isn't needed.

>Anyways how could Void Main be wrong thats what it says in my book.
Probably because the author of your book was stupid.

-Prelude

8. lol.... On thing I must say is damn you got some quick response times :-)

Do you know of a link that has all the C commands or allot of the ones used in r00kie programing ?

9. Here is a list of functions and their correct usage, and all the parameters they take. Some of these are non-standard (meaning they are system specific), however, a great number of them are ANSI compliant, which is ideally what you should aim for.

That isn't to say there is no reason to not write non-ANSI code; there are perfectly valid reasons why you'd need non-ANSI code. However, for most of the learning projects you'd encounter, it's wise to be ANSI compliant.

Quzah.

10. >Here is a list of functions and their correct usage
I actually haven't been to rt.com/man since I ported the pages to my computer and wrote a program to access them like Linux. I was truly bored one night.

-Prelude

11. Okay thanks for the linkage... I looked around a bit but still can't find exaclty how double works. Would you mind posting how that works ? Plz :-)

12. >I looked around a bit but still can't find exaclty how double works
[oversimplification]
What do you mean by how it works? A double is a data type, similar to int except it can hold considerably larger values with greater precision than int. The usage of double is more or less the same as the other types in how you declare it, assign to it, etc.. The only real difference is what kind of values it holds and what you can do with those values.
[/oversimplification]

-Prelude

13. Arggggggggg.... Okay last post for the day this program is making me mad. For somereason my

printf ("\nAre you finished using the Newton Raphson's Root finder ?");
scanf("%c", &f);
if (f=='y' || f=='Y')
return;

if (f=='n' || f=='N')
goto label3;

will not work. This is supposed to take place at the end when it finds the pure root. Ne1 know why this is not working. I have a feeling its in the wrong spot, but I have tried it in like 6 other places and now I am turning to you.... again

14. Almost every problem in the programs that are posted is caused by programmers using scanf, while having a \n still in the buffer.

A simple solution to this would be to simply use fgets.

fgets(buffer, max_input_size, stdin).

Code:
```buffer[1024] //or any integer that you want for a max input.

fgets(buffer, 1024, stdin);```
This will insert the characters typed including the \0 into buffer.

Popular pages Recent additions