I just started learning C++ for a computer class at my highschool. I started learning about "if" and "else" statements so i wrote a program around that. It calculates the average GPA for the semester or total (if you want it to). It also factors in AP and Honors points into the GPA and if you choose you can have up to 4 classes. Now we are covering functions and I wanted to know how i would go about placing the different grading scales into a function to make my program smaller and easier to read, because, as you can see it is pretty long.

``````//Grade Calculator
#include <iostream>
#include <string>

using namespace std;

int main(void)
{

//Variables and Constants---------------------------------------------------------------------------------------------------------

char classABansw, classBBansw, classCBansw, classDBansw, classEBansw;
char classAansw, classBansw, classCansw, classDansw, classEansw;
char avgGansw;
string classAname, classBname, classCname, classDname, classEname;
string classAtype, classBtype, classCtype, classDtype, classEtype;
string lol;
double calcNumbA, calcNumbB, calcNumbC, calcNumbD, calcNumbE, calcNumbF;
double classAperc, classBperc, classCperc, classDperc, classEperc;
double classAgpa, classBgpa, classCgpa, classDgpa, classEgpa;
double classAnumb, classBnumb, classCnumb, classDnumb;
double avgG, classNumb, yearNumb, semesterNumb;

//Title Prompt--------------------------------------------------------------------------------------------------------------------

cout << endl;
cout << "\t\t\t" << " " << ".d8888b.   d8888b.  .d888b." << endl;
cout << "\t\t\t" << " " << "88         88   88  88   88" << endl;
cout << "\t\t\t" << " " << "88    888  88888P'  8888888" << endl;
cout << "\t\t\t" << " " << "88    88   88       88   88" << endl;
cout << "\t\t\t" << " " << "`88888P'   d9       d9   Pb" << endl;
cout << endl;
cout << "\t" << "    " << "....................  Calculator  ....................." << endl;
cout << endl;
cout << " " << "*Note this factors in AP(1) and Honors(.5) points not all colleges honor this*" << endl;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << endl;
system("pause");
cout << endl;

//Operator Input------------------------------------------------------------------------------------------------------------------

cout << "  " << "\\\\" << "General Information" << endl;
cout << "    " << "//" << "What year is this at your school?" << " (1, 2, 3,or 4) " << ": " << endl;
cout << "\t" << "  ";
cin >> yearNumb;
if (yearNumb > 4)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want to enter this value to be submitted?" << ": " << yearNumb << endl;
cout << endl;
cout << "**REMEMBER 1, 2, 3, or 4 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> yearNumb;
}
if (yearNumb <= 0)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want this value to be submitted?" << ": " << yearNumb << endl;
cout << endl;
cout << "**REMEMBER 1, 2, 3, or 4 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> yearNumb;
}
cout << "    " << "//" << "Are you on a first or second semester?" << " (1 or 2) " << ": " << endl;
cout << "\t  ";
cin >> semesterNumb;
if (semesterNumb > 2)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want this value to be submitted?" << ": " << semesterNumb << endl;
cout << endl;
cout << "**REMEMBER 1 or 2 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> semesterNumb;
}
if (semesterNumb <= 0)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want this value to be submitted?" << ": " << semesterNumb << endl;
cout << endl;
cout << "**REMEMBER 1 or 2 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> semesterNumb;
}
cout << endl;
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "GPA Information" << endl;
cout << "    " << "//" << "Do you know your GPA prior to this semester?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> avgGansw;
if (avgGansw == 'y')
{
cout << "    " << "//" << "Enter prior GPA: " << endl;
cout << "\t" << "  ";
cin >> avgG;
cout << endl;
}
else if (avgGansw == 'Y')
{
cout << "    " << "//" << "Enter prior GPA: " << endl;
cout << "\t" << "  ";
cin >> avgG;
cout << endl;
}
else
{
cout << endl;
}
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "Number of Classes" << endl;
cout << "    " << "//" << "Enter the Number of classes you are currently in" << " (max: 4) " << ": " << endl;
cout << "\t" << "  ";
cin >> classNumb;
if (classNumb > 4)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want this value to be submitted?" << ": " << classNumb << endl;
cout << endl;
cout << "**REMEMBER 1, 2, 3, or 4 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> classNumb;
}
if (classNumb < 0)
{
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want this value to be submitted?" << ": " << classNumb << endl;
cout << endl;
cout << "**REMEMBER 1, 2, 3, or 4 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> classNumb;
}
if (classNumb == 0)
{
//Spacing-----------------------------------------------------------------------------------------------------------------------
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
//Message Prompt----------------------------------------------------------------------------------------------------------------
cout << "                 " << "Why are running this program if you don't have" << endl;
cout << "             " << "any classes?" << " *NO SPACES* " << "[example:I_have_too_much_time]" << ": " << endl;
//Spacing-----------------------------------------------------------------------------------------------------------------------
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
//Operater Imput/Client Output---------------------------------------------------------------------------------------------------
cout << "             ";
cin >> lol;
cout << endl;
cout << endl;
cout << lol << " is not a valid excuse for you wasting your time.";
cout << endl;
cout << endl;
}
else
{
cout << endl;
//Operator Input-First Class Information-----------------------------------------------------------------------------------------------
if (classNumb >= 1)
{
classAnumb = 1;
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "First Class Information" << endl;
cout << "    " << "//" << "Enter your first class name" << " *NO SPACES* " << "[Example:Team_Sports]" << ": " << endl;
cout << "\t" << "  ";
cin >> classAname;
cout << "    " << "//" << "Current percent in that class " << "[Example:80]" << ": " << endl;
cout << "\t" << "  ";
cin >> classAperc;
cout << "    " << "//" << "Is this class AP?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classAansw;
if (classAansw == 'y')
{
if (classAperc >= 90)
classAgpa = (5);
else if (classAperc >= 80)
classAgpa = (4);
else if (classAperc >= 74)
classAgpa = (3);
else if (classAperc >= 70)
classAgpa = (2);
else if (classAperc <= 69)
classAgpa = 0;
}
else if (classAansw == 'Y')
{
if (classAperc >= 90)
classAgpa = (5);
else if (classAperc >= 80)
classAgpa = (4);
else if (classAperc >= 74)
classAgpa = (3);
else if (classAperc >= 70)
classAgpa = (2);
else if (classAperc <= 69)
classAgpa = 0;
}
else
{
cout << "    " << "//" << "Is this class Honors?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classABansw;
if (classABansw == 'y')
{
if (classAperc >= 90)
classAgpa = (4.5);
else if (classAperc >= 80)
classAgpa = (3.5);
else if (classAperc >= 74)
classAgpa = (2.5);
else if (classAperc >= 70)
classAgpa = (1.5);
else if (classAperc <= 69)
classAgpa = 0;
}
else if (classABansw == 'Y')
{
if (classAperc >= 90)
classAgpa = (4.5);
else if (classAperc >= 80)
classAgpa = (3.5);
else if (classAperc >= 74)
classAgpa = (2.5);
else if (classAperc >= 70)
classAgpa = (1.5);
else if (classAperc <= 69)
classAgpa = 0;
}
else
{
if (classAperc >= 90)
classAgpa = (4);
else if (classAperc >= 80)
classAgpa = (3);
else if (classAperc >= 74)
classAgpa = (2);
else if (classAperc >= 70)
classAgpa = (1);
else if (classAperc <= 69)
classAgpa = 0;
}
}
}
else
{
classAperc = 0;
classAname = '/';
classAgpa = 0;
classAnumb = 0;
}
//Operator Input-Second Class Information---------------------------------------------------------------------------------------------------
if (classNumb >= 2)
{
classBnumb = 1;
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "Second Class Information" << endl;
cout << "    " << "//" << "Enter your second class name" << " *NO SPACES* " << "[Example:Team_Sports]" << ": " << endl;
cout << "\t" << "  ";
cin >> classBname;
cout << "    " << "//" << "Current percent in that class " << "[Example:80]" << ": " << endl;
cout << "\t" << "  ";
cin >> classBperc;
cout << "    " << "//" << "Is this class AP?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classBansw;
if (classBansw == 'y')
{
if (classBperc >= 90)
classBgpa = (5);
else if (classBperc >= 80)
classBgpa = (4);
else if (classBperc >= 74)
classBgpa = (3);
else if (classBperc >= 70)
classBgpa = (2);
else if (classBperc <= 69)
classBgpa = 0;
}
else if (classBansw == 'Y')
{
if (classBperc >= 90)
classBgpa = (5);
else if (classBperc >= 80)
classBgpa = (4);
else if (classBperc >= 74)
classBgpa = (3);
else if (classBperc >= 70)
classBgpa = (2);
else if (classBperc <= 69)
classBgpa = 0;
}
else
{
cout << "    " << "//" << "Is this class Honors?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classBBansw;
if (classBBansw == 'y')
{
if (classBperc >= 90)
classBgpa = (4.5);
else if (classBperc >= 80)
classBgpa = (3.5);
else if (classBperc >= 74)
classBgpa = (2.5);
else if (classBperc >= 70)
classBgpa = (1.5);
else if (classBperc <= 69)
classBgpa = 0;
}
else if (classBBansw == 'Y')
{
if (classBperc >= 90)
classBgpa = (4.5);
else if (classBperc >= 80)
classBgpa = (3.5);
else if (classBperc >= 74)
classBgpa = (2.5);
else if (classBperc >= 70)
classBgpa = (1.5);
else if (classBperc <= 69)
classBgpa = 0;
}
else
{
if (classBperc >= 90)
classBgpa = (4);
else if (classBperc >= 80)
classBgpa = (3);
else if (classBperc >= 74)
classBgpa = (2);
else if (classBperc >= 70)
classBgpa = (1);
else if (classBperc <= 69)
classBgpa = 0;
}
}
}
else
{
classBperc = 0;
classBname = '/';
classBgpa = 0;
classBnumb = 0;
}
//Operator Input-Third Class Information---------------------------------------------------------------------------------------
if (classNumb >= 3)
{
classCnumb = 1;
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "Third Class Information" << endl;
cout << "    " << "//" << "Enter your third class name" << " *NO SPACES* " << "[Example:Team_Sports]" << ": " << endl;
cout << "\t" << "  ";
cin >> classCname;
cout << "    " << "//" << "Current percent in that class " << "[Example:80]" << ": " << endl;
cout << "\t" << "  ";
cin >> classCperc;
cout << "    " << "//" << "Is this class AP?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classCansw;
if (classCansw == 'y')
{
if (classCperc >= 90)
classCgpa = (5);
else if (classCperc >= 80)
classCgpa = (4);
else if (classCperc >= 74)
classCgpa = (3);
else if (classCperc >= 70)
classCgpa = (2);
else if (classCperc <= 69)
classCgpa = 0;
}
else if (classCansw == 'Y')
{
if (classCperc >= 90)
classCgpa = (5);
else if (classCperc >= 80)
classCgpa = (4);
else if (classCperc >= 74)
classCgpa = (3);
else if (classCperc >= 70)
classCgpa = (2);
else if (classCperc <= 69)
classCgpa = 0;
}
else
{
cout << "    " << "//" << "Is this class Honors?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classCBansw;
if (classCBansw == 'y')
{
if (classCperc >= 90)
classCgpa = (4.5);
else if (classCperc >= 80)
classCgpa = (3.5);
else if (classCperc >= 74)
classCgpa = (2.5);
else if (classCperc >= 70)
classCgpa = (1.5);
else if (classCperc <= 69)
classCgpa = 0;
}
else if (classCBansw == 'Y')
{
if (classCperc >= 90)
classCgpa = (4.5);
else if (classCperc >= 80)
classCgpa = (3.5);
else if (classCperc >= 74)
classCgpa = (2.5);
else if (classCperc >= 70)
classCgpa = (1.5);
else if (classCperc <= 69)
classCgpa = 0;
}
else
{
if (classCperc >= 90)
classCgpa = (4);
else if (classCperc >= 80)
classCgpa = (3);
else if (classCperc >= 74)
classCgpa = (2);
else if (classCperc >= 70)
classCgpa = (1);
else if (classCperc <= 69)
classCgpa = 0;
}
}
}
else
{
classCperc = 0;
classCname = '/';
classCgpa = 0;
classCnumb = 0;
}
//Operator Input-Forth Class Information-------------------------------------------------------------------------------------
if (classNumb >= 4)
{
classDnumb = 1;
cout << "________________________________________________________________________________" << endl;
cout << "  " << "\\\\" << "Forth Class Information" << endl;
cout << "    " << "//" << "Enter your forth class name" << " *NO SPACES* " << "[Example:Team_Sports]" << ": " << endl;
cout << "\t" << "  ";
cin >> classDname;
cout << "    " << "//" << "Current percent in that class " << "[Example:80]" << ": " << endl;
cout << "\t" << "  ";
cin >> classDperc;
cout << "    " << "//" << "Is this class AP?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classDansw;
if (classDansw == 'y')
{
if (classDperc >= 90)
classDgpa = (5);
else if (classDperc >= 80)
classDgpa = (4);
else if (classDperc >= 74)
classDgpa = (3);
else if (classDperc >= 70)
classDgpa = (2);
else if (classDperc <= 69)
classDgpa = 0;
}
else if (classDansw == 'Y')
{
if (classDperc >= 90)
classDgpa = (5);
else if (classDperc >= 80)
classDgpa = (4);
else if (classDperc >= 74)
classDgpa = (3);
else if (classDperc >= 70)
classDgpa = (2);
else if (classDperc <= 69)
classDgpa = 0;
}
else
{
cout << "    " << "//" << "Is this class Honors?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classDBansw;
if (classDBansw == 'y')
{
if (classDperc >= 90)
classDgpa = (4.5);
else if (classDperc >= 80)
classDgpa = (3.5);
else if (classDperc >= 74)
classDgpa = (2.5);
else if (classDperc >= 70)
classDgpa = (1.5);
else if (classDperc <= 69)
classDgpa = 0;
}
else if (classDBansw == 'Y')
{
if (classDperc >= 90)
classDgpa = (4.5);
else if (classDperc >= 80)
classDgpa = (3.5);
else if (classDperc >= 74)
classDgpa = (2.5);
else if (classDperc >= 70)
classDgpa = (1.5);
else if (classDperc <= 69)
classDgpa = 0;
}
else
{
if (classDperc >= 90)
classDgpa = (4);
else if (classDperc >= 80)
classDgpa = (3);
else if (classDperc >= 74)
classDgpa = (2);
else if (classDperc >= 70)
classDgpa = (1);
else if (classDperc <= 69)
classDgpa = 0;
}
}
}
else
{
classDperc = 0;
classDname = '/';
classDgpa = 0;
classDnumb = 0;
}

//Output Constants and Variables----------------------------------------------------------------------------------

calcNumbA = ((yearNumb - 1) * 2) + (semesterNumb - 1);
calcNumbB = ((yearNumb - 1) * 2) + (semesterNumb);
calcNumbC = (classAnumb + classBnumb + classCnumb + classDnumb);
calcNumbD = (classAgpa + classBgpa + classCgpa + classDgpa);
calcNumbE = ((calcNumbD /calcNumbC) + (avgG * calcNumbA)) /calcNumbB;
calcNumbF = (calcNumbD /calcNumbC);

//Client Output---------------------------------------------------------------------------------------------------

//Spacing-------------------------------------------------------------------------------------------------
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << "\t\t\t" << "     " << "/ = Not Applicable" << endl;
cout << endl;
cout.setf(ios::left);
cout.width(2);
cout << "__Class____";
cout.setf(ios::right);
cout.width(15);
cout << "__GPA____";
cout.width(25);
cout << "__Percent____";
cout.width(12);
cout << "\t__Name__________________" << endl;
cout << endl;
//Output Row 1-----------------------------------------------------------------------------------------
cout.precision(3);
cout.setf(ios::left);
cout.width(2);
cout << " " << "1st Period";
cout.setf(ios::right);
cout.width(12);
cout << classAgpa;
cout.width(23);
cout << classAperc;
cout.width(33);
cout << classAname << endl;
//Output Row 2-----------------------------------------------------------------------------------------
cout.setf(ios::left);
cout.width(2);
cout << " " << "2nd Period";
cout.setf(ios::right);
cout.width(12);
cout << classBgpa;
cout.width(23);
cout << classBperc;
cout.width(33);
cout << classBname << endl;
//Output Row 3-----------------------------------------------------------------------------------------
cout.setf(ios::left);
cout.width(2);
cout << " " << "3rd Period";
cout.setf(ios::right);
cout.width(12);
cout << classCgpa;
cout.width(23);
cout << classCperc;
cout.width(33);
cout << classCname << endl;
//Output Row 4-----------------------------------------------------------------------------------------
cout.setf(ios::left);
cout.width(2);
cout << " " << "4th Period";
cout.setf(ios::right);
cout.width(12);
cout << classDgpa;
cout.width(23);
cout << classDperc;
cout.width(33);
cout << classDname << endl;
//Divider-----------------------------------------------------------------------------------------------
cout << "________________________________________________________________________________" << endl;
//Output Totals-----------------------------------------------------------------------------------------
cout << ">" << "Semester GPA" << endl;
cout << calcNumbF << endl;
if (avgGansw == 'n')
{
cout << endl;
}
else
{

cout << ">" << "Total GPA" << endl;

cout << calcNumbE << endl;
}
}

