Simple GPA calculator program

R

rmr531

I have an assignment for a class that has been driving me crazy for the
past few days. Basically I need to create a simple GPA calculator that
takes in several letter grades (ex: A) and then calculates the GPA
based off that A=4 B=3 C=2 D=1 F=0. Here is what I have, sorry it in
inefficient sloppy code but I am still new to this and just want it to
work.

print "Enter the letter grade (A, B, C, D, F) for each test taken in a
class, enter Z to quit.\n";

****************************************
$score=0;
$A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";
for($classes=0, $grade != $Z, $classes++)
{
$grade;
print "Enter a grade: ";
chomp($grade = <STDIN> );

if($grade == $A)
{$score + 4;}
if($grade == $B)
{$score + 3;}
if($grade == $C)
{$score + 2;}
if($grade == $D)
{$score + 1;}
else {$score + 0;}
}

$gpa=$score/$classes;
print "Your GPA is: $gpa.\n";
*******************************************************
Any help on this one is greatly appreciated, I know many of you out
there are PERL experts but if you could make the solutions very basic
that would be great, remember this is a beginner class :) I know it is
something stupid that I did but I cannot figure it out, I think that my
logic is sound. Thanks all.
 
J

John Bokma

I have an assignment for a class

I would probably give you a D for trying.

(With your program an F is not possible).

One tip: == is for numbers, eq is for strings.
 
T

Tad McClellan

I have an assignment for a class


If we do your assignment for you, then you won't learn what
you are supposed to learn.

We would hate to see you short-changed like that.

You pay good money in tuition to learn stuff, so you should *learn* stuff.

but I am still new to this and just want it to
work.


You will fail in programming with such an attitude.

Consider changing majors.

Someone destined to become a programmer would want to
_understand_ how it works, while you appear to be looking
for cargo-cult programming...

$A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";


You can compare to strings just as you would compare to
a variable.

There is no need to copy the string into a variable before
doing a comparison.

for($classes=0, $grade != $Z, $classes++)
^^^^^^^^^^^^
^^^^^^^^^^^^ $grade ne 'Z'

!= is for comparing numbers, ne is for comparing strings.
{
$grade;


Why do you include that line of code?

That is, what do you think that that line does for you?

(this is not a rhetorical question.)

if($grade == $A)


== is for comparing numbers, eq is for comparing strings.

{$score + 4;}


Q: Where do you store this newly computed value?

A: Nowhere!

Any help on this one is greatly appreciated,


Uh huh.

I know many of you out
there are PERL experts


*none* of us are PERL experts.

Many of us are Perl experts though.

but if you could make the solutions


Sure does sound like you want us to do your homework for you.

We are too ethical for that (and you should be too).

I think that my
logic is sound.


You are not thinking correctly then.
 
J

John Bokma

Tad McClellan said:
[..]
but I am still new to this and just want it to
work.

You will fail in programming with such an attitude.

No, no, the attitude is good. Wanting the program to work is good. Better
then make it appear like it works :-D.
*none* of us are PERL experts.

Many of us are Perl experts though.

And some are perl experts :-D
Sure does sound like you want us to do your homework for you.

We are too ethical for that (and you should be too).

I have made in the past homework in exchange for money and books (Perl
related ;-) ). I included also documentation. I don't think that's
unethical. It's up to the student to reread my code, and learn from it,
and ask smart questions. So far they have done so. Maybe that them paying
for the help stimulated them.

BTW, most of the times the students had already done quite some work, but
just got stuck. In one case it was not even Perl related at all.
 
J

John W. Krahn

I have an assignment for a class that has been driving me crazy for the
past few days. Basically I need to create a simple GPA calculator that
takes in several letter grades (ex: A) and then calculates the GPA
based off that A=4 B=3 C=2 D=1 F=0. Here is what I have, sorry it in
inefficient sloppy code but I am still new to this and just want it to
work.

print "Enter the letter grade (A, B, C, D, F) for each test taken in a
class, enter Z to quit.\n";

****************************************
$score=0;
$A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";
for($classes=0, $grade != $Z, $classes++)
{
$grade;
print "Enter a grade: ";
chomp($grade = <STDIN> );

if($grade == $A)
{$score + 4;}
if($grade == $B)
{$score + 3;}
if($grade == $C)
{$score + 2;}
if($grade == $D)
{$score + 1;}
else {$score + 0;}
}

