Counting numbers gives wrong answer

I've made a program to count the number of total numbers used, to count the number of even numbers and to count how many times a number is less than 1 and greater than 200. However it isn't showing the correct results, when I input 60 it is saying that I've entered 1 invalid number and 0 even numbers but I don't understand why. Is it counting the -1? but if it was counting the -1 why isn't it counting 60 as even?

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
  	int main() {
		int number = 0;
		int even_count = 0;
		
		int counter = 1;
		int inbetween = 0;
		int greaterthan200lessthan1 = 0;


		cout << "Enter number: " << counter << ": ";
		cin >> number;


		while (number != -1) {

			counter++;


			cout << "Enter number: " << counter << ": ";
			cin >> number;

			if (number % 2 == 0) {
				even_count = even_count++;
					

			
					

			}

			if (number >= 1 && number <= 200) {
				inbetween++;
					

			}
			else {
				greaterthan200lessthan1++;
					
			}
		}

		cout << "The number of even number is:  " << even_count << endl;
		


		cout << "Numbers which are less that 1 and greater than 200 is: " << greaterthan200lessthan1 << endl; 

		cout << "Total of numbers entered is: " << counter << endl; 

		return 0;
	}
Last edited on
You never do anything with the first numbe the user enters at line 11. It the number is not -1, you immediately ask them to input another number, overwriting the previous value, at line 20.
Due to line 10/11 the first number will increase counter but is due to line 19/20 ignored.
Hello Robin53,

It is helpful if you post the complete compilable code. This includes any header files that you used.

Lines 10 and 11 are outside the while loop and do not get processed.

I would change the while loop to a do/while loop to keep the code the way it is.

After line 20 I would add an if statement to check for "-1" and if true "continue". This will avoid processing the "-1".

The last if/else does not work well. When you enter "-1" to finish "greaterthan200lessthan1" is incremented when it should not be.

Then you have a few to many blank lines that are not needed.

Working with what you have I offer this as a suggestion to make your code a bit neater:
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
#include <stdhead.hpp>

int main()
{
	int number = 0;
	int even_count = 0;

	int counter = 0;  // <--- Changed.
	int inbetween = 0;
	int greaterthan200lessthan1 = 0;

	//std::cout << "Enter number: " << counter << ": ";  // <--- These lines not processed.
	//std::cin >> number;

	do
	{
		counter++;

		std::cout << "Enter number: " << counter << ": ";
		std::cin >> number;

		// <--- Could use an if statement to check for "-1".
		if (number == -1)
			continue;

		if (number % 2 == 0)
			even_count++;

		if (number >= 1 && number <= 200)
			inbetween++;
		else if (number < 1 && number > 200)
			greaterthan200lessthan1++;

	} while (number != -1);

	std::cout << "The number of even number is:  " << even_count << std::endl;

	std::cout << "The inbetweeens are :  " << inbetween << std::endl;  // <--- Added for testing.

	std::cout << "Numbers which are less that 1 and greater than 200 is: " << greaterthan200lessthan1 << std::endl;

	std::cout << "Total of numbers entered is: " << counter << std::endl;

	return 0;
}

Please notice the comments in the program.

Hope that helps,

Andy
Handy Andy wrote:
Lines 10 and 11 are outside the while loop and do not get processed.

This is nonsense. Those lines get compiled properly, and they get executed properly. If they didn't, the OP wouldn't be seeing the problem they're seeing!

Please don't give beginners misleading, incorrect information.
Last edited on
@MikeyBoy,

You got me sorry. My thoughts were in a different direction and I was to far ahead of myself when I wrote that then I saw your first comment after I posted. That is more or less the idea I had just said it wrong.

I will try to improve on that.

Andy
But even when I type 210 it isn't being included in the

std ::cout << "Numbers which are less that 1 and greater than 200 is: " << greaterthan200lessthan1 << std::endl;
But even when I type 210 it isn't being included in the

std ::cout << "Numbers which are less that 1 and greater than 200 is: " << greaterthan200lessthan1 << std::endl;

With which version of the code? The one you posted in your OP? Or a version modified to fix the problem that several of us have told you about?
the modified version
Hello Robin53,

I do not see the modified version, so not sure what you have done.

Using the code I posted:
1
2
3
4
if (number >= 1 && number <= 200)
		inbetween++;
	else if (number < 1 && number > 200)
		greaterthan200lessthan1++;

Look at the "else if" statement line 3. When I wrote this I based it on what you had done and later found the problem. Think about the condition. How can "number" be less than zero and greater than 200 at the same time. In order for the condition to be true both sides of "&&" have to be true and that will not happen at the same time. In this case the "||" is what is needed because only one side has to be true for the whole to be true.

Earlier I said:
Lines 10 and 11 are outside the while loop and do not get processed.
I agree it is not the best statement, but still technically true. As MikeyBoy and coder777 have pointed out you enter a value for "number" on line 11 and then again on line 20. The value entered on line 11 is overwritten on line 20 and the first number is counted, but never processed inside the while loop.

This may work if the first number entered is "-1" it will cause the while loop to fail as it should, but anything other than "-1" will not be processed in the while loop because you enter a new number.

Hope that helps,

Andy
Yes it does help and I’ve got it working. The problem I’ve got now is that it counts -1 in the overall total but also in the overall total for numbers less than as a number but I don’t want it to because -1 is used to exit the loop

Also i need to change the int to double but when I do it won’t let me use if((number % 2 ==0) && (number >= 0))
Last edited on
Handy Andy wrote:
I agree it is not the best statement, but still technically true.

WTF? No! Technically, it is absolutely untrue. Those lines do get processed. Line 10 does prompt the user for a number. Line 11 does read a number from standard input, and stores that number in number. If those lines did not get processed, the OP would never have seen the problem that they reported.

Stop telling confusing and misleading lies to beginners. You are beginning to look like a malicious troll.
Last edited on
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
#include <iostream>

int main()
{
    const int LOWER_LIMIT = 1 ;
    const int UPPER_LIMIT = 200 ;

    int cnt_all = 0 ;
    int cnt_even = 0 ;
    int cnt_out_of_range = 0 ;


    std::cout << "enter numbers one by one. enter -1 or a non-digit to end\n" ;
    int number ;

    // execute the body of the loop if
    // a. a valid number is entered and b. it is not equal to -1
    while( std::cin >> number && number != -1 )
    {
        ++cnt_all ;
        if( number%2 == 0 ) ++cnt_even ;
        if( number < LOWER_LIMIT || number > UPPER_LIMIT ) ++cnt_out_of_range ;
    }

    std::cout << "count of all numbers: " << cnt_all << '\n'
              << "count of even numbers: " << cnt_even << '\n'
              << "count of odd numbers: " << cnt_all - cnt_even << '\n'
              << "count of numbers not in range [" << LOWER_LIMIT << ','
              << UPPER_LIMIT << "]: " << cnt_out_of_range << '\n'
              << "count of numbers in range: " << cnt_all - cnt_out_of_range << '\n' ;
}
Last edited on
Topic archived. No new replies allowed.