Getting int num;redefinition error in while loop

The problem is I'm getting a int num;redefinition error. I don't think whats in the braces is right either

int num = lower_num; --- (causes a int num;redefinition error.)
while (num <= higher_num, )
{
cout << (sum += num);
num++;
}

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*Write a program that asks the users for two integers, x and y.
It should then add up all the integers between (and including)
 x and y and output the result.If x and y are equal to each other,
your program should ask the user for two numbers again
and repeat until the user provides different values.*/

#include<iostream>
using namespace std;

int main()
{
	//variables
	int x, y, num, sum = 0;

	// enter 2 numbers x and y
	cout << (" Enter the first number \n");
	cin >> x;
	cout << (" Enter the second number \n");
	cin >> y;

	/*If x and y are equal to each other, your
	program should ask the user for two numbers again
	and repeat until the user provides different values.*/
	if (x == y)
	{
		cout << " The numbers equal each other. enter the two numbers again \n";
		cin >> x;
		cin >> y;
	}

	//Your program should work correctly if x > y or if x < y.
	int higher_num;
	int lower_num;

	if (x > y)
	{
		higher_num = x;
		lower_num = y;
	}
	else
	{
		higher_num = y;
		lower_num = x;
	}

	/*It should then add up all the integers between
	(and including) x and y and output the result.*/

          int num = lower_num;
	while (num <= higher_num, )
	{
			cout << (sum += num);
			num++;		
	} 
	
	cout << "\n The sum of all the integers between \n";
	cout << " (and including) " << x << " and " << y << " is " << sum << endl << endl;

	system("pause");
	return 0;
}
 
Last edited on
while (num = lower_num)

This while loop will never ever end (unless the lower number happens to be zero).
I'm getting a int num;redefinition error. I don't think whats in the braces is right either

int num = lower_num; (causes a int num;redefinition error.)
while (num <= higher_num, )
{
cout << (sum += num);
num++;
}

Last edited on
1
2
3
4
5
while (num <= higher_num, )
{


} 


This new while loop will never ever end.
> The problem is I'm getting a int num;redefinition error.
Look at lines 13 and 49.

Since you already have line 13, you can make line 49 just
num = lower_num;
Thanks salem c that works! Thanks everybody for your help getting me there I appreciate it !
Topic archived. No new replies allowed.