//End---------------------------------------------------------------------------------------------------

cout << endl;
cout << ">" << "Process Complete" << endl;
system("pause");
return (0);
}``````

You didn't tell us or give an example of what you know (do you know how to implement functions in any way?) so I'll give you a little example of how you can factor your code out.

Place this above main():

``````double checkInput(double input)
{
double newInput;
cout << endl;
cout << "    " << "//" << "Your input determines the accuracy of this program." << endl;
cout << "      " << "//" << "Are you sure you want to enter this value to be submitted?" << ": " << input << endl;
cout << endl;
cout << "**REMEMBER 1, 2, 3, or 4 are recomended**" << endl;
cout << endl;
cout << "\t" << "Re-Enter value" << ": ";
cin >> newInput;
return newInput;
}``````

Now anywhere you have this pattern in your code, replace it like this:

``````if (yearNumb > 4)
{
yearNumb = checkInput(yearNumb);
}``````

You might consider this change however:

``````if (yearNumb > 4 ||  yearNumb <= 0)
{
yearNumb = checkInput(yearNumb);
}``````

If you've learned loops (for, do), you should probably put this in one until the input is as desired.

692 lines - "Holy long code, Batman!"

First, stop with the multiple lines of `cout << endl` for spacing. If you really need the vertical space, output the endlines in a loop.

Second, you have duplication of code blocks to handle responses in the upper case and lower case. Combine these with `if (avgGansw == 'Y' || avgGansw == 'y')` You could make a function from the chunks that handle each classNumb - use just one function repeatedly for as many classes as the student takes, returning and accumulating the GPA.

Really, the basic action you need to take is look at the code - anything that is repeated (with just a change in the variable name used) is candidate to be written as a function.

One of the concepts I try to get my students to understand is to "be a lazy programmer" - only write things once.

Oh, and work on the indenting. It may be that the forum is interacting with your code file badly, but in some parts you're not following conventional style. Here's a snipped from your code, better indented:

``````//the forum is still wrapping some lines that would be ok in normal editing - less than 80 char's
else
{
cout << endl;
//Operator Input-First Class Information-----------------------------------------------------------------------
if (classNumb >= 1)
{
classAnumb = 1;
cout << "____________________________________________________" << endl;
cout << "  " << "\\\\" << "First Class Information" << endl;
cout << "    " << "//" << "Enter your first class name" << " *NO SPACES* "
<< "[Example:Team_Sports]" << ": " << endl;
cout << "\t" << "  ";
cin >> classAname;
cout << "    " << "//" << "Current percent in that class " << "[Example:80]"
<< ": " << endl;
cout << "\t" << "  ";
cin >> classAperc;
cout << "    " << "//" << "Is this class AP?" << " (y or n) " << ": " << endl;
cout << "\t" << "  ";
cin >> classAansw;
if (classAansw == 'y')
{
if (classAperc >= 90)
classAgpa = (5);
else if (classAperc >= 80)
classAgpa = (4);
else if (classAperc >= 74)
classAgpa = (3);
else if (classAperc >= 70)
classAgpa = (2);
else if (classAperc <= 69)
classAgpa = 0;
}``````

Thanks for all the support. I will post my progress as i edit my program.

PS: My indentions were so large that the "code" function did have to shorten them to be viewed. Sorry for the confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.