While Loop Problem

Hello,

I am currently learning about While Loops in my C++ class, and while this has been mostly no issue for me, I am having terrible trouble creating the following program:
__________________________________________________________________________
"Allow a user to input an integer and then display only those integers between -30 and 50 inclusive that are evenly divisible by the user entered value. Also display the sum of such numbers. Say user inputs 10. Then you need to display below:

'Here are the numbers evenly divisible by 10: -30 -20 -10 0 10 20 30 40 50
Their sum is: 90'

Note that in above output 10 should change according to the user input."
__________________________________________________________________________

Below is the code I have created thus far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main() {

	int numMain;
	int i = -30;

	cin >> numMain;

	while ((i >= -30) && (i <= 50)) {
		if (i % numMain == 0) {
			cout << i << endl;
	}
		i++;
		cout << "Here are the numbers evenly divisible by " << i << " :" << endl;
		
	}

	cout << "Their sum is: " << endl;

	system("pause");
	return 0;
}
What's the very first thing that is meant to be output?

Where in your code are you doing that? Is it the first thing to be output?
Last edited on
To give you an idea...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
int main() {

	int numMain;
	int i = -30;

	int runningTotal{ 0 }; // Variable to maintain the running total.

	cin >> numMain;
	cout << "Here are the numbers evenly divisible by " << numMain << ": ";
	while ((i >= -30) && (i <= 50)) {
		if (i % numMain == 0) {
			runningTotal += i;
			cout << i << " "; //endl;
		}
		i++;
		//cout << "Here are the numbers evenly divisible by " << i << " :" << endl;

	}

	cout << "\nTheir sum is: " << runningTotal << endl;

	system("pause");
	return 0;
}


Output:
10
Here are the numbers evenly divisible by 10: -30 -20 -10 0 10 20 30 40 50
Their sum is: 90
Press any key to continue . . .
Thank you SO much! Did not even think to reset starting parameter after each iteration.
Topic archived. No new replies allowed.