Having trouble with my code for hw assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kookai
    New Member
    • Jul 2006
    • 4

    Having trouble with my code for hw assignment

    I need help with this problem. I have tried writting the program, i just can not get it to run properly.

    Please Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90 - 100, 3 if the average is 80 - 89, 2 if the average is 70 - 79, 1 if the average is 60 - 69 and 0 if the average is lower than 60.

    Look tried
    put where the error?

    #include <iostream.h>
    void main ()
    {
    int s;

    if ((s>=90) && (s <=100))
    {
    return 4;
    }
    else if((s>=80) && (s<=89))
    {
    return 3;
    }
    else if ((s>=70) && (s<=79))
    {
    return 2;
    }
    else if ((s>=60) && (s<=69))
    {
    return 1;
    }

    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    You never input s.

    How about
    Code:
    cout << "Enter student's grade (0-100)" << endl;
    cin >> s;
    Then use the logic. Of course, a faster way would be
    Code:
    if((s >= 0) && (s <= 100))
    {
      s -= 60;
      s /= 10;
      if(s == 5) // only needed when a student gets 100%
        s--;
      cout << s << endl;
    }

    Comment

    • Ashish_CPP
      New Member
      • Jul 2006
      • 35

      #3
      Originally posted by kookai
      I need help with this problem. I have tried writting the program, i just can not get it to run properly.

      Please Write a function qualityPoints that inputs a student's average and returns 4 if a student's average is 90 - 100, 3 if the average is 80 - 89, 2 if the average is 70 - 79, 1 if the average is 60 - 69 and 0 if the average is lower than 60.

      Look tried
      put where the error?

      #include <iostream.h>
      void main ()
      {
      int s;

      if ((s>=90) && (s <=100))
      {
      return 4;
      }
      else if((s>=80) && (s<=89))
      {
      return 3;
      }
      else if ((s>=70) && (s<=79))
      {
      return 2;
      }
      else if ((s>=60) && (s<=69))
      {
      return 1;
      }

      }

      Hi,

      First thing is that u r trying to return from main( ); which is having void return type.

      Instead of using so many if, else conditions u can try this

      if(s>59)
      return( (((s-60)/10)+1) );
      else
      return 0;

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by D_C
        Code:
        if((s >= 0) && (s <= 100))
        {
          s -= 60;
          s /= 10;
          if(s == 5) // only needed when a student gets 100%
            s--;
          cout << s << endl;
        }
        You have an error and return the wrong values show in the following table

        Code:
                               Return Value
        Score (range)     Required     From Code
        60 - 69               1            0
        70 - 79               2            1
        80 - 89               3            2
        90 - 99               4            3
        100                   4            4
        Your s==5 case never happens.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by Ashish_CPP
          First thing is that u r trying to return from main( ); which is having void return type.
          In fact the problem is with the declaration of main.

          in C main must be declared as

          Code:
          int main(int argc, char *argv[ ])
          {
              /*program-statements */
          }
          This works for C++ to however when using C++ you also have the option of declaring main as

          Code:
          int main()
          {
              /*program-statements */
          }
          In all cases main must return int. Not returning int from main invokes undefined behaviour (which is not a good thing).

          Unless you have any specific return value in mind then you should return

          EXIT_SUCCESS

          or

          EXIT_FAILURE

          from main. The are portable return values and should be defined in stdlib.h

          Comment

          • dna5122
            New Member
            • Jul 2006
            • 12

            #6
            I think you guys are giving kookai too much information too early in the game. If the assignment is to write that function then a ladder of if-else statements is probably a good thing. If the assignment is to write a gradebook application then the condensed arithmetic is more impressive.

            It should be noted that not returning an int does not always result in undefined behavior. Windows is fine with void main() { return; }, but I know other operating systems aren't.

            Comment

            • mkane
              New Member
              • Oct 2006
              • 1

              #7
              Here's a solution that's probably close to what your teacher was expecting. The changes I made to your code:
              1. put your logic in a function
              2. added return 0;
              3. called the function from main.

              [CODE}
              #include <iostream.h>

              int qualityPoints()
              {
              int s;

              cout << "Enter student's grade (0-100)" << endl;
              cin >> s;

              if ((s >= 90) && (s <= 100))
              {
              return 4;
              }
              else if ((s >= 80) && (s <= 89))
              {
              return 3;
              }
              else if ((s >= 70) && (s <= 79))
              {
              return 2;
              }
              else if ((s >= 60) && (s <= 69))
              {
              return 1;
              }
              return 0;
              }


              void main ()
              {
              int points = qualityPoints() ;
              cout << "Points: " << points << endl;
              return;
              }

              [/CODE]

              Comment

              Working...