Simple average grade calculator I'd like this code to be minimized, while displaying the exact same stuff. 

Specifically, on how to remove some of the repetitions made within the code (such as "if, else if") etc. using other statements or making the compiler go through it faster, as I feel like I lack the experience and the knowledge to do this without messing up everything (such as using switch &amp; case).

For example, when I test for "tf", I'd like not to be forced to repeat the "average" depending on whether or not we add the "TEZA" grade.



#include &lt;iostream&gt;
using namespace std;
int main()

{
    string sub, tf;
    int m1, m2, m3, m4, sum, TEZA;
    double avg, tzm;

    cout &lt;&lt; "SIMPLE AVERAGE CALCULATOR";
    cout &lt;&lt; "\n" &lt;&lt; "\n" &lt;&lt; "Subject at hand?: ";
    cin &gt;&gt; sub;
    cout &lt;&lt; "\n" &lt;&lt; "Input the FOUR marks you'd like verified: " &lt;&lt; "\n";
    cout &lt;&lt; "\n" &lt;&lt; "M1: ";
    cin &gt;&gt; m1;
    cout &lt;&lt; "\n" &lt;&lt; "M2: ";
    cin &gt;&gt; m2;
    cout &lt;&lt; "\n" &lt;&lt; "M3: ";
    cin &gt;&gt; m3;
    cout &lt;&lt; "\n" &lt;&lt; "M4: ";
    cin &gt;&gt; m4;
    cout &lt;&lt; "\n" &lt;&lt; "Would you like to include the TEZA grade?(Y/N): ";
    cin &gt;&gt; tf;

    sum = m1 + m2 + m3 + m4;
    avg = (double) sum / 4;

    if (tf == "Y" | tf == "y")
    {
        cout &lt;&lt; "What is the TEZA grade?: ";
        cin &gt;&gt; TEZA;
        int tzm = ((double) avg * 3 + TEZA) / 4;
        cout &lt;&lt; "\n" &lt;&lt; "Your average grade at " &lt;&lt; sub &lt;&lt; " is " &lt;&lt; tzm &lt;&lt; "\n"
             &lt;&lt; "\n";

        cout &lt;&lt; "You got the following mark: ";
        if (tzm &gt;= 9 &amp;&amp; tzm &lt;= 10)
            cout &lt;&lt; "A" &lt;&lt; "\n";
        else if (tzm &gt;= 8 &amp;&amp; tzm &lt;= 9)
            cout &lt;&lt; "B" &lt;&lt; "\n";
        else if (tzm &gt;= 7 &amp;&amp; tzm &lt;= 8)
            cout &lt;&lt; "C" &lt;&lt; "\n";
        else if (tzm &gt;= 6 &amp;&amp; tzm &lt;= 7)
            cout &lt;&lt; "D" &lt;&lt; "\n";
        else if (tzm &gt;= 5 &amp;&amp; tzm &lt;= 6)
            cout &lt;&lt; "E" &lt;&lt; "\n";
        else if (tzm &lt; 5)
            cout &lt;&lt; "F" &lt;&lt; "\n";

        if (tzm &gt;= 5)
        {
            cout &lt;&lt; "DO YOU PASS: " &lt;&lt; "\n";
            cout &lt;&lt; "Yes." &lt;&lt; "\n";
        }
        else
            cout &lt;&lt; "No." &lt;&lt; "\n";
    }

    else

    {
        cout &lt;&lt; "\n" &lt;&lt; "Average at " &lt;&lt; sub &lt;&lt; " is " &lt;&lt; avg &lt;&lt; "\n" &lt;&lt; "\n";

        cout &lt;&lt; "You got the following mark: ";
        if (avg &gt;= 9 &amp;&amp; avg &lt;= 10)
            cout &lt;&lt; "A" &lt;&lt; "\n";
        else if (avg &gt;= 8 &amp;&amp; avg &lt;= 9)
            cout &lt;&lt; "B" &lt;&lt; "\n";
        else if (avg &gt;= 7 &amp;&amp; avg &lt;= 8)
            cout &lt;&lt; "C" &lt;&lt; "\n";
        else if (avg &gt;= 6 &amp;&amp; avg &lt;= 7)
            cout &lt;&lt; "D" &lt;&lt; "\n";
        else if (avg &gt;= 5 &amp;&amp; avg &lt;= 6)
            cout &lt;&lt; "E" &lt;&lt; "\n";
        else if (avg &lt; 5)
            cout &lt;&lt; "F" &lt;&lt; "\n";

        cout &lt;&lt; "\n" &lt;&lt; "DO YOU PASS?: " &lt;&lt; "\n";

        if (avg &gt;= 5)
            cout &lt;&lt; "Yes." &lt;&lt; "\n";
        else
            cout &lt;&lt; "No." &lt;&lt; "\n";

    }
}
Bug

This calculation looks suspiciously buggy:


int tzm = ((double) avg * 3 + TEZA) / 4;



Why are you taking avg, which is already a double, and casting it to a double?  Furthermore, why are you taking the right-hand side, which is a double, and coercing it into an int variable?  Note that you had declared double tzm earlier in the code; this int tzm declaration shadows the earlier declaration.  Your compiler should have warned you that the tzm on line 8 is unused.  (You do compile with warnings enabled, right?  It's a good habit.)

In any case, you should write a comment explaining why the tzm formula is the way it is.  I don't understand the calculation.

By the way, you can get floating-point division without explicitly casting, simply by writing / 4.0.

Using the bitwise | operator in tf == "Y" | tf == "y" is not really appropriate; it should be a logical || operator.

Optimization

There is absolutely no point in optimizing this code for performance.  Nearly all of the time is going to be spent waiting for input; the calculations are trivial.

What you should do, though, is to optimize it for elegance and readability.

The key is to identify functionality within main() that can be extracted out into functions, so that main() no longer has any tedious details.  For example, a common pattern is printing a question, and expecting a response from the user.  I'd define three functions, to ask the user for string, integer, and yes/no responses.

I would also define a letterGrade() function so that all of the results can be reported using just one statement.

Avoid cluttering your code with variables that don't mean much.  I'd get rid of sum and tf, for example.  The tzm variable is simply a recalculated average of sorts, so I'd get rid of it too.

Suggested solution

#include &lt;iostream&gt;
#include &lt;string&gt;

std::string ask(const std::string&amp; prompt) {
    std::string answer;
    std::cout &lt;&lt; prompt;
    std::cin &gt;&gt; answer;
    return answer;
}

int askInt(const std::string&amp; prompt) {
    int answer;
    std::cout &lt;&lt; prompt;
    std::cin &gt;&gt; answer;
    return answer;
}

bool askYN(const std::string&amp; prompt) {
    std::string answer = ask(prompt);
    return answer == "Y" || answer == "y";
}

std::string letterGrade(double grade) {
    return (grade &gt; 10) ? "" :
           (grade &gt;= 9) ? "A" :
           (grade &gt;= 8) ? "B" :
           (grade &gt;= 7) ? "C" :
           (grade &gt;= 6) ? "D" :
           (grade &gt;= 5) ? "E" : "F";
}

int main() {
    std::cout &lt;&lt; "SIMPLE AVERAGE CALCULATOR\n\n";
    std::string subject = ask("Subject at hand?: ");

    std::cout &lt;&lt; "\nInput the FOUR marks you'd like verified:\n";
    int m1 = askInt("\nM1: "),
        m2 = askInt("\nM2: "),
        m3 = askInt("\nM3: "),
        m4 = askInt("\nM4: ");
    double avg = (m1 + m2 + m3 + m4) / 4.0;

    if (askYN("\nWould you like to include the TEZA grade?(Y/N): ")) {
        int teza = askInt("What is the TEZA grade?: ");
        avg = (3 * avg + teza) / 4.0;
    }

    std::cout &lt;&lt; "\nYour average grade at " &lt;&lt; subject &lt;&lt; " is " &lt;&lt; avg &lt;&lt; "\n\n"
                 "You got the following mark:" &lt;&lt; letterGrade(avg) &lt;&lt; "\n"
                 "DO YOU PASS: " &lt;&lt; (avg &gt;= 5 ? "Yes." : "No.") &lt;&lt; "\n";
}