$gpa=$score/$classes;
print "Your GPA is: $gpa.\n";
*******************************************************
Any help on this one is greatly appreciated, I know many of you out
there are PERL experts but if you could make the solutions very basic
that would be great, remember this is a beginner class :) I know it is
something stupid that I did but I cannot figure it out, I think that my
logic is sound. Thanks all.


my %lookup = (
A => sub { 4 },
B => sub { 3 },
C => sub { 2 },
D => sub { 1 },
Z => sub { print 'Your GPA is: ', $_[ 0 ] / $_[ 1 ], ".\n" and exit }
);

my ( $score, $classes );
{ print 'Enter a grade: ';
chomp( my $grade = uc <STDIN> );

$score += $lookup{ $grade }( $score, $classes++ ) and redo
}



John
 
R

rmr531

Thanks all for the help. Tad, you are mean :) no I'm just kidding, I
am sure that your advice has been taken advantage of before, but
seriously take it easy on us newbies many of us are trying our best, I
am just more of a Java guy, this Perl almost makes it to simple
sometimes, putting strings and int and char all into one variable type
is just chaos on my little mind. But your advice was very helpful, and
I do appreciate it.

A couple of you seemed to think that I was asking you to do the entire
thing for me, I honestly was not, if I had I would not have done as
much as I did. Maybe I just worded the post wrong but I was only
looking for people to point our my mistakes (which there were plenty
of, but I was on the right track, you have to admit), and the only
reason that I just wanted to get it working was because it is due
today. Maybe I should have said that I just want to figure out why it
is not working, because that is what I meant.

Basically all that I needed was the "ne" and the and "eq", not to make
excuses but it is VERY hard to find that kind of stuff in the book that
I have. Also I needed to change all those ifs to elsif. But I got it
working, just need to tweak it to make it look pretty now. And
speaking of ethics I am not going to post the full working program just
because someone might use it to get out of an assignment, plus I am
sure that you all know what it looks like.

Thanks both John's for the help, John K. your program worked well and I
kind of understand it but unfortunately this is a really really
beginner class and even though it seems pretty simple it would have
stood out like a sore thumb to the instructor, plus I could not have
explained exactly how it worked (see I really am ethical).
 
D

DJ Stunks

John said:
print "Enter the letter grade (A, B, C, D, F) for each test taken in a
class, enter Z to quit.\n";

my %lookup = (
A => sub { 4 },
B => sub { 3 },
C => sub { 2 },
D => sub { 1 },
Z => sub { print 'Your GPA is: ', $_[ 0 ] / $_[ 1 ], ".\n" and exit }
);

my ( $score, $classes );
{ print 'Enter a grade: ';
chomp( my $grade = uc <STDIN> );

$score += $lookup{ $grade }( $score, $classes++ ) and redo
}

tight! :D

-jp
 
T

Tad McClellan

Basically all that I needed was the "ne" and the and "eq", not to make
excuses but it is VERY hard to find that kind of stuff in the book that
I have.


You should not be using the book that you have as your primary reference.

(What book _do_ you have?)

You should use the documentation for the software that you are
using for your primary reference.

You need to find info on an "operator", so the place to look first is:

perldoc perlop

Scanning the section headers leads to the "Relational Operators"
section without much fuss or bother.
 
R

rmr531

Thanks, that should help quite a bit, I wish they showed us that kind
of stuff in class.

The book that we have is "Perl by Example" by Ellie Quigley. I
definately would not reccommend it, everyone in the class hates it,
including the instructor, but what can you do, we have to use what the
college wants us too even if they are wrong.
 
T

Tad McClellan

Thanks, that should help quite a bit,


You're welcome.

I wish they showed us that kind
of stuff in class.


Students in my classes get shown that kind of stuff. :)

The book that we have is "Perl by Example" by Ellie Quigley. I
definately would not reccommend it, everyone in the class hates it,


Yeah, that book has a very poor reputation.
 
T

Ted Zlatanov

The book that we have is "Perl by Example" by Ellie Quigley. I
definately would not reccommend it, everyone in the class hates it,
including the instructor, but what can you do, we have to use what
the college wants us too even if they are wrong.

It might be a fun exercise for the class to pick a few chapters and
find errors in them, then send that list of errors to the department
chair and the dean.

You can learn a lot that way, both about Perl and about real-world
software, programmers, and politics :)

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top