Problem: A summation of two prompted numbers from the user

When I enter two numbers with the first one being smaller than the second the program functions correctly. When I enter two number with the first one being larger than the second the program command window is non-responsive after the second number is entered. Can someone please help me find what I am doing wrong?

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
47
48
 #include <iostream>
using std::cin; using std::cout; using std::endl;

/*
 * A program that finds the sum between two
 * numbers provided from the user. 
 */

int main()
{
	cout << "Enter two number: " << endl;
	int v1 = 0, v2 = 0, v3 = 0, v4 = 0, sum1 = 0, sum2 = 0;
	bool lock = false;		//lock to prevent both while loops executing
	cin >> v1 >> v2;		//reads in the two numbers from the user
	v3 = v1 + 1;			//variable for case 1
	v4 = v2 + 1;			//variable for case 2
	sum1 = v1;				//sum variable for case 1
	sum2 = v2;				//sum variable for case 2

	// Case 1: The first number entered is smaller than the second
	while(v3<=v2)
	{
		sum1 += v3;
		v3++;
		lock = true;
	}
	// Case 2: The second number entered is smaller than the first
	while(lock == false)
	{
		while(v4<=v1)
		{
			sum2 += v4;
			v4++;
		}
	}
	// Print Case 1
	if(lock == true)
	{
		cout << "The sum of " << v1 << " to " << v2 << " inclusive is " << sum1 << endl;
	}
	//Print Case 2
	if(lock == false)
	{
		cout << "The sum of " << v1 << " to " << v2 << " inclusive is " << sum2 << endl;
	}
	system("PAUSE");
	return 0;
Well, consider removing the whole boolean lock thing, and just using if-else statements to see which is met.
when the first number is larger than the second number you enter this loop.
1
2
3
4
5
6
7
8
while(lock == false)
	{
		while(v4<=v1)
		{
			sum2 += v4;
			v4++;
		}
	}


you don't update the lock variable so you never exit the loop
Last edited on
Thanks Yanson!

Since I am testing for the lock to decide which print statement to print depending on the case. I have decided to use break instead of updating my lock variable.

1
2
3
4
5
6
7
8
9
while(lock == false)
	{
		while(v4<=v1)
		{
			sum2 += v4;
			v4++;
		}
		break;
	}


The goal of this assignment is to use while loops. However, I am sure there probably is some fancy efficient way to do what I just did with less coding.
Last edited on
Is this a little cleaner maybe?

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
#include <iostream>
using std::cin; using std::cout; using std::endl;

/*
 * A program that finds the sum between two
 * numbers provided from the user. 
 */

int main()
{
	cout << "Enter two number: " << endl;
	int v1 = 0, v2 = 0, v3 = 0, v4 = 0, sum1 = 0, sum2 = 0;
	bool lock = false;		//lock to prevent both while loops executing
	cin >> v1 >> v2;		//reads in the two numbers from the user
	v3 = v1++;			//variable for case 1
	v4 = v2++;			//variable for case 2
	sum1 = v1;				//sum variable for case 1
	sum2 = v2;				//sum variable for case 2

	// Case 1: The first number entered is smaller than the second
	if(v1<=v2)
	{
		while(v3<=v2)
		{
			sum1 += v3;
			v3++;
			lock = true;
		}
		cout << "The sum of " << v1 << " to " << v2 << " inclusive is " << sum1 << endl;
	}

	// Case 2: The second number entered is smaller than the first
	if(lock == false)
	{
		while(v4<=v1)
		{
			sum2 += v4;
			v4++;
		}
		cout << "The sum of " << v1 << " to " << v2 << " inclusive is " << sum2 << endl;
	}

	system("PAUSE");
	return 0;
Actually, there's an easier way. Remove lock, and replace the second if with else.
Topic archived. No new replies allowed.