Need help finding error!

Hello!

I've almost completed my code, however, at the end it displays a strange number for number_1... not sure why.

Any help would be great! Thanks!

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

int main()

{
    
// declare variables

	int number_1;
	int number_2; 
	int sum = 0; 

// ask for integers

	cout << "Enter first integer: ";
	cin >> number_1;
	
	cout << "Enter second integer: ";
	cin >> number_2;

// while statement to determine answer

	while (number_1 <= number_2)	
	{
		if(number_1 %2 != 0)
		
		    cout << number_1 << " is odd." << endl;
		    
		        else 		
		            sum += number_1;
            		number_1++;
	}
	
// display answers

	cout << "The sum of all even numbers between " << number_1 << " and " << number_2 << " is " << sum << "." << endl;
	cin.get();
}

strange number for number_1

I aint no wizard. Whats a strange number? What do you input, what do you expect it to output?*
Last edited on
At the end of the program it's suppose to display the sum of all even numbers between number_1 and number_2. Except instead of it reading what the user inputted as number_1, it reads a different number.

Example:

Enter first integer: 1
Enter second integer: 7
1 is odd.
3 is odd.
5 is odd.
7 is odd.
The sum of all even numbers between 8 and 7 is 12.
Last edited on
well duh

number_1++;

You're increasing number_1 xD.

If you wanna solve the problem, do something like this.

1
2
3
cout << "Enter first integer: ";
cin >> number_1;
int saveNumber_1 = number_1;


And then

cout << "The sum of all even numbers between " << saveNumber_1<< " and " << number_2 << " is " << sum << "." << endl;
Wonderful.

Thank you for all your help!
Topic archived. No new replies allowed.