Initial Integer won't output after cmath coding.

Hi all. I'm having a problem with my output displaying the correct integer. User is prompted to enter 2 integers. Part of the code determines evens and odds and sum of even.

After the coding of the Even and Sum requirements, I output the sum of the evens, but when coding the cout of the firstNum and secondNum (entered by user), the firstNum is displaying the wrong number, which I believe is because of the previous coding counter.

Below is code; then following is the text in the console output.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	int firstNum, secondNum;
	int evens, odds;
	int sum = 0;

	cout << "Enter 2 integers. The second must be larger \n"
	     << "than the first number.";
	cin >> firstNum >> secondNum;
	cout << endl;

	if (firstNum >= secondNum)
	{
		cout << "Your first number is not smaller than the second. \n"
			 << "Please reenter the two numbers.";
		cin >> firstNum >> secondNum;
		cout << endl;
	}
	else
		while(firstNum <= secondNum) 
		{
		if(firstNum % 2 != 0)
			cout << firstNum << " is odd." << endl;
		else
			sum += firstNum;
		firstNum++;
		}
	
	cout << "The sum of all even numbers between " << firstNum << " and " << secondNum << " is " << sum << "."
		 << endl;
	
	
	return 0;
}


[OUTPUT]

Enter 2 integers. The second must be larger
than the first number. 1 13

1 is odd.
3 is odd.
5 is odd.
7 is odd.
9 is odd.
11 is odd.
13 is odd.
The sum of all even numbers between 14 and 13 is 42.
Press any key to continue . . .


The BOLD and UNDERLINED number is supposed to be 1 not 14. And the Sum is NOT supposed to be 42, but instead the Sum of all even numbers.

Ideas of how to reverse this?
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. You are using firstNum as a counter variable here:

26
27
28
29
30
31
32
33
while(firstNum <= secondNum) 
		{
		if(firstNum % 2 != 0)
			cout << firstNum << " is odd." << endl;
		else
			sum += firstNum;
		firstNum++; //this increases it by 1
		}


What you want to do is create an extra counter variable, or use a for loop:

26
27
28
29
30
31
32
for(int i=firstNum; i<= secondNum; i++) 
		{
		if(i % 2 != 0)
			cout <<i << " is odd." << endl;
		else
			sum += i;
		}


That way firstNum is left alone and will show correctly.

Hope that helps.

All the best,
NwN
Interesting.

So always create an extra counter var then, which doesn't change the value of the variable, correct?

Thanks for the help, NwN.
You don't have to create an extra variable. Instead you could output the text at an earlier stage, before you modified the value of the variable
cout << "The sum of all even numbers between " << firstNum << " and " << secondNum << " is ";
After this you are free to change the values - though you may prefer not to.

And the Sum is NOT supposed to be 42, but instead the Sum of all even numbers

2+4+6+8+10+12 does equal 42

Edit: actually my first suggestion would change the output in an undesirable way, because there are other cout statements before reaching the final result. So probably best to ignore that idea for now - though it might apply in other circumstances.
Last edited on
Chervil;

Yes the sum is supposed to be 42. LOL
I rechecked it myself and felt dumb. However, I think I dismissed it initially as being wrong, before checking, because my 14 was wrong as the firstNum.

Thanks for your feed back.
closed account (o3hC5Di1)
CFalkirk wrote:
So always create an extra counter var then, which doesn't change the value of the variable, correct?


"Always" is a dangerous word to use in programming, but in general I would suggest doing so, yes. Mainly because it increases readability. It makes it clear that it is a counter variable and that firstNum holds the first number typed in. Improved readability not only for others, but also for you to debug these kinds of problems.

All the best,
NwN
Would you then mind helping me with one more problem with the same coding?

Farther in, I am supposed to list Squares between 1 and 10 of the firstNum. And then the SUM of the odd squared numbers.

I seem to have successfully outputted the Squares...but my coding after that output to SUM is not outputting at all.

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
	int firstNum, secondNum;
	int sum = 0;
	
	cout << "Enter 2 integers. The second must be larger \n"
		 << "than the first number.";
	cin >> firstNum >> secondNum;
	cout << endl;

	int a = firstNum, b = secondNum;

	if (firstNum >= secondNum)
	{
		cout << "Your first number is not smaller than the second. \n"
			 << "Please reenter the two numbers.";
			cin >> firstNum >> secondNum;
		cout << endl;
	}
	else
		for(int i = firstNum; i <= secondNum; i++) 
		{
		if(i % 2 != 0)
			cout << i << " is odd." << endl;
		else
			sum += i;
		}
	
		cout << "The sum of all even numbers between " << firstNum << " and " << secondNum << " is " << sum << "."
		 << endl;

	cout << "The following are the Squares of " << firstNum << " up to 10." 
		 << endl;

		
	while(a <= 10)
	{
		cout << a * a << endl;
			a++;
	}
		
	for(int j = a; j <= b; j++) 
	{
		if(j % 2 != 0)
			sum += j;
	}

	
	return 0;
}



I know I have something not right at the bottom, but I've been flipping page after page in my book, and can't find a solution. It's possible I've done the logic wrong.

One more time; once appreciated.
closed account (o3hC5Di1)
Hi there,

I think you need to take a little break from this, because I'm sure you know the answer:

In line 48 to 52 (the for loop) you calculate the sum you are talking about. However, there is no std::cout statement after that which writes it to the screen.

Rest is as important in work as the actual work itself ;-)

All the best,
NwN
Good call. Give my brain and eyes a rest.
Topic archived. No new replies allowed.