Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Problem in C++ -- spitting nonsense

Name: Xero 2006-10-06 6:07

Script started on Fri Oct 06 05:52:56 2006
~ $ cat -n gradebookattempt2.cpp

     1  //CPS 181

     2  //

     3  //Quizbook Assignment

     4  //   The purpose of this assignment is to design a gradebook application, which will take gradebook input from the user, for two quizzes, a midterm, and a final, and will then give the user their semester grade.

     5  //Wish me luck in programming this.

     6 

     7  //Declare libraries...

     8 

     9  #include <iostream>

    10  using namespace std;

    11 

    12  //Declare your class...

    13 

    14  class Grades

    15  {

    16  private:

    17  int q1;

    18  int q2;

    19  int mid;

    20  int final;

    21  double avg;

    22  char lgrade;

    23  public:

    24  Grades()

    25  {

    26  q1 = 0;

    27  q2 = 0;

    28  mid = 0;

    29  final = 0;

    30  }

    31    void setq1(int quiz1);

    32    void setq2(int quiz2);

    33    void setmid(int midterm);

    34    void setfin(int fin);

    35    void setavg();

    36    char setlgrade();

    37    int getq1();

    38    int getq2();

    39    int getmid();

    40    int getfin();

    41    double getavg();

    42    char getlgrade();

    43  };

    44 

    45  //Begin your main prog...

    46 

    47  int main()

    48  {

    49    Grades scores; //Object -- define an instance of the class...

    50 

    51    int rec_q1, rec_q2, rec_mid, rec_fin;

    52 

    53    cout << "What is your grade out of 10 points from Quiz 1?" << endl;

    54    cin >> rec_q1;

    55    cout << "What is your grade out of 10 points from Quiz 2?" << endl;

    56    cin >> rec_q2;

    57    cout << "What is your grade out of 100 points from the Midterm?" << endl;

    58    cin >> rec_mid;

    59    cout << "What is your grade out of 100 points from the Final?" << endl;

    60    cin >> rec_fin;

    61    scores.setq1(rec_q1);

    62    scores.setq2(rec_q2);

    63    scores.setmid(rec_mid);

    64    scores.setfin(rec_fin);

    65    cout << "Here is your data from your work:" << endl;

    66    cout << "Quiz 1: " << scores.getq1() << endl;

    67    cout << "Quiz 2: " << scores.getq2() << endl;

    68    cout << "Midterm: " << scores.getmid() << endl;

    69    cout << "Final: " << scores.getfin() << endl;

    70    cout << "Your semester grade is as follows: " << scores.getavg() << "% which is a(n) " << scores.getlgrade() << endl;

    71  cout << endl;

    72    return 0;

    73  }

    74 

    75  //Now we deal with the member functions...

    76 

    77  void Grades::setq1(int quiz1)

    78  {

    79    q1 = quiz1;

    80  }

    81 

    82  void Grades::setq2(int quiz2)

    83  {

    84    q2 = quiz2;

    85  }

    86 

    87  void Grades::setmid(int midterm)

    88  {

    89    mid = midterm;

    90  }

    91 

    92  void Grades::setfin(int fin)

    93  {

    94    final = fin;

    95  }

    96 

    97  int Grades::getq1()

    98  {

    99    return q1;

   100  }

   101 

   102  int Grades::getq2()

   103  {

   104    return q2;

   105  }

   106 

   107  int Grades::getmid()

   108  {

   109    return mid;

   110  }

   111 

   112  int Grades::getfin()

   113  {

   114    return final;

   115  }

   116 

   117  void Grades::setavg()

   118  {

   119    int qcombine, qdivide;

   120    double qpercent, midpercent, finalpercent;

   121 

   122    qcombine = (q1 + q2);

   123    qdivide = ((qcombine)/(20))*100;

   124    qpercent = ((qdivide)/(4));

   125 

   126    midpercent = ((mid)/(4));

   127 

   128    finalpercent = ((final)/(2));

   129 

   130    avg = (finalpercent + midpercent + qpercent);

   131  }

   132 

   133  double Grades::getavg()

   134  {

   135    return avg;

   136  }

   137 

   138  char Grades::setlgrade()

   139  {

   140    if (avg <= 59)

   141      lgrade = 'F';

   142    else if ((avg >= 60) && (avg <= 69))

   143      lgrade = 'D';

   144    else if ((avg >= 70) && (avg <= 79))

   145      lgrade = 'C';

   146    else if ((avg >= 80) && (avg <= 89))

   147      lgrade = 'B';

   148    else if ((avg >= 90) && (avg <= 100))

   149      lgrade = 'A';

   150  }

   151 

   152  char Grades::getlgrade()

   153  {

   154    return lgrade;

   155  }

- $ g++ gradebookattempt2.cpp

~ $ a.out

What is your grade out of 10 points from Quiz 1?

9

What is your grade out of 10 points from Quiz 2?

8

What is your grade out of 100 points from the Midterm?

98

What is your grade out of 100 points from the Final?

88

Here is your data from your work:

Quiz 1: 9

Quiz 2: 8

Midterm: 98

Final: 88

Your semester grade is as follows: 5.22764e-270% which is a(n) Ì



~ $ exit

exit


script done on Fri Oct 06 05:53:45 2006

------------------------------------------------------------

As you can see, user inputs two quiz grades, a midterm, and a final. Both quizes combine into 20pts, and is worth 25% of the "semester" grade. Midterm = 25% "semester" grade. Final = 50% "semester" grade... and yet, even though I cannot find a single flaw in my code, it's spitting back nonsense at me, instead of a grade. bah. Halp?

Name: Anonymous 2006-10-06 8:18

1: Your code is like liquid pain running through my veins.
2: you're calling getavg() and getlgrade() which return unitialized variables, which will naturally result in garbage.

Name: Anonymous 2006-10-06 9:01 (sage)

wtf are those weird characters? and wtf is . in your path?
i can already tell your post is full of MASSIVE FAIL, without even looking at your code.

Name: Anonymous 2006-10-06 11:15 (sage)

>>1

Take a Java course; you do not belong in this world.

Name: Anonymous 2006-10-06 13:23

like >>2 said, you never call setavg() and setlgrade(), and you never initialized the avg and lgrade variables, so you are just getting random bits.

Name: Anonymous 2006-10-07 4:34

-Weffc++ is more helpful than you might think at first :)


$ g++ -Wall -Wextra -Weffc++ -pedantic -O2 test.cpp
test.cpp: In constructor ‘Grades::Grades()’:
test.cpp:48: warning: ‘Grades::q1’ should be initialized in the member initialization list
test.cpp:48: warning: ‘Grades::q2’ should be initialized in the member initialization list
test.cpp:48: warning: ‘Grades::mid’ should be initialized in the member initialization list
test.cpp:48: warning: ‘Grades::final’ should be initialized in the member initialization list
test.cpp:48: warning: ‘Grades::avg’ should be initialized in the member initialization list
test.cpp:48: warning: ‘Grades::lgrade’ should be initialized in the member initialization list
test.cpp: In member function ‘char Grades::setlgrade()’:
test.cpp:300: warning: control reaches end of non-void function

Name: Anonymous 2006-10-09 2:24

INITIALIZE YOUR VARIABLES

Name: Anonymous 2006-10-10 7:54

HOW DO I INITIALIZED VARIABLES

Name: Anonymous 2006-10-10 9:44

>>8
Use memset

Name: Anonymous 2006-10-10 21:46

>>9 No, I think the compiler wants:
Grades::Grades() : q1(0), q2(0), mid(0), final(0) { }

Name: Anonymous 2006-10-11 4:39

>>10
I was attempting to troll with bad practices

Name: Anonymous 2006-10-11 5:56

>>11

Your humour failed on these idiots

Name: Anonymous 2006-10-11 12:20

>>12
Thats because /prog/ is full of Python and Ruby fanboys who don't know what memory is or how it works.

Name: Anonymous 2006-10-11 14:53

>>10 is right for the constructor form, the cmopiler will optimize everything, pack all the values in the structure without calling the constructor.

Don't forget to inline your get/set accessors, and put your class in a separate include file.

Name: Anonymous 2006-10-11 22:31

>>14
if your compiler doesn't suck ass and the get/set accessors aren't 1000 lines long, the compiler should automatically inline them.

Name: Anonymous 2006-10-11 22:54

>>15

$ gcc --fsuck-anus

It feels better than you think.

Name: Anonymous 2006-10-13 15:05

>>15
have you ever looked at assembly code once in your life?

Name: Anonymous 2006-10-15 3:44

my eyes are bleeding.. your teacher should kill himself.

Name: Anonymous 2006-10-15 17:04

Your semester grade is as follows: 5.22764e-270%
It sure will be

Name: Anonymous 2006-10-16 3:53

>>17
What does reading assembly code have to do with compilers optimizing code?

Name: Anonymous 2006-10-16 5:12

>>20
Not much, but it helps to verify whether they're doing a decent job or not.

Name: Anonymous 2009-01-14 14:06

Sepples

Name: Anonymous 2010-11-15 12:42

Name: Sgt.Kabuㅥ옑kiman渿▼ 2012-05-28 20:25

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List