Gpa Calc

Pages: 12
oh i'm such a twat! I guess thats what you get when you're a novice programming at night lol.
@ gmghulamabbas:

When people offer you advice on your code, take time to listen because there completely right,
and your code is completely wrong. Helios completly solved your code for you, i guess you need your code spoon fed to you.

this can still be way simplified,
i only had a few mins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <limits>
using namespace std;

double solve(string);
void pause()
  {
  cout << "Press ENTER to continue...";
  cin.sync();
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
  }
int main()
{
string grade1, grade2, grade3, grade4, grade5;
double total;
 cout<<"Enter grade of Calculus1 :";
 cin>>grade1;
 cout<<"Enter grade of Intro to Information Technology :";
 cin>>grade2;
 cout<<"Enter grade of Physics :";
 cin>>grade3;
 cout<<"Enter grade of Communication Skills :";
 cin>>grade4;
 cout<<"Enter grade of Religious Studies :";
 cin>>grade5;
cout<<"Grade in Calculus1 :"<<solve(grade1)<<endl
	<<"Grade in Intro to Information Technology :"<<solve(grade2)<<endl
	<<"Grade in Physics :"<<solve(grade3)<<endl
	<<"Grade  in Communication skills :"<<solve(grade4)<<endl
	<<"Grade in Religious Studies :"<<solve(grade5)<<endl;
 total = (
	 solve(grade1)+
	 solve(grade2)+
	 solve(grade3)+
	 solve(grade4)+
	 solve(grade5))/5;
 cout<<"FINAL GPA : "<<total<<endl;
 pause();
return 0;
}
double solve(string grade)
{
double gpa;
if(grade=="A"||grade=="a")
{gpa=4;}
else if(grade=="Am"||grade=="am")
{ gpa=3.67;}
else if(grade=="Bp"||grade=="bp")
{gpa=3.33;}
else if(grade=="B"||grade=="b")
{ gpa=3.00;}
else if(grade=="Bm"||grade=="bm")
{gpa=2.67;}
else if(grade=="Cp"||grade=="cp")
{gpa=2.33;}
else if(grade=="C"||grade=="c")
{ gpa=2.00;}
else if(grade=="Cm"||grade=="cm")
{ gpa=1.67;}
else if(grade=="Dp"||grade=="dp")
{gpa=1.33;}
else if(grade=="D"||grade=="d")
{gpa=1.00;}
else if(grade=="F"||grade=="f")
{gpa=0;}
else if(grade=="I"||grade=="i")
{ gpa=0;}
return gpa;
}


as you can see i knocked out a 100 lines of code,
see the difference
Last edited on
closed account (S6k9GNh0)
That could have been thirty times cleaner with a tolower / toupper function and a switch statement. Also, using the tolower function would be EVEN easier if you used - / + instead of m / p.

@ (whoever said my code was bugged): The error occurs where I declared the grades array. Works in most compilers. And if not, you can create a vector or just give it a constant definition.
Last edited on
Every once in a while I get the urge to implement someone's homework in a "good" way, and this was one of them.

My implementation is the same length as the above, but mine also allows + and - in addition to p and m, verifies input and asks again if the input is invalid, and handles an arbitrary number of classes. And all without a single if() statement (just one do-while).

I don't feel too bad posting it here because it used so many advanced concepts there is no way OP could turn it in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iomanip>
#include <iostream>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <string.h>

struct GradeToPoints {
    const char* grade;
    double      points;
};

template< typename T, size_t N >
T* abegin( T (&a)[ N ] )
    { return a; }

template< typename T, size_t N >
T* aend( T (&a)[ N ] )
    { return a + N; }

template< typename T, size_t N >
size_t asize( T (&)[ N ] )
    { return N; }

double CalcGradePoints( const std::string& grade ) {
    GradeToPoints grades[] = {
        { "a", 4.00 }, { "am", 3.67 }, { "a-", 3.67 },
        { "b+", 3.33 }, { "bp", 3.33 }, { "b", 3.00 },
        { "b-", 2.67 }, { "bm", 2.67 }, { "cp", 2.33 },
        { "c+", 2.33 }, { "c", 2.00 }, { "c-", 1.67 },
        { "cm", 1.67 }, { "dp", 1.33 }, { "d+", 1.33 },
        { "d", 1.00 }, { "f", 0.00 }, { "i", 0.00 }
    };

    GradeToPoints* score = std::find_if(
        abegin( grades ), aend( grades ), boost::lambda::bind(
        strcasecmp, &boost::lambda::_1->*&GradeToPoints::grade, 
        grade.c_str() ) == 0 );

    return score == aend( grades ) ? -1.0 : score->points;
}

double GetGradePoints( const char* class_name ) {
    double grade_points;
    do {
        std::string grade;
        std::cout << "Enter grade for " << class_name << ": " << std::flush;
        getline( std::cin, grade );
        grade_points = CalcGradePoints( grade );
    } while( grade_points < 0 );
 
    return grade_points;
}

int main() {
    const char* classes[] = {
        "Calculus", "Intro to Information Technology", "Physics",
        "Communication Skills", "Religious Studies"
    };

    double grade_points = 0.0;
    std::for_each( abegin( classes ), aend( classes ), 
        boost::lambda::var( grade_points ) += boost::lambda::bind( &GetGradePoints, 
        boost::lambda::_1 ) );

    std::cout << "Final GPA = " << std::fixed << std::setprecision( 2 )
        << ( grade_points / asize( classes ) ) << std::endl; 
}

Last edited on
Oh, God, I can see forever!
i know, i only had a few mins and his code was bugging me so i tried to keep it as it was, but use a function. But for the switch , i had strings so i had to use if statments. jsmiths code is pretty clean version though
thanks all of you my dear

you people tried to solve my problem and now its solved

thanks again
wish u good luck
Take care
its kinda akward that you say dear in every post, just sayin
Topic archived. No new replies allowed.
Pages: 12