Velocity Reviews > Perl > Simple GPA calculator program

# Simple GPA calculator program

rmr531@gmail.com
Guest
Posts: n/a

 06-07-2006
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";
{

{\$score + 4;}
{\$score + 3;}
{\$score + 2;}
{\$score + 1;}
else {\$score + 0;}
}

\$gpa=\$score/\$classes;
************************************************** *****
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.

John Bokma
Guest
Posts: n/a

 06-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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.

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/

Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> 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++)

^^^^^^^^^^^^

!= is for comparing numbers, ne is for comparing strings.

> {

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.)

== 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.

--
(E-Mail Removed) Perl programming
Fort Worth, Texas

John Bokma
Guest
Posts: n/a

 06-07-2006
John Bokma <(E-Mail Removed)> wrote:

> (E-Mail Removed) wrote:
>
>> I have an assignment for a class

>
> I would probably give you a D for trying.
>
> (With your program an F is not possible).

Ok, that was stupid

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

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/

John Bokma
Guest
Posts: n/a

 06-07-2006

> (E-Mail Removed) <(E-Mail Removed)> wrote:

[..]

>> 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 .

>> I know many of you out
>> there are PERL experts

>
>
> *none* of us are PERL experts.
>
> Many of us are Perl experts though.

And some are perl experts

> 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.

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/

John W. Krahn
Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) wrote:
> 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++)
> {
> print "Enter a grade: ";
>
> {\$score + 4;}
> {\$score + 3;}
> {\$score + 2;}
> {\$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
--
use Perl;
program
fulfillment

rmr531@gmail.com
Guest
Posts: n/a

 06-07-2006
Thanks all for the help. Tad, you are mean no I'm just kidding, I
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
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).

DJ Stunks
Guest
Posts: n/a

 06-07-2006

John W. Krahn wrote:
> (E-Mail Removed) wrote:
> > 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!

-jp

Guest
Posts: n/a

 06-07-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> 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

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

perldoc perlop

section without much fuss or bother.

--
(E-Mail Removed) Perl programming
Fort Worth, Texas

rmr531@gmail.com
Guest
Posts: n/a

 06-07-2006
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.