Difficulty setting Loop condition and/or increment

Hello. I do not fully understand how to set the condition for loops and add the incremental values. Currently, my code reads 42. I need it to continue to 413. I have read many posts and three different books but I am still unable to grasp this concept. Will you please try "putting it in other words" or direct me to a site that may offer a clearer view?
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
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdlib.h>    

using namespace std; 

// Beginning w/ start value, use a while loop to add up the numbers in the
// given range and increment

int main(void)
{
   int start;  
     cout << "Enter start number: " << endl; // prompt for start value
     cin  >>   start;   //   8
   int end;
     cout << "Enter end number: "   << endl; // prompt for end value
     cin  >>   end;     // 121 
     
   if (start > end) // start value cannot be greater than end value        
     { 
       cout << " Error" << endl;
       return 0;
      }
        
    else    // prompt for increment value
      cout << " Enter increment number " << endl;                                        
      int increment;
      cin >> increment; // 17
       
    if (increment <= 0) // increment value cannot be less than or equal to zero
      cout << "Error" << endl; 
          
    int sum = 0; // set sum to approach zero
          
    while (sum <= increment) //PROBLEM??? I don't know what condition to set or
                             // how to figure it out. Could it be incremented incorrectly?
                             
       {          
         sum = start + increment; // 25 =  8 + 17
         sum = sum   + increment; // 42 = 25 + 17 
           
       }
         cout << "Sum is " << sum << endl;                                 
    
    system("Pause");
return 0;
} // End main 
Last edited on
It's not just your loop condition that's incorrect... your whole loop logic is a little messed up.

while loops are very simple:

1
2
3
4
while( condition )
{
    body;
}


This will do the following:

1) Check the Condition. If it's false, the rest of the loop is skipped and your program continues on.
2) Otherwise, if the condition is true, perform the 'body'
3) After the body is performed, go to step #1 and check the condition again.


That's it.


Your condition here doesn't make much sense:

while (sum <= increment)

Since 'increment' is going to be added to sum each time the loop body is run... this means the loop body will run at most 1 time.


Your body also doesn't make much sense (and your comments do not reflect what is actually happening). It's as if you are mixing up your variables.

Here is your code again just for quick reference:

1
2
3
4
5
while (sum <= increment)
{
    sum = start + increment;
    sum = sum + increment;
}


Now let's walk through this line by line to see what is happening. Let's start by using the numbers you plugged in:

start = 8
end = 121
increment = 17
sum = 0 (you initialize it to zero)

1) Condition is checked (sum <= increment) becomes (0 <= 17). This is true... so the loop body executes

2) sum = start + increment; start=8 and increment=17... 8+17=25. So after this line of code... sum=25

3) sum = sum + increment; sum=25 (from step 2) and increment=17. 25+17=42. So after this line of code... sum=42

4) Loop body is complete, so check the condition again. (sum <= increment) becomes (42 <= 17). This is false... so the loop is complete. The loop body is not executed again.



A few things to note about this:

- You are not using 'end' anywhere... so the value the user inputs for it is completely ignored.
- You are initializing 'sum' with 0 (this is probably wrong).
Last edited on
Disch, thank you for the assist. I have made changes to the loop structure. The expected results are not exactly what I am looking for but they are close. After running, sum_for_while is 440
1
2
3
4
5
6
7
8
9
10
11
12
int sum_for_while = 0; // set sum to approach zero
int incrementedValue = start;

while (incrementedValue <= end) 
       {          
         incrementedValue = incrementedValue + increment;
         increment++;
         
         sum_for_while = sum_for_while + incrementedValue;
           
       }
         cout << "Sum (using a while loop) " << sum_for_while << endl; 
Last edited on
I don't think you need increment++;

As i understand it you have an expected final result of 413
start = 8, end = 121, increment = 17

The total 413 is the sum of this series:
8 + 25 + 42 + 59 + 76 + 93 + 110
where each term is 17 more than the one before it..

Maybe you should add some cout statements inside the loop to see what are the values of sum_for_while and incrementedValue on each iteration.

Chervil, thank you very much for the
add some cout statements inside the loop.
In doing so, I was able to see my math and how I had the statements transposed. It is ironic how the little things make all the difference.
Topic archived. No new replies allowed.