What is wrong with this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Rudy

    #1

    What is wrong with this?

    What is wrong with this program? When I try to compile it, I get the
    following error. Compiler is gcc on FreeBSD.

    strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c
    e6-3.c: In function `main':
    e6-3.c:32: syntax error before `||'
    e6-3.c:33: syntax error before `printf'
    e6-3.c:36: syntax error before `||'
    e6-3.c:40: syntax error before `('
    e6-3.c:40: syntax error before `strcpy'
    e6-3.c:41: syntax error before `('
    e6-3.c:41: syntax error before `strcpy'
    e6-3.c:42: syntax error before `('
    e6-3.c:42: syntax error before `strcpy'
    e6-3.c:43: syntax error before `('
    e6-3.c:43: syntax error before `strcpy'
    e6-3.c:44: syntax error before `('
    e6-3.c:44: syntax error before `strcpy'
    e6-3.c:45: syntax error before `('
    e6-3.c:45: syntax error before `strcpy'
    e6-3.c:46: syntax error before `('
    e6-3.c:46: syntax error before `strcpy'
    e6-3.c:47: syntax error before `('
    e6-3.c:47: syntax error before `strcpy'
    e6-3.c:48: syntax error before `('
    e6-3.c:48: syntax error before `strcpy'
    e6-3.c:49: syntax error before `('
    e6-3.c:49: syntax error before `strcpy'
    e6-3.c:50: syntax error before `('
    e6-3.c:50: syntax error before `strcpy'
    e6-3.c:51: syntax error before `('
    e6-3.c:51: syntax error before `strcpy'
    strata:/home/dcrudy/c 1056 $$$ ->



    /*

    Programming Exercise 6-2: A professor generates letter
    grades by the following values: 0-60 F, 61-70 D, 71-80 C,
    81-90 B, 91-100 A. Given a numeric grade, print the
    letter. Also print +, blank, or - using the following
    values in the units place: 1-3 -, 4-7 blank, 8-0 +.

    Page 93

    */

    #include <stdio.h>
    #include <string.h>

    char line[100]; /* user data input line */
    char cgrade[10]; /* character grade */
    int ngrade; /* numeric grade */
    int result; /* final results (computed) */

    int main()
    {

    do
    {

    /* Get user input */
    printf("Input numeric grade: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &ngrade);

    /* Check to make sure that input is valid */
    if (ngrade < 0) || (ngrade > 100)
    printf("Invalid Input: You must input a number between 0 and
    100.\n");
    else break;
    }
    while (ngrade < 0) || (ngrade > 100);

    /* Now figure out what the letter grade is */
    if (ngrade <= 60) strcpy(cgrade, "F");
    if (ngrade >= 61) && (ngrade <= 63) strcpy(cgrade, "D-");
    if (ngrade >= 64) && (ngrade <= 67) strcpy(cgrade, "D");
    if (ngrade >= 68) && (ngrade <= 70) strcpy(cgrade, "D+");
    if (ngrade >= 71) && (ngrade <= 73) strcpy(cgrade, "C-");
    if (ngrade >= 74) && (ngrade <= 77) strcpy(cgrade, "C");
    if (ngrade >= 78) && (ngrade <= 80) strcpy(cgrade, "C+");
    if (ngrade >= 81) && (ngrade <= 83) strcpy(cgrade, "B-");
    if (ngrade >= 84) && (ngrade <= 87) strcpy(cgrade, "B");
    if (ngrade >= 88) && (ngrade <= 90) strcpy(cgrade, "B+");
    if (ngrade >= 91) && (ngrade <= 93) strcpy(cgrade, "A-");
    if (ngrade >= 94) && (ngrade <= 97) strcpy(cgrade, "A");
    if (ngrade >= 98) && (ngrade <= 100) strcpy(cgrade, "A+");

    /* Print the results */
    printf("Letter grade is %s.\n", cgrade);
    return(0);
    }


    --
    Daniel Rudy

    Remove nospam, invalid, and 0123456789 to reply.
  • Mark A. Odell

    #2
    Re: What is wrong with this?

    Daniel Rudy <dcrudy@invalid .pacbell.nospam .net.0123456789 > wrote in
    news:Verqc.6872 5$Cg6.49444@new ssvr25.news.pro digy.com:
    [color=blue]
    > What is wrong with this program? When I try to compile it, I get the
    > following error. Compiler is gcc on FreeBSD.
    >
    > strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c
    > e6-3.c: In function `main':
    > e6-3.c:32: syntax error before `||'
    > e6-3.c:33: syntax error before `printf'
    > e6-3.c:36: syntax error before `||'
    > e6-3.c:40: syntax error before `('
    > e6-3.c:40: syntax error before `strcpy'
    > e6-3.c:41: syntax error before `('
    > e6-3.c:41: syntax error before `strcpy'
    > e6-3.c:42: syntax error before `('
    > e6-3.c:42: syntax error before `strcpy'
    > e6-3.c:43: syntax error before `('
    > e6-3.c:43: syntax error before `strcpy'
    > e6-3.c:44: syntax error before `('
    > e6-3.c:44: syntax error before `strcpy'
    > e6-3.c:45: syntax error before `('
    > e6-3.c:45: syntax error before `strcpy'
    > e6-3.c:46: syntax error before `('
    > e6-3.c:46: syntax error before `strcpy'
    > e6-3.c:47: syntax error before `('
    > e6-3.c:47: syntax error before `strcpy'
    > e6-3.c:48: syntax error before `('
    > e6-3.c:48: syntax error before `strcpy'
    > e6-3.c:49: syntax error before `('
    > e6-3.c:49: syntax error before `strcpy'
    > e6-3.c:50: syntax error before `('
    > e6-3.c:50: syntax error before `strcpy'
    > e6-3.c:51: syntax error before `('
    > e6-3.c:51: syntax error before `strcpy'
    > strata:/home/dcrudy/c 1056 $$$ ->
    >
    >
    >
    > /*
    >
    > Programming Exercise 6-2: A professor generates letter
    > grades by the following values: 0-60 F, 61-70 D, 71-80 C,
    > 81-90 B, 91-100 A. Given a numeric grade, print the
    > letter. Also print +, blank, or - using the following
    > values in the units place: 1-3 -, 4-7 blank, 8-0 +.
    >
    > Page 93
    >
    > */
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > char line[100]; /* user data input line */
    > char cgrade[10]; /* character grade */
    > int ngrade; /* numeric grade */
    > int result; /* final results (computed) */
    >
    > int main()
    > {
    >
    > do
    > {
    >
    > /* Get user input */
    > printf("Input numeric grade: ");
    > fgets(line, sizeof(line), stdin);
    > sscanf(line, "%d", &ngrade);
    >
    > /* Check to make sure that input is valid */
    > if (ngrade < 0) || (ngrade > 100)[/color]
    if (ngrade < 0 || ngrade > 100)
    -- or --
    if ((ngrade < 0 || ngrade > 100))

    Two extra parens, or, two too few parens.

    --
    - Mark ->
    --

    Comment

    • Leor Zolman

      #3
      Re: What is wrong with this?

      On Tue, 18 May 2004 16:56:21 GMT, Daniel Rudy
      <dcrudy@invalid .pacbell.nospam .net.0123456789 > wrote:
      [color=blue]
      >What is wrong with this program? When I try to compile it, I get the
      >following error. Compiler is gcc on FreeBSD.
      >[/color]

      When you have multiple conditions, for any conditional expression, they
      must all be enclosed within an "outer" pair of parens. IOW,
      if (blah) && (blah) : no good
      if (blah && blah): ok
      if ((blah) && (blah)): also OK (but usually unnecessary due to
      precedence.)

      Same for whiles.
      -leor

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • Mark A. Odell

        #4
        Re: What is wrong with this?

        "Mark A. Odell" <odellmark@hotm ail.com> wrote in
        news:Xns94ED857 964257Copyright MarkOdell@130.1 33.1.4:
        [color=blue]
        >[color=green]
        >> int main()
        >> {
        >>
        >> do
        >> {
        >>
        >> /* Get user input */
        >> printf("Input numeric grade: ");
        >> fgets(line, sizeof(line), stdin);
        >> sscanf(line, "%d", &ngrade);
        >>
        >> /* Check to make sure that input is valid */
        >> if (ngrade < 0) || (ngrade > 100)[/color]
        > if (ngrade < 0 || ngrade > 100)
        > -- or --
        > if ((ngrade < 0) || (ngrade > 100))[/color]

        Ahem.

        --
        - Mark ->
        --

        Comment

        • Daniel Rudy

          #5
          Re: What is wrong with this?

          And somewhere around the time of 05/18/2004 10:07, the world stopped and
          listened as Leor Zolman contributed the following to humanity:[color=blue]
          > On Tue, 18 May 2004 16:56:21 GMT, Daniel Rudy
          > <dcrudy@invalid .pacbell.nospam .net.0123456789 > wrote:
          >
          >[color=green]
          >>What is wrong with this program? When I try to compile it, I get the
          >>following error. Compiler is gcc on FreeBSD.
          >>[/color]
          >
          >
          > When you have multiple conditions, for any conditional expression, they
          > must all be enclosed within an "outer" pair of parens. IOW,
          > if (blah) && (blah) : no good
          > if (blah && blah): ok
          > if ((blah) && (blah)): also OK (but usually unnecessary due to
          > precedence.)
          >
          > Same for whiles.
          > -leor
          >[/color]


          Ah, I see. The book has the following format:

          if (expression)
          statement;
          else
          statement;

          Which is sorta confusing at first glance, but it makes sense now. Thank
          you for the reply.

          --
          Daniel Rudy

          Remove nospam, invalid, and 0123456789 to reply.

          Comment

          • Leor Zolman

            #6
            Re: What is wrong with this?

            On Tue, 18 May 2004 17:24:13 GMT, Daniel Rudy
            <dcrudy@invalid .pacbell.nospam .net.0123456789 > wrote:
            [color=blue]
            >And somewhere around the time of 05/18/2004 10:07, the world stopped and
            >listened as Leor Zolman contributed the following to humanity:[/color]

            I'd love to be inside someone's mind when they see the above snippet in a
            Google Groups search result ;-)

            [color=blue]
            >Ah, I see. The book has the following format:
            >
            >if (expression)
            > statement;
            >else
            > statement;
            >
            >Which is sorta confusing at first glance, but it makes sense now. Thank
            >you for the reply.[/color]

            No prob,
            -leor


            --
            Leor Zolman --- BD Software --- www.bdsoft.com
            On-Site Training in C/C++, Java, Perl and Unix
            C++ users: download BD Software's free STL Error Message Decryptor at:
            An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

            Comment

            • Martin Ambuhl

              #7
              Re: What is wrong with this?

              Daniel Rudy wrote:[color=blue]
              > What is wrong with this program? When I try to compile it, I get the
              > following error. Compiler is gcc on FreeBSD.
              >
              > strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c
              > e6-3.c: In function `main':
              > e6-3.c:32: syntax error before `||'
              > e6-3.c:33: syntax error before `printf'[/color]
              [...]
              [color=blue]
              > if (ngrade < 0) || (ngrade > 100)
              > printf("Invalid Input: You must input a number between 0 and
              > 100.\n");[/color]
              [...]

              A selection-statement is one of
              if ( expression ) statement
              if ( expression ) statement else statement
              switch ( expression ) statement
              and
              The controlling expression of an if statement shall have scalar type.

              For your code[color=blue]
              > if (ngrade < 0) || (ngrade > 100)[/color]
              to be right,
              ngrade < 0) || (ngrade > 100
              would have to be an expression. It is not.
              ngrade < 0 || ngrade > 100
              and
              (ngrade < 0) || (ngrade > 100)
              are expressions, yielding the if-statements
              if (ngrade < 0 || ngrade > 100) { /* statement */ }
              and
              if ((ngrade < 0) || (ngrade > 100)) { /* statement */ }

              The error on the next line is a "cascading error", the result of an
              attempt to parse the broken line before it. Unless, of course, the
              end-of-line between 'and' and '100' is in the original and not an
              artifact of newsposting software.

              Comment

              Working...