Hola! I am having a little difficulty with this problem: using a loop print out the sum of five integers, provided by the user, greater than 0 and less than 15. My idea for the solution was an array that would hold the inserted values. Then a loop would change the n variable to do the sum. This is my code:
#include <iostream>
usingnamespace std;
int main()
{
int integer[5] ;
int sum ;
cout << "Please insert five integers\n" ;
cin >> integer[0] >> integer[1] >> integer[2] >> integer[3] >> integer[4] ;
for (int n = 0; n < 6; n++)
{
if ((integer[n] > 0) && (integer[n] < 15))
{
sum += integer[n] ;
}
}
cout << "The sum of all the numbers greater than zero " << "and less than 15 is " << sum << "\n" ;
return 0;
}
I thought that integer[n] in the for loop would pull the correct number from the array as n increased and produce the correct sum, but as my presence here shows my assumption wasn't successful. My program ends up producing this:
Please insert five integers
1 2 3 4 5
The sum of all the numbers greater than zero and less than 15 is -1208219521