Hello ChrisK312,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Line 13
if (num % 2 == 0)
. What you are not understanding from the error message is that the lhs (left hand side) of "%" needs to be an "int", but you have defined "num" as a double. I have not looked at rjphares's link for awhile, but I believe this link should explain it.
It does not look like you are doing anything that would need a double, so I would define the variables on line 6 as 'int"s and that should solve your problem.
You should
ALWAYS initialixe your variables before you use them. It is good programming and good practice to do so. Your compiler should be up to the C++11 standards in which case all you need is an empty set of {}s. Like:
int num{}, even{}, odd{}, total{};
. This will set each variable to zero when it is defined and will avoid other errors like using an uninitialized variable.
Another suggestion:
Try to avoid using
using namespace std;
in your programs it may seem easy now, but
WILL get you in trouble some day.
It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy.
What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.
My last suggestion has to do with the {}s. If you use the following example you will find it much easier to find a matching set of {}s when they line up in the same column.
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
|
#include <iostream>
//using namespace std; // <--- Best not to use.
int main(void)
{
int num{}, even{}, odd{}, total{}; // <--- Changed to "int" and initialized the variables.
std::cout << "Welcome to integer tester!\n" << std::endl;
std::cout << "Please enter an integer: ";
std::cin >> num;
while (num >= 0)
{
if (num % 2 == 0)
{
std::cout << num << " is even." << std::endl;
total++;
even++;
}
else
{
std::cout << num << " is odd." << std::endl;
total++;
odd++;
}
}
std::cout << "You tested " << total << " integers." << std::endl;
std::cout << even << " of those numbers were even." << std::endl;
std::cout << odd << " of those numbers were odd." << std::endl;
return 0;
}
| |
The program looks OK, but I have not tested it yet. As I was reading the code I noticed "Welcome to integer tester!", but you defined "num" as a double. Not much logic there.
Hope that helps,
Andy