C++ GPA_Calc suggestions

Discussion in 'iOS Development' started by ubunix, Jun 17, 2009.

  1. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    Okay so for one of my summer classes I was assign to create a c++ win32 console program that calculates the GPA of the users inputed grades. Is there any suggestion on what I should change or add to this?

    Source <revised - thanks oioioi>
    Code:
    # include <iostream>
    # include <string>
    
    using namespace std;
    
    int main ()
    {
    	float gpa, a;
    	int sum = 0;
    
    	cout<<"//////////////////////////////////////////"<<endl;
    	cout<<"///                                    ///"<<endl;
    	cout<<"///    U.S Grading Standard <GPA>      ///"<<endl;
    	cout<<"///   A+ = 4     A = 4    A- = 3.8     ///"<<endl;
    	cout<<"///   B+ = 3.5   B = 3    B- = 2.8     ///"<<endl;
    	cout<<"///   C+ = 2.5   C = 2    C- = 1.8     ///"<<endl;
    	cout<<"///   D+ = 1.5   D = 1    D- = 0.8     ///"<<endl;
    	cout<<"///              F = 0                 ///"<<endl;
    	cout<<"///                                    ///"<<endl;
    	cout<<"//////////////////////////////////////////\n"<<endl;
    
    	cout <<"Enter total # of classes  : "; 
    	cin>>a;
    	cout <<"------------------------------------------\n\n"; 
    
    	float *c = new float[a];
    	string *b = new string[a];
    
    	for (int i=1; i<=a;i++)
    	{	
    		cout<<"Enter name of class ["<<i<<"] : ";
    		cin>>b[i];
    		cout<<"Enter grade you earned for "<<b[i]<<" : ";
    		cin>>c[i];
    		cout <<"------------------------------------------\n\n"; 
    		gpa = (sum += c[i]) / a;
    
    	}
    	for (int i=1; i<=a;i++)
    	{
    		cout<<b[i]<<"\t\t Grade : "<<c[i]<<"\n"<<endl;
    	}
    		cout<< "Your GPA for the semester is " <<"(" << gpa <<")\n\n\n";
    		system("pause");
    		
    
    		delete[] c;
    		delete[] b;
    
    		return (0);
    }
    
    Screenshot:

    Please Register or Log in to view images



    Feel free to test the complied version in the attachments.

    Attached Files:

  2. oioioi

    oioioi New Member

    Joined:
    Apr 18, 2008
    Messages:
    102
    Likes Received:
    0
    maybe make c and b arrays dynamic(using new[] and delete[]), this will make it look more advance

    Please Register or Log in to view images

  3. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    How would I go about doing that?
  4. oioioi

    oioioi New Member

    Joined:
    Apr 18, 2008
    Messages:
    102
    Likes Received:
    0
    Code:
    # include <iostream>
    # include <string>
    
    using namespace std;
    
    int main ()
    {
    	float gpa, a;
    	int sum = 0;
    
    	cout<<"//////////////////////////////////////////"<<endl;
    	cout<<"///                                    ///"<<endl;
    	cout<<"///    U.S Grading Standard <GPA>      ///"<<endl;
    	cout<<"///   A+ = 4     A = 4    A- = 3.8     ///"<<endl;
    	cout<<"///   B+ = 3.5   B = 3    B- = 2.8     ///"<<endl;
    	cout<<"///   C+ = 2.5   C = 2    C- = 1.8     ///"<<endl;
    	cout<<"///   D+ = 1.5   D = 1    D- = 0.8     ///"<<endl;
    	cout<<"///              F = 0                 ///"<<endl;
    	cout<<"///                                    ///"<<endl;
    	cout<<"//////////////////////////////////////////\n"<<endl;
    
    	cout <<"Enter total # of classes  : "; 
    	cin>>a;
    	cout <<"------------------------------------------\n\n"; 
    
    	//allocate dynamic memory for our arrays
    	float *c = new float[a];
    	string *b = new string[a];
    
    	for (int i=1; i<=a;i++)
    	{	
    		cout<<"Enter name of class ["<<i<<"] : ";
    		cin>>b[i];
    		cout<<"Enter grade you earned for "<<b[i]<<" : ";
    		cin>>c[i];
    		cout <<"------------------------------------------\n\n"; 
    		gpa = (sum += c[i]) / a;
    
    	}
    	for (int i=1; i<=a;i++)
    	{
    		cout<<b[i]<<"\t\t Grade : "<<c[i]<<"\n"<<endl;
    	}
    		cout<< "Your GPA for the semester is " <<"(" << gpa <<")\n\n\n";
    		system("pause");
    		
    		//delete the dynamic memory
    		delete[] c;
    		delete[] b;
    
    		return (0);
    }
    
    more info on dynamic memory with c++ can be found here: http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/
  5. ubunix

    ubunix Member

    Joined:
    Aug 6, 2008
    Messages:
    637
    Likes Received:
    1
    Device:
    iPhone 5 (Black)
    I was wondering how to do that. So now I can have number classes that a float can handle in stead of 100, thanks!
  6. trojanfoe

    trojanfoe New Member

    Joined:
    Jun 18, 2009
    Messages:
    1
    Likes Received:
    0
    A couple of things:

    • It's tedious expecting users to know how many grades they want to enter
    • It's easier for user's to deal with grades, not the 'score' associated with the grade
    • There is no need for data storage as the algorithm for average is simply total / count, so just keep those values

    Try this:

    Code:
    # include <iostream>
    # include <string>
    
    using namespace std;
    
    static struct
    {
            const char *name;
            float grade;
    } grade_map[] =
    {
            { "A+", 4.0 },
            { "A", 4.0 },
            { "A-", 3.8 },
            { "B+", 3.5 },
            { "B", 3.0 },
            { "B-", 2.8 },
            { "C+", 2.5 },
            { "C", 2.0 },
            { "C-", 1.8 },
            { "D+", 1.5 },
            { "D",  1.0 },
            { "D-", 0.8 },
            { "F",  0.0 }
    };
    
    const unsigned num_maps = sizeof(grade_map) / sizeof(grade_map[0]);
    
    int main ()
    {
            float total = 0.0;
            int count = 0;
    
            cout<<"////////////////////////////////////////////"<<endl;
            cout<<"///                                      ///"<<endl;
            cout<<"///   Enter your grades and hit return   ///"<<endl;
            cout<<"///   Supported grades are:              ///"<<endl;
            cout<<"///   A+, A, A-, B+, B, B-, C+, C, C-    ///"<<endl;
            cout<<"///   D+, D, D- and F                    ///"<<endl;
            cout<<"///   Enter anything else to end         ///"<<endl;
            cout<<"///                                      ///"<<endl;
            cout<<"////////  //////////////////////////////////"<<endl;
            cout << endl;
    
            while (true)
            {
                    string entry;
                    unsigned i;
                    cout << "Enter grade for class " << count + 1 << ": ";
                    cin >> entry;
                    for (i = 0; i < num_maps; i++)
                            if (grade_map[i].name == entry)
                                    break;
                    if (i == num_maps)
                            break;
                    total += grade_map[i].grade;
                    count++;
            }
    
            cout << count << " grades entered; GPA is " << total / (float)count << endl;
    
            return 0;
    }
    
    It's uses a look-up table to map grades to their scores so user's have an easier time.

    Enjoy

Share This Page