Adding by increments in a C++ while loop

I've been stumped on this one part on my assignment for hours. I've searched to the far reaches of the internet, but with no avail. All I need help with is what to do on step six and seven. I'm confused on how to add up the increments.

Here are the instructions:

The program shall follow these steps:
1. Declare the variables that you will need for this program. When doing so, declare a variable to hold the sum from the while loop approach and a variable to hold the sum from the for loop approach
2. Prompt the user for three integer values, a starting value, an ending value, and an increment value, and read in these values
3. If the starting value is greater than the ending value, print an error message followed by return 0;, which causes the main function to end and the program to terminate
4. If the increment value is less than or equal to zero, print an error message followed by return 0;, which causes the main function to end and the program to terminate
5. Set the sum of the while loop approach to zero (This is analogous to pushing Clear on a calculator)
6. Beginning with the starting value, implement an algorithm that uses a while loop to add up the numbers in the given range and increment
7. Print the sum that results from using the while loop approach
8. Set the sum of the for loop approach to zero
9. Beginning with the starting value, implement an algorithm that uses a for loop to add up the numbers in the given range and increment
10. Print the sum that results from using the for loop approach. (As a way of testing your two implementations, the two sums should be the same)
Note that the program does not calculate any sums if an error is detected in the input values. It just prints an error message and terminates.

Below is an example execution of the program. In this case, the program added up the numbers 8, 25, 42, 59, 76, 93, and 110. Your program shall follow the same format shown below for prompting the user and printing the results.

Enter a starting integer value: 8
Enter an ending integer value : 121

Enter a positive increment: 17

Sum (using a while loop): 413

Sum (using a for loop): 413

Here is the code I have so far.

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
27
28
29
30
31
32
33
34
  
#include <iostream>
#include <stdlib.h>    

using namespace std; 


// ############################################################
int main(void)
{
	int x;
	int y;
	int z;
	int sumW = 0;
	int sumF;
	{
	
	
		cout << "Enter a starting integer: ";
			cin >> x;
		cout << "Enter an ending integer: ";
			cin >> y;
		cout << "Enter a positive increment: ";
			cin >> z;
			
		{
			if (z <= 0 || x > y) cout << "Error ";
			if (z <= 0 || x > y) return 0;
		}
		
		while (x < y)
		{
			
		}
Last edited on
The sum is 0 at start.
A number is equal to to starting value.

Now you will add the number to the sum and then add the increment to the number. You repeat this as long as the number does not exceed the ending value.

With the example numbers:
1
2
3
4
5
6
7
8
9
10
sum = 0
number = 8

// first iteration:
sum becomes 8
number increases to 25 (8+17)

// second iteration:
sum becomes 33 (8+25)
number increases to 42 (25+17)
But how do you turn that into code?
Topic archived. No new replies allowed.