# 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 <iostream>
#include <vector>

int main() {
int thirdTerm;
int thirdLastterm;
int sum;
int total;
int constantNumber = 1;
int a = 0;
std::vector<int> vSeries;

while (std::cout << "Please enter the third term in the series: " && !(std::cin >> thirdTerm)) {
std::cout << "Invalid input; please re-enter.\n";
}
while (std::cout << "Enter the third from the last term in the series: " && !(std::cin >> thirdLastterm)) {
std::cout << "Invalid input; please re-enter.\n";
}
while (std::cout << "Please enter the sum of the series: " && !(std::cin >> sum)) {
std::cout << "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 < vSeries.size(); i++) {
total += vSeries[i];
}
} while (total < sum);

constantNumber++;

if (constantNumber > 100000) {
std::cout << "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 << "\nNumber of terms in the series: " <<  vSeries.size() << std::endl;
for (int i = 0; i < vSeries.size(); i++) {
if (i == 0)
std::cout << vSeries[i];
else
std::cout << "," << vSeries[i];
}

return 0;
}


A couple of things I noticed:

Here:

while (std::cout << "Please enter the third term in the series: " && !(std::cin >> thirdTerm)) {
std::cout << "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& input)
{
while (std::cout << prompt && !(std::cin >> input))
{
std::cout << "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 < vSeries.size(); i++) {
total += vSeries[i];
}
} while (total < 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 < sum);