'while' loop using increment doesn't produce expected results

I know why it doesn't, I've done something wrong. The trouble is that the code is legitimate according to Dev-C++ and the syntax makes sense to me.

Basically I have a program which - theoretically - takes two integer inputs and outputs the sum of all integers between those two. For instance:
If the inputs are 5 and 8, the output is 13
If the inputs are -3 and 3, the output is 0
If the inputs are 4 and 5, the output is 0
However, what I'm getting is quite different and doesn't seem consistent:
13
2147483647
5

The 5 I understand, it's because using num3 = num1 + 1 (see below) is a bad idea. But I don't know how to resolve it. The 2147483647 makes no sense, obviously I've done something it doesn't like.

Here's my main:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ()
{
    int num1, num2, num3;
    
    cout << "Please enter two integers.";
    cin >> num1 >> num2;
    
    num3 = num1 + 1;
    
    while (num3 < num2)
    {
          (num3 = num3 + num3++);
    }
    
    cout << "The integers between the inputs add up to " << num3 << "." << endl;
    system ("pause");
}


Should I perhaps not even be using a while loop? It seemed the best option based on reading my textbooks.
Last edited on
Print out the intermediate results immediately after line 12. That should point you to the problem.
That gets the correct answer for 5/8, gets 5 for 4/5, and starts an infinite loop for -3/3. I don't see what I can do I'm sorry.

I've also just realised that this whole program can only possibly work if the inputs are entered in ascending order. So any hints as to how to start again would be greatly appreciated.
You're doing this while(accum < upper_limit) that doesn't make sense.
There is a better way of solving this problem, in constant time. Ask to Gauss.
You are trying to use num3 as both a counter and an accumulator. It can't hold the total and keep track of the next integer to add at the same time. You need another variable. What you need to do is count from 1 greater than the first value and stop before you reach the second value. Then a different variable must be used to keep the total.
Okay, this roughly works, but the flaw seems to be with the else clause. I don't understand the loop condition enough to figure this out, but I want 2 things:
- The else clause to bring the user back to the if clause (that is, once they enter the integers correctly the calculation takes place, and
- Once the calculation has been completed, the program pauses to exit on prompt.

So how would I go about fixing it, and on a side note what is the cleanest way of getting that pause? I usually use system ("pause"), but that seems like a waste of processing plus it only works under Windows.

(The worst that can happen here is that I change the if to a while and remove the else, plus the necessary rearrange. I could do that, I just want it to work even if people enter the integers the wrong way.)

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
#include <iostream>
using namespace std;

int main()
{
    int num1 = 0;
    int num2 = 1;
    
    cout << "Please select two integers (whole numbers), and enter them as prompted." << endl;
    cout << "Please enter the smallest integer: ";
    cin >> num1;
    cout << "Please enter the largest integer: ";
    cin >> num2;
            
    if(num1 <= num2)
    {
    int num3 = 0;
    for(int i=num1+1; i<num2; i++)
    {
            num3 += i;
    }
    
    cout << "The total of all integers between " << num1 << " and " << num2 << " is " << num3 << ".";
    
    cin.get();
    cin.get();
    }
    
    else(num1 > num2);
    {
              cout << "Please re-enter the integers in the opposite order." << endl;
              cout << "Please enter the smallest integer: ";
              cin >> num1;
              cout << "Please enter the largest integer: ";
              cin >> num2;
    }
}
To make it stop:

1
2
cout<<"Press any key to terminate the program.";
getch();
If I do that I just get an error saying it's an undeclared function. Is it not in the iostream header?
Consider swapping num1 and num2 yourself if necessary, instead of asking the user to re-enter them in the correct order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//...

int num1;
int num2;

cout << "enter two ints: ";
cin >> num1 >> num2;

if (num1 > num2)
{
    //swap them
}

//... 

As for the other problem, I believe the two cin.get(); statements are enough.
I did that, and now no matter what num1 and num2 values I enter the output is 2359208. I presume it's pulling a random value from memory because I've messed something else up. What is it? Even if I remove that entire second if clause it still produces the same result. Here's the full source, yet again.

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
#include <iostream>
using namespace std;

int main()
{
    int num1, num2, num3;
    
    cout << "Please select two integers (whole numbers), and enter them as prompted." << endl;
    cout << "Please enter the first integer: ";
    cin >> num1;
    cout << "Please enter the second integer: ";
    cin >> num2;
            
    if(num1 <= num2)
    {
    int num3 = 0;
    for(int i=num1+1; i<num2; i++)
    num3 += i;
    }

    if(num1 > num2);
    {
    int num3 = 0;
    for(int i=num2+1; i<num1; i++)
    num3 += i;
    }

    cout << "The total of all integers between " << num1 << " and " << num2 << " is " << num3 << "." << endl;
    cout<<"Press any key to terminate the program.";
    cin.get();
    cin.get();
}
You are declaring num3 to be local to the for() loop, so it is masking the other num3 that you declared at the beginning and print out at the end.

You need to use the variable num3 without redeclaring it:
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
#include <iostream>
using namespace std;

int main()
{
    int num1, num2, num3;
    
    cout << "Please select two integers (whole numbers), and enter them as prompted." << endl;
    cout << "Please enter the first integer: ";
    cin >> num1;
    cout << "Please enter the second integer: ";
    cin >> num2;
            
    num3 = 0; // Right! alters the original variable previously declared
    if(num1 <= num2)
    {
    // int num3 = 0; // Wrong. Declares a local replacement for num3 obscuring the original
    for(int i=num1+1; i<num2; i++)
    num3 += i;
    }

    if(num1 > num2);
    {
    // int num3 = 0; // Wrong. Declares a local replacement for num3 obscuring the original
    for(int i=num2+1; i<num1; i++)
    num3 += i;
    }

    cout << "The total of all integers between " << num1 << " and " << num2 << " is " << num3 << "." << endl;
    cout<<"Press any key to terminate the program.";
    cin.get();
    cin.get();
}


EDIT: Fixed my bug ;o)
Last edited on
Thank you all so much. Perfect! And I've learnt something that I hopefully won't do again.
Topic archived. No new replies allowed.