I am working on Project Euler, the first problem, and I’ve gotten this program to output the numbers I need, but I cannot figure out how to take the outputted numbers and add them together.
Here’s the code:
#include <iostream>
#include <cmath>
int main(void) {
int test = 0;
while (test<1000) {
test++;
if (test%3 == 0 && test%5 == 0) {
std::cout << test << std::endl;
}
}
std::cin.get();
return 0;
}
The easiest option would be a
totalvariable that you add to as the criteria match.The first step is creating it and initializing it to 0, so you end up with the right number later.
After that, subtotals are added to it, so that it accumulates the overall total.
Once you’ve added on the subtotals, you can just print it as the overall total.
Now, here’s how it fits into the code at hand, along with some other pointers: