What is wrong with my code?

Below is my code, and I can't get it to compile. It keeps giving me an error on the line that says " if (num % 2 == 0) { ". The error message says "% illegal, left operand has type double" and I have no idea what that means. Can someone help me out, please?

#include <iostream>
using namespace std;

int main(void) {

double num, even, odd, total;

cout << "Welcome to integer tester!\n" << endl;
cout << "Please enter an integer: ";
cin >> num;

while (num >= 0) {
if (num % 2 == 0) {
cout << num << " is even." << endl;
total++;
even++;
}
else {
cout << num << " is odd." << endl;
total++;
odd++;
}
}

cout << "You tested " << total << " integers." << endl;
cout << even << " of those numbers were even." << endl;
cout << odd << " of those numbers were odd." << endl;

return 0;
}
Can you tell us what the concept of types means in C++?

http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/operators/ discusses the % operator but does not mention type double is not accepted. The only type I know of that can work is type int.
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
Hello ChrisK312,

After I loaded up the program and ran it I found that the while loop is an endless loop. Using my above code on line 12 you enter a value for "num", but never again. The while loop will always use this number from line 12 and become an endless loop.

Other than an additional "std::cin" and some minor adjustments to the output to keep everything from running together the program works.

Hope that helps,

Andy
I used your help to figure it out. Thank you very much
Topic archived. No new replies allowed.