Need your help guys :'(

This is a discussion on Need your help guys :'( within the C Programming forums, part of the General Programming Boards category; Originally Posted by Saarah after I did some modification i got this final code I think your assessment of this ...

  1. #16
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    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.
    Last edited by risby; 09-06-2006 at 07:04 AM.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    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

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Just say "No!" to scanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    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

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    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

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    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

  9. #24
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    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 ...

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    12
    ThaaaaaaaNK You So muCh

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

    thanks again

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 04:47 PM
  2. How long have you guys been working with C/C++??
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 08-01-2003, 03:41 PM
  3. Tic Tac Toe -- Can you guys rate this please?
    By Estauns in forum Game Programming
    Replies: 2
    Last Post: 09-15-2001, 10:22 AM
  4. hello guys
    By lupi in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:42 AM
  5. hello guys
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 01:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21