Help. Where is my error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Red Dragon

    Help. Where is my error?

    I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery.
    Why does the computer refuse to execute my scanf ("%c",&q);
    On input 3 4 1 (for a,b and c) I had real roots OK
    On input 1 8 16 I had same real roots OK.

    However on 4 2 5, (for imaginary roots ) the computer cannot see the scanf ("%c",&q); statement. It just jumps over it.
    How can I make the computer not to ignore this statement? I am on Visual C++ platform.
    Thanks
    Khoon.

    /* Roots of a Quadratic Equation.
    12.10.05 */

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main (void)

    {
    int a; int b; int c; float x1; float x2; int E; int E1; float R; float I;float S;
    char p; char q; char y;

    printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
    printf ("equation ax%c+bx+c=0 :",253);
    scanf ("%d%d%d", &a,&b,&c);

    E =(b*b)-(4*a*c);

    if ( E > 0)
    {
    x1 = (float)(-b+sqrt(E))/(2*a);
    x2 = (float)(-b-sqrt(E))/(2*a);

    printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x 2);
    }



    else if (E == 0)
    {

    x1 = (float)(-b+sqrt(E))/(2*a);

    printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n", x1);
    }

    else
    {

    p = 'y';

    printf ("Your quadratic equation has two distinct imaginary roots. Do you want to know\n");
    printf ("the values of the imaginary roots (Y/N)?");

    scanf ("%c",&q);

    printf ("\nq = %c\n",q);/* Test statement*/

    if (p==q)
    printf ("OK I will show your the imaginary roots tomorrow.\n");

    else
    printf ("Good bye\n");

    return 0;

    }
    }

  • Geoff Turner

    #2
    Re: Help. Where is my error?


    "Red Dragon" <tskhoon@stream yx.com> wrote in message[color=blue]
    >news:4353ae06_ 1@news.tm.net.m y...
    >I am self study C student. I got stuck in the program below on quadratic[/color]
    equation and will be >most grateful if someone could help me to unravel the
    mystery.[color=blue]
    >Why does the computer refuse to execute my scanf ("%c",&q);
    >On input 3 4 1 (for a,b and c) I had real roots OK
    >On input 1 8 16 I had same real roots OK.[/color]
    [color=blue]
    >However on 4 2 5, (for imaginary roots ) the computer cannot see the[/color]
    scanf ("%c",&q); >statement. It just jumps over it.[color=blue]
    >How can I make the computer not to ignore this statement? I am on Visual[/color]
    C++ platform.[color=blue]
    >Thanks
    >Khoon.[/color]
    [color=blue]
    >/* Roots of a Quadratic Equation.
    > 12.10.05 */[/color]
    [color=blue]
    >#include <stdio.h>
    >#include <stdlib.h>
    >#include <math.h>[/color]
    [color=blue]
    >int main (void)[/color]
    [color=blue]
    >{
    >int a; int b; int c; float x1; float x2; int E; int E1; float R; float[/color]
    I;float S;[color=blue]
    >char p; char q; char y;[/color]
    [color=blue]
    >printf ("Please key in the value of constant a,b and c for finding the[/color]
    roots of quadratic");[color=blue]
    > printf ("equation ax%c+bx+c=0 :",253);
    >scanf ("%d%d%d", &a,&b,&c);[/color]
    [color=blue]
    > E =(b*b)-(4*a*c);[/color]
    [color=blue]
    > if ( E > 0)
    > {
    > x1 = (float)(-b+sqrt(E))/(2*a);
    > x2 = (float)(-b-sqrt(E))/(2*a);[/color]
    [color=blue]
    > printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f[/color]
    ,x2=%1.6f",x1,x 2);[color=blue]
    > }[/color]
    [color=blue]
    > else if (E == 0)
    > {[/color]
    [color=blue]
    > x1 = (float)(-b+sqrt(E))/(2*a);[/color]
    [color=blue]
    > printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n", x1);
    > }[/color]
    [color=blue]
    > else
    > {[/color]
    [color=blue]
    > p = 'y';[/color]
    [color=blue]
    > printf ("Your quadratic equation has two distinct imaginary roots. Do[/color]
    you want to know\n");[color=blue]
    > printf ("the values of the imaginary roots (Y/N)?");[/color]

    /*************** *************** *************** ***********
    fflush (stdin );
    /*************** *************** *************** ***********[color=blue]
    > scanf ("%c",&q);[/color]
    [color=blue]
    > printf ("\nq = %c\n",q);/* Test statement*/[/color]
    /snip>


    Comment

    • Walter Roberson

      #3
      Re: Help. Where is my error?

      In article <dj0cbe$nc$1@bl ackmamba.itd.rl .ac.uk>,
      Geoff Turner <g.s.turner@rl. ac.uk> wrote:
      [color=blue]
      > fflush (stdin );[/color]

      No! fflush() has no defined behaviour on an input stream!!

      --
      I am spammed, therefore I am.

      Comment

      • Walter Roberson

        #4
        Re: Help. Where is my error?

        In article <4353ae06_1@new s.tm.net.my>,
        Red Dragon <tskhoon@stream yx.com> wrote:[color=blue]
        >I am self study C student. I got stuck in the program below on
        >quadratic equation and will be most grateful if someone could help me to
        >unravel the mystery.
        >Why does the computer refuse to execute my scanf ("%c",&q);[/color]

        We go through this about every second day, and it is surely
        in the FAQ.

        My explanation of a week ago can be found at


        --
        Is there any thing whereof it may be said, See, this is new? It hath
        been already of old time, which was before us. -- Ecclesiastes

        Comment

        • Michael Mair

          #5
          Re: Help. Where is my error?

          Red Dragon wrote:[color=blue]
          > I am self study C student. I got stuck in the program below on
          > quadratic equation and will be most grateful if someone could help me to
          > unravel the mystery.
          > Why does the computer refuse to execute my * scanf ("%c",&q); *
          > On input 3 4 1 (for a,b and c) I had real roots OK
          > On input 1 8 16 I had same real roots OK.
          >
          > However on 4 2 5, (for imaginary roots ) the computer cannot see the
          > *scanf ("%c",&q);* statement. It just jumps over it.
          > How can I make the computer not to ignore this statement? I am on
          > Visual C++ platform.
          > Thanks
          > Khoon.[/color]
          <snip!>

          You have been repeatedly asked not to post using HTML.
          Please stop it. This is usenet, not some mailing list or forum.

          Read the FAQ on input using scanf() -- it is not advisable.
          At all.

          Apart from that: You did not look at the return value of scanf()
          so how do you know what happened? You also seem not to have used
          the debugger which comes with your IDE.

          -Michael
          --
          E-Mail: Mine is an /at/ gmx /dot/ de address.

          Comment

          • Keith Thompson

            #6
            Re: Help. Where is my error?

            "Red Dragon" <tskhoon@stream yx.com> writes:[color=blue]
            > I am self study C student. I got stuck in the program below on quadratic
            > equation and will be most grateful if someone could help me to unravel the
            > mystery.
            > Why does the computer refuse to execute my scanf ("%c",&q);[/color]

            It doesn't. Your call to scanf("%c", &q) is doing exactly what it's
            supposed to do.

            To find out what it's supposed to do, read the documentation for
            scanf().

            You're almost certainly better off using a different method to read
            input. A common technique is to use fgets() to read a line at a time,
            then use sscanf() to parse the string. (This can have its own
            drawbacks; fgets() either leaves the newline character in the input
            string, or truncates the line if it's too long.)

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Old Wolf

              #7
              Re: Help. Where is my error?

              Red Dragon wrote:[color=blue]
              > I am self study C student. I got stuck in the program below on
              > quadratic equation and will be most grateful if someone could help
              > me to unravel the mystery.
              > Why does the computer refuse to execute my scanf ("%c",&q);[/color]

              You posted this program a couple of weeks ago and got lots
              of advice. But you seem to have ignored most of this advice.
              Here it is again:

              1) x1, x2, R, I, S should be "double", not "float"
              2) You MUST check the return value of scanf() and take appropriate
              action if it is wrong
              3) Don't do any casting (eg. in the line "x1 = (float)FOO")
              4) The "return 0" goes AFTER the "else" block, not inside it.

              Hint: If you printf the result of your scanf("%c") using
              %d, you might understand what is going on.

              Comment

              • Red Dragon

                #8
                Re: Help. Where is my error?

                > We go through this about every second day, and it is surely[color=blue]
                > in the FAQ.
                >
                > My explanation of a week ago can be found at
                > http://groups.google.ca/group/comp.l...65f0744a89f300
                >
                > --
                >Quote from Google: Is there any thing whereof it may be said, See, this is new? It hath[/color]
                scanf() with a %c format element reads *exactly* one character. You
                are entering two characters, the input you want and the newline to
                terminate the line. Thus after the first scanf(), you still have
                a character in the input buffer waiting to be read by the second
                scanf().

                Dear Walter,
                Yes Mr. Walter. This is the exact problem I am having. I dont quite understand the phrase "newline to terminate the line". What is it? It is the "\n" thing? Or a "space"? Can you explain what is the "newline to terminate the line"? How shall I solve the problem? I can use two scanf() line and it will solve the problem. The first scanf() reads the "newline... " and the second scanf() will read my input. Is this the normal way to solve the problem? What is the proper way? Sorry for asking you so many questions.
                Thank you.
                Khoon.







                Comment

                • Red Dragon

                  #9
                  Re: Help. Where is my error?

                  >[color=blue]
                  > You have been repeatedly asked not to post using HTML.
                  > Please stop it. This is usenet, not some mailing list or forum.
                  >
                  > Read the FAQ on input using scanf() -- it is not advisable.
                  > At all.
                  >
                  > Apart from that: You did not look at the return value of scanf()
                  > so how do you know what happened? You also seem not to have used
                  > the debugger which comes with your IDE.
                  >
                  > -Michael
                  > --
                  > E-Mail: Mine is an /at/ gmx /dot/ de address.[/color]

                  Mr. Michael,
                  I wish to apologize for causing you problem. I did not know I was causing a
                  problem.
                  1. The dont understand the HTML thing. I did not use HTML. What I did was
                  to copy from my Visual C++ platform and paste on the Outlook Express
                  screen. With Mr. Skarmander's instruction, I have set the radio button to
                  "Plain Text" on the Send Tab and thought the problem is solved. I have
                  actually sent a copy of the outgoing mail to myself and I dont see any HTML
                  thing on my screen on the returned copy. So I dont know what else to do.

                  2. Yes. I tried to look at the return value of scanf() but got nothing
                  because the computer skipped it . As I just found out, it actually read
                  the "newline" (I dont know what it is ) so it registered nothing, creating
                  the problem in question.

                  3. What is debugger and IDE.? Hope you can bear with me because I am new
                  to all this and am doing self study. I have nobody else to ask.

                  Thank you and sorry again.
                  Khoon.





                  Comment

                  • Red Dragon

                    #10
                    Re: Help. Where is my error?


                    "Keith Thompson" <kst-u@mib.org> wrote in message news:lny84rq5s1 .fsf@nuthaus.mi b.org...[color=blue]
                    > "Red Dragon" <tskhoon@stream yx.com> writes:[color=green]
                    >> I am self study C student. I got stuck in the program below on quadratic
                    >> equation and will be most grateful if someone could help me to unravel the
                    >> mystery.
                    >> Why does the computer refuse to execute my scanf ("%c",&q);[/color]
                    >
                    > It doesn't. Your call to scanf("%c", &q) is doing exactly what it's
                    > supposed to do.
                    >
                    > To find out what it's supposed to do, read the documentation for
                    > scanf().[/color]
                    I have read the documentation in the Google as recommended by Walter. I understand now what has happened. On program running, my scanf() read the "newline" instruction in the buffer and jumped to the next statement. So I dont have a chance to make an input. To solve the problem, I can put in 2 scanf()s. But is this the correct way?
                    Rgds,
                    Khoon.
                    [color=blue]
                    > You're almost certainly better off using a different method to read
                    > input. A common technique is to use fgets() to read a line at a time,
                    > then use sscanf() to parse the string. (This can have its ow
                    > drawbacks; fgets() either leaves the newline character in the input
                    > string, or truncates the line if it's too long.)[/color]


                    Comment

                    • Red Dragon

                      #11
                      Re: Help. Where is my error?


                      "Old Wolf" <oldwolf@inspir e.net.nz> wrote in message news:1129584706 .068328.31530@g 43g2000cwa.goog legroups.com...[color=blue]
                      > Red Dragon wrote:[color=green]
                      >> I am self study C student. I got stuck in the program below on
                      >> quadratic equation and will be most grateful if someone could help
                      >> me to unravel the mystery.
                      >> Why does the computer refuse to execute my scanf ("%c",&q);[/color]
                      >
                      > You posted this program a couple of weeks ago and got lots
                      > of advice. But you seem to have ignored most of this advice.
                      > Here it is again:[/color]
                      [color=blue]
                      > 1) x1, x2, R, I, S should be "double", not "float"[/color]

                      Thank you for your help and I very much appreciate it. I am doing the exercise in my book and since precision is not an issue here, I have put it on lower priority. But as you rightly point out. I will adopt it as I will be dealing with big figures.
                      [color=blue]
                      > 2) You MUST check the return value of scanf() and take appropriate
                      > action if it is wrong[/color]

                      I was doing this. That is why I call it "test statement".
                      [color=blue]
                      > 3) Don't do any casting (eg. in the line "x1 = (float)FOO")
                      > 4) The "return 0" goes AFTER the "else" block, not inside it.[/color]

                      Thanks
                      Khoon.

                      Comment

                      • pete

                        #12
                        Re: Help. Where is my error?

                        Red Dragon wrote:[color=blue]
                        >
                        >
                        > "Old Wolf" <oldwolf@inspir e.net.nz> wrote in message
                        > news:1129584706 .068328.31530@g 43g2000cwa.goog legroups.com...[/color]
                        [color=blue][color=green]
                        > > You posted this program a couple of weeks ago and got lots
                        > > of advice. But you seem to have ignored most of this advice.
                        > > Here it is again:
                        > > 1) x1, x2, R, I, S should be "double", not "float"[/color]
                        >
                        > Thank you for your help and I very much appreciate it. I am doing the
                        > exercise in my book and since precision is not an issue here, I have
                        > put it on lower priority. But as you rightly point out. I will
                        > adopt it as I will be dealing with big figures.[/color]

                        What are you waiting for?
                        Unless you have an array of them,
                        small arithmetic types like float, short, and char,
                        which often get quietly converted to larger types,
                        are very probably a poor choice.

                        [color=blue][color=green]
                        > > 2) You MUST check the return value of scanf() and take appropriate
                        > > action if it is wrong[/color]
                        >
                        > I was doing this. That is why I call it "test statement".[/color]

                        int rc;

                        rc = scanf("%c", &q);
                        printf("scanf returned a value of %d\n", rc); /* Test statement*/

                        --
                        pete

                        Comment

                        • Red Dragon

                          #13
                          Re: Help. Where is my error?

                          I have got all the results by using 2 scanf(). One is a dummy. I am also using double instead of float.
                          Below are the results. I am copying and paste from Notepad, so I hope I dont have HTML problem.
                          Regards,
                          Khoon


                          /* Roots of a Quadratic Equation.
                          12.10.05 */

                          #include <stdio.h>
                          #include <stdlib.h>
                          #include <math.h>

                          int main (void)

                          {
                          int a; int b; int c; double x1; double x2; double E; int E1; double R; double I;double S;
                          char q; char Y;

                          printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
                          printf ("equation ax%c+bx+c=0 :",253);
                          scanf ("%d%d%d", &a,&b,&c);

                          E =(b*b)-(4*a*c);

                          if ( E > 0)
                          {
                          x1 = (float)(-b+sqrt(E))/(2*a);
                          x2 = (float)(-b-sqrt(E))/(2*a);

                          printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x 2);
                          }

                          else if (E == 0)
                          {

                          x1 = (float)(-b+sqrt(E))/(2*a);

                          printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n", x1);
                          }

                          else

                          {

                          printf ("Your quadratic equation has two distinct imaginary roots. Do you want to know");
                          printf ("\nthe values of the imaginary roots (Y/N)?");

                          scanf ("%c",&q); /* Dummy. The computer jumps this scanf() */

                          scanf ("%c",&q);

                          printf ("q = %c\n",q); /* Test statement*/

                          if ('Y'==q)
                          {
                          R = (float)-b/(2*a);
                          S=abs(E);
                          S=sqrt(S);
                          I = S/(2*a);
                          printf ("\nThe imaginary roots are:\n");
                          printf (" x1=%1.6f + %1.6fi , x2=%1.6f - %1.6fi\n",R,I,R ,I);
                          }
                          else
                          printf ("Thank you for using this computer\n");
                          }
                          return 0;
                          }

                          /* RESULT
                          Please key in the value of constant a,b and c for finding the roots of quadratic
                          equation ax²+bx+c=0 :3 4 1

                          Your quadratic equation has two distinct real roots: x1=-0.333333 ,x2=-1.000000
                          Press any key to continue */

                          /*Please key in the value of constant a,b and c for finding the roots of quadratic
                          equation ax²+bx+c=0 :1 8 16

                          Your quadratic equation has two same: x1=x2=-4.000000
                          Press any key to continue */

                          /*Please key in the value of constant a,b and c for finding the roots of quadrati
                          equation ax²+bx+c=0 :4 2 5
                          Your quadratic equation has two distinct imaginary roots. Do you want to know
                          the values of the imaginary roots (Y/N)?Y
                          q = Y

                          The imaginary roots are:
                          x1=-0.250000 + 1.089725i , x2=-0.250000 - 1.089725i
                          Press any key to continue*/

                          /*Please key in the value of constant a,b and c for finding the roots of quadratic
                          equation ax²+bx+c=0 :4 2 5
                          Your quadratic equation has two distinct imaginary roots. Do you want to know
                          the values of the imaginary roots (Y/N)?N
                          q = N
                          Thank you for using this computer
                          Press any key to continue*/


                          Comment

                          • Christopher Benson-Manica

                            #14
                            Re: Help. Where is my error?

                            Red Dragon <tskhoon@stream yx.com> wrote:
                            [color=blue]
                            > 1. The dont understand the HTML thing. I did not use HTML. What I did was
                            > to copy from my Visual C++ platform and paste on the Outlook Express
                            > screen. With Mr. Skarmander's instruction, I have set the radio button to
                            > "Plain Text" on the Send Tab and thought the problem is solved. I have
                            > actually sent a copy of the outgoing mail to myself and I dont see any HTML
                            > thing on my screen on the returned copy. So I dont know what else to do.[/color]

                            Well, whatever you've done worked for this post, at least - we'll let
                            you know if the problem occurs again.

                            --
                            Christopher Benson-Manica | I *should* know what I'm talking about - if I
                            ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                            Comment

                            • Red Dragon

                              #15
                              Re: Help. Where is my error?

                              > What are you waiting for?[color=blue]
                              > Unless you have an array of them,
                              > small arithmetic types like float, short, and char,
                              > which often get quietly converted to larger types,
                              > are very probably a poor choice.
                              >
                              >[color=green][color=darkred]
                              >> > 2) You MUST check the return value of scanf() and take appropriate
                              >> > action if it is wrong[/color]
                              >>
                              >> I was doing this. That is why I call it "test statement".[/color]
                              >
                              > int rc;
                              >
                              > rc = scanf("%c", &q);
                              > printf("scanf returned a value of %d\n", rc); /* Test statement*/
                              >
                              > --
                              > pete[/color]

                              Hi Pete,
                              I have already been using double.
                              As for the test, I find that for all inputs of q, "rc" registers 1.
                              So what is it suppose to test? What does it suppose to show?
                              I cannot see how to make use of it because everything I do, "scanf returned
                              a value of 1".
                              Thanks
                              Khoon.





                              Comment

                              Working...