Many ways.
If you want the user to enter 4 things exactly, every run through the loop increment a counter and do it until that counter gets to four.
Code:
#include <iostream>
int main()
{
int counter = 1;
int sum = 0;
int value = 0;
while (counter < 5)
{
std::cin >> value;
sum += value;
counter++;
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
Or how about go until the user enters 0?
Code:
#include <iostream>
int main()
{
int counter = 1;
int sum = 0;
int value = 1; //Make sure it does not start at 0, but it can be 0 with a do-while loop
while (value !=0)
{
std::cin >> value;
sum += value;
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
Or you can do when it is anything less than 0, if it is exactly 99999 stop. The possibilities are endless.