# Need your help guys :'(

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-06-2006
risby
Quote:

Originally Posted by Saarah
after I did some modification i got this final code

I think your assessment of this as "final code" needs revisiting :-)

Quote:

Originally Posted by Saarah
Code:

`const int TWO = 2;`

how does this help? The constant isn't used in the program.

Quote:

Originally Posted by Saarah
Code:

`        float Determinant;`

i know Salem said float but the argument to, and return value from, the sqrt function are both doubles so it seems quite odd to declare determinant as float rather than double especially when root1 and root2 are now doubles.

Quote:

Originally Posted by Saarah
Code:

```        printf("Please enter 3 integers separated by a space:\n");         scanf("%d,%d,%d,",&a,&b,&c);```

Your prompt here specifies a space as the separator but your format specifier specifies a comma. Also, why have you specified the need for a trailing comma?

Quote:

Originally Posted by Saarah
Code:

```        if (Determinant > 0)         {         printf("The Determinant equals to: %f\n", Determinant);         root1=(-b+sqrt(Determinant))/(2*a);         root2=(-b-sqrt(Determinant))/(2*a);         printf("The two roots are %d and %d\n",root1, root2);         }         else if (Determinant < 0)         {         printf ("There are no real roots.\n");         }```

This structure does not allow for the determinant being zero, say if both a and b = 4 and c=1.

Quote:

Originally Posted by Saarah
Code:

```        printf("If more data to be processed, type Y or y: \n");         printf("else type Q or q to quit: \n");         scanf("%c",&ANS);         if (ANS == 'Y' || ANS == 'y')                 goto More;         else {                 printf("**** End of the program **** \n"); }```
also the scanf for the last part doesn't seem to work and doesn't go back to More

I'm not entirely sure what's going on here but I find I have to end the input of variables by entering both a return and then an end-of-file character (crtl-z on windows, ctrl-d on unix).

Quote:

Originally Posted by Saarah
this is driving me crazy ,, but i feel so close to finish it ..

welcome to our world Saarah.
• 09-07-2006
Saarah
Hello

Thanks all for all the info, it helped me alot

now i have managed to get it to work. This is my final code

Code:

```#include <stdio.h> #include <math.h> main () {           float a, b, c, root1, root2, Determinant;         float re, im;         char ANS; More:         printf("\nThis program will compute the roots\n");         printf("of a quadratic equation of the form:\n");         printf("\tAx^2 + Bx + C");         printf("\nEnter A, B, and C with a space between each: ");         scanf("%f%f%f", &a, &b, &c);         Determinant = (b*b)-(4*a*c);         if (Determinant >= 0) {         root1 = (b - sqrt(Determinant))/(2*a);         root2 = (b + sqrt(Determinant))/(2*a);         printf("the roots are: x=%f and x=%f\n", root1, root2);         }         else {         re = -b/(2*a);         im = sqrt(-Determinant)/(2*a);         printf("the roots are:  %f + %fi, and %f - %fi\n", re, im, re, im);         }           printf("\nIf more data to be processed, type Y or y:\n");           printf("else type Q or q to quit:\n");         scanf("%c", &ANS);         if (ANS == 'Y' || ANS == 'y')         goto More;         else                 printf("**** End of the program **** \n");         return 0;}```
The Only thing that does not work iS this part

Code:

```          printf("\nIf more data to be processed, type Y or y:\n");           printf("else type Q or q to quit:\n");         scanf("%c", &ANS);         if (ANS == 'Y' || ANS == 'y')         goto More;         else                 printf("**** End of the program **** \n");```
The program does not wait for an answer from me, check the example bellow

Code:

```root@o [/home]# ./a.out This program will compute the roots of a quadratic equation of the form:         Ax^2 + Bx + C Enter A, B, and C with a space between each: 1 -5 4 the roots are: x=-4.000000 and x=-1.000000 If more data to be processed, type Y or y: else type Q or q to quit: **** End of the program ****```
After giving the roots, it then prints out

If more data to be processed, type Y or y:
else type Q or q to quit:

and it doesn't wait for an answer and ends the program

Can someone help me with this last part

Thank you so much
• 09-07-2006
Dave_Sinkula
Just say "No!" to scanf.
• 09-07-2006
Saarah
Quote:

Originally Posted by Dave_Sinkula
Just say "No!" to scanf.

Can you explain more to me on what should i edit, I didn't quite understand what you wrote, thank you
• 09-07-2006
Dave_Sinkula
Quote:

Originally Posted by Saarah
Can you explain more to me on what should i edit, I didn't quite understand what you wrote, thank you

Wherever you use scanf, get rid of it; use fgets to input a string, and then sscanf if you need to convert it to a number if need be.
• 09-07-2006
Saarah
I am sorry, but I am a new to c programming

I still didn't understand the fgets, and the professor didn't tell us to use fgets

so is there any possible way to get the last scanf to work

thanks
• 09-07-2006
quzah
What's not to understand?
Code:

```ptr = fgets( tohere, thisamount, fromhere ); if( ptr == NULL )     printf( "There was an error reading.\n" ); else     ...no error...```
It reads tohere, thisamount of bytes, fromhere. If you are trying to read from a keyboard, then fromhere instead of being a FILE *, is the defined stdin, which is the standard input stream.

Anyway, sure, you can get scanf working. You just need to make sure they input exactly what you expect, and that you handle all input. Remember, when you press enter, that counts as an input character too.

Quzah.
• 09-08-2006
Saarah
For the code i wrote above

Code:

```printf("\nIf more data to be processed, type Y or y:\n");           printf("else type Q or q to quit:\n");         scanf("%c", &ANS);         if (ANS == 'Y' || ANS == 'y')         goto More;         else                 printf("**** End of the program **** \n");```
when i run the program, i enter 3 numbers for a b c and then the roots are given, after that i want to ask if they want to enter another 3 numbers to calculate.

for the code above, it should print ( If more data to be processed, type Y or y: ) and ( else type Q or q to quit: )

it does that fine, but it doesn't allow me to enter my answer, it just print the above then print this ( **** End of the program **** )

so i think i did something wrong in my last code, that doesn't allow the program to wait for an input for Y or y

so am stuck now

thanks though for the replies
• 09-08-2006
risby
Quote:

Originally Posted by Saarah
This is my final code

You are far too keen on that word 'final' :-)

Quote:

Originally Posted by Saarah
After giving the roots, it then prints out

Code:

```If more data to be processed, type Y or y: else type Q or q to quit:```
and it doesn't wait for an answer and ends the program

Can someone help me with this last part

I can't explain exactly, something to do with keyboard buffers and flushing and timing, but if you add a newline to your input format
Code:

`scanf("%f%f%f\n", &a, &b, &c);`
and then enter your values followed by a return and then an end-of-file character (i.e. ctrl-z for windows, ctrl-d for unix) and then another return, you will get the prompt.
Code:

```E:\Data\Source\work>quadratic2 This program will compute the roots of a quadratic equation of the form:         Ax^2 + Bx + C Enter A, B, and C with a space between each: 2 3 4 ^Z the roots are:  -0.750000 + 1.198958i, and -0.750000 - 1.198958i If more data to be processed, type Y or y: else type Q or q to quit: y This program will compute the roots of a quadratic equation of the form:         Ax^2 + Bx + C Enter A, B, and C with a space between each:```
Code:

```risby@tobago:~/src/work> ./quadratic2 This program will compute the roots of a quadratic equation of the form:         Ax^2 + Bx + C Enter A, B, and C with a space between each: 2 3 4 the roots are:  -0.750000 + 1.198958i, and -0.750000 - 1.198958i If more data to be processed, type Y or y: else type Q or q to quit: y This program will compute the roots of a quadratic equation of the form:         Ax^2 + Bx + C Enter A, B, and C with a space between each:```
Good luck with your final, final, final, completely finished and tested and working code ...
• 09-08-2006
Saarah
ThaaaaaaaNK You So muCh

it's working now with mee ,, am so happy :)

thanks again
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12