Arithmetic Progression I had to write a program that would complete an arithmetic progression based on the users input. The user would provide the third term in the series, the third from the last term, and the sum of all the terms in the series. The program would display the total number of terms are in the series and then the series itself. I already submitted it but I am hoping to get some general feedback.

Example: 3, 8, 55

Program output:

10

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

#include &lt;iostream&gt;
#include &lt;vector&gt;

int main() {
    int thirdTerm;
    int thirdLastterm;
    int sum;
    int total;
    int constantNumber = 1;
    int a = 0;
    std::vector&lt;int&gt; vSeries;

    while (std::cout &lt;&lt; "Please enter the third term in the series: " &amp;&amp; !(std::cin &gt;&gt; thirdTerm)) {
        std::cin.clear(); //clear bad input flag
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); //discard input
        std::cout &lt;&lt; "Invalid input; please re-enter.\n";
    }
    while (std::cout &lt;&lt; "Enter the third from the last term in the series: " &amp;&amp; !(std::cin &gt;&gt; thirdLastterm)) {
        std::cin.clear(); //clear bad input flag
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); //discard input
        std::cout &lt;&lt; "Invalid input; please re-enter.\n";
    }
    while (std::cout &lt;&lt; "Please enter the sum of the series: " &amp;&amp; !(std::cin &gt;&gt; sum)) {
        std::cin.clear(); //clear bad input flag
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); //discard input
        std::cout &lt;&lt; "Invalid input; please re-enter.\n";
    }

    do {
        a = 0;
        vSeries.clear();
        vSeries.push_back(thirdTerm - (2 * constantNumber));
        vSeries.push_back(thirdTerm - (1 * constantNumber));

        do {
            total = 0;
            vSeries.push_back(thirdTerm + (a * constantNumber));
            a++;
            for (int i = 0; i &lt; vSeries.size(); i++) {
                total += vSeries[i];
            }
        } while (total &lt; sum);

        constantNumber++;

        if (constantNumber &gt; 100000) {
            std::cout &lt;&lt; "There doesn't appear to be a solution based on the input - exiting program.";
                return 0;
        }

    } while (vSeries[vSeries.size() - 3] != thirdLastterm || total != sum);

    std::cout &lt;&lt; "\nNumber of terms in the series: " &lt;&lt;  vSeries.size() &lt;&lt; std::endl;
    for (int i = 0; i &lt; vSeries.size(); i++) {
        if (i == 0)
            std::cout &lt;&lt; vSeries[i];
        else
            std::cout &lt;&lt; "," &lt;&lt; vSeries[i];
    }

    return 0;
}
A couple of things I noticed:

Here:

while (std::cout &lt;&lt; "Please enter the third term in the series: " &amp;&amp; !(std::cin &gt;&gt; thirdTerm)) {
    std::cin.clear(); //clear bad input flag
    std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); //discard input
    std::cout &lt;&lt; "Invalid input; please re-enter.\n";
}


Aside from the prompt string this is repeated three times.  This would be better if it was in a separate function that accepted the prompt string and the address of the variable to store the input value:

void GetInput(string prompt, int&amp; input)
{
    while (std::cout &lt;&lt; prompt &amp;&amp; !(std::cin &gt;&gt; input)) 
    {
        std::cin.clear(); //clear bad input flag
        std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); //discard input
        std::cout &lt;&lt; "Invalid input; please re-enter.\n";
    }
}

GetInput("Please enter the third term in the series: ",thirdTerm);
GetInput("Enter the third from the last term in the series: ",thirdLastterm);
GetInput("Please enter the sum of the series: ",sum);


Here:

    do {
        total = 0;
        vSeries.push_back(thirdTerm + (a * constantNumber));
        a++;
        for (int i = 0; i &lt; vSeries.size(); i++) {
            total += vSeries[i];
        }
    } while (total &lt; sum);


You're iterating through the vector getting a new total.  It would be better to just add each element to a running total:

    total = 0;
    do {
        int temp = thirdTerm + (a * constantNumber);
        vSeries.push_back(temp);
        total += temp;
        a++;
    } while (total &lt; sum);