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?
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;
}
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.
#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++;
elseif (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;
}
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.
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 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++;
elseif (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.
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))
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.
#include <iostream>
int main()
{
constint LOWER_LIMIT = 1 ;
constint 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' ;
}