Hi guys it's been a while but i'm back again . I recently rekindled my interest in C++ and while reading through a tutorial on the basics of C++ decided i would make a program that would tell you if the number you enter is odd or even, however when i tested it it told me that 5 is even... Code: #include <iostream> using namespace std; int main () { int a, c; float b; cout << "Enter a number and i will define it as odd or even" << endl; cin >> a; b=a/2; c = (int) b; if (c!=b) { cout << "The number is odd" << endl; } else { cout << "The number is even" << endl; } getchar(); return 0; } The basic idea here is that you enter a number into the integer variable a and then b is defined as half of that value. If the number is odd then b will be a float and if the number is even then it will be an integer. From there c is defined as b (integer), this means that if b happens to be a float then anything after the decimal point will be lost and c will not be equal to b. If c is not equal to b then the program should tell you that you entered an odd number, if it is equal to b then it should tell you it is an even number.
Without firing up c++ and testing it I would guesstimate that (int) 2.5 = 3. An easier way to do it would be: Code: if (a%2 == 0) {cout << "Even.\n";} else {cout << "Odd.\n";};
Would you mind explaining what modulo '%' does? I recognise the operator but don't actually understand what that does. That certainly seems a much more efficient way of doing it though, using only one variable.
Mod is basically divide until you get a remainder, the output is whatever's left. e.g. 5%2 = 1, i.e. 2 divides into 5 n times, and 1 is the remainder.
Although ninja'ed by Krikkit here's my version... Code: #include <iostream> int main(int argc, char *argv[]) { int a; while (true) { std::cout << "Enter a value: "; std::cin >> a; if (std::cin.fail()) { std::cin.clear(); break; } if (a % 2) std::cout << "The value was odd.\n"; else std::cout << "The value was even.\n"; } return 0; }
Ahh i see thanks very much +rep. Takes me back to my primary school maths lol Edit: thanks to azrael- too, i managed to get my own code working as well: Code: #include <iostream> using namespace std; int main () { int c; float a, b; cout << "Enter a number and i will define it as odd or even" << endl; cin >> a; b = a / 2; c = (int) b; if (c!=b) { cout << "The number is odd" << endl; } else { cout << "The number is even" << endl; } getchar(); return 0; } After commenting most of the code and printing b after declaring it as a / 2 i realised that a being an integer stopped b from ever becoming a float through this method of definition
You could cook this down to Code: std::cout << ((a % 2) ? "Odd." : "Even") << std::endl; if you really wanted. One liners Cheesecake
Using debug, I can tell you that : Enter 5 b = 2 because the value that your entering is an int, when it divides it, it doesn't give any decimal places, even though your putting it into a float. *edit* ninja'd you all had to be smarty pants didn't you. I was trying to remember the if with colon statement, but couldn't be bothered,
I do love a nice one-liner, I'm not efficient enough with my methods to get them going most of the time. All too often I do a bit of code, look up something I'm stuck on and find a 10 line chunk done in a couple of lines.
Well, to be brutally honest, while one-liners can look cool they can be equally hard to debug. Especially the longer, more convoluted ones. I *do* love terniary comparisons, though.
It's good practice to always initialise variables otherwise they are "undefined". int a = 0; int b = 0;
I mean you should initialise them when they are created. Consider this code: Code: float pi; pi = 3.17; float rad = 5; float area = pi * rad * rad; Some time down the line another developer comes along and tries to do this: Code: float pi; float my_rad = 10; float my_area = pi * my_rad * my_rad; pi = 3.17; float rad = 5; float area = pi * rad * rad; This will cause a run time error.
Of course, I know what you mean and variables/objects must be initialized, but not necesarrily at creation time. If you do the following: Code: int a = 5; int b = 6; int c = 7; a = b + c; then there is absolutely no point in initializing a and giving it the value of 5, the only important to note about initialization is to not try to use an uninitialized value, but this doesn't need we must initialize the values at the creation of the variable/object, because that's not necessary.
I wouldn't say that's either devs fault. Second one should have read the code properly and put the extra lines in below the initialisation. But the first dev could have used : #define pi 3.14159265
In that case, you wouldn't declare "a" until you wanted to assign a value to it: Code: int b = 6; int c = 7; int a = b + c; My point really is that there is no need to declare a variable and not assign a value to it.
Deadelus, that's not true. Consider this code: Code: class DeadelusIsWrong { private: int a; //a is a member of DeadelusIsWrong and it shouldn't be initialized when declared public: int getA() { return a; } /*When the DeadelusIsWrong object is created, a is initialized, so the error won't happen. I'm not saying you shouldn't initialize your variables when you create them, I'm saying it's WRONG to state that you must always initialize a variable/member when you declare it. This class was written to prove what I said earlier*/ DeadelusIsWrong(int a) { this.a = a; } } As you can see, a is a member of the class and it is declared before knowing it's value. There is no point of initializing a here and you don't have an option to declare a member after your constructor was executed.