Number Changer
| bluezor (87) | |||
This program will make you enter a number. If that number is even, it'll print it is even and then either multiply, add, subtract or divide a number (randomly) from it. If it's odd, it'll print it is odd and add one to make it even. Then as before, multiply, add, subtract or divide a number (randomly).
I try running it, but it always does a == 1 only (multiply) and not the others. It also can only multiply by 1, instead of 1 to 4. | |||
| Duoas (1596) | |||||||
| 1. #include <cstdlib> 2. If you tell the user that you are changing input into an even number, you ought to actually do it.
3. Operator precedence rules will bite you. I always avoid the problem by using parentheses liberally.
4. Lines 54..60 should be something like:
Hope this helps. [edit] BTW, nice start! | |||||||
| Mertovun (10) | |||
| i think you should use <time.h> instead of <ctime>. And i m not sure but i think there is another problem. If number is even nothing is happened. If it is a problem it is because of all the process is in the borders of first "else if" which is about the odd numbers. And if number is even the the program goes to the startpoint. If you want to change this you should cut the "}" sign at 57th line and paste it to the 32th. Sorry about poor English. :) | |||
| Duoas (1596) | |||
| In a C++ program, use <ctime> not <time.h> I'm posting again because I forgot to mention: 5. Lines 19 and 36: e can be zero, which causes a divide-by-zero fault. | |||
| helios (1521) | |||
| Duoas: Doesn't == have a lower precedence than +? bluezor: Wouldn't it be easier to add 1 to 'a' in the initialization? Or compare it to 0, 1, 2, and 3? | |||
| bluezor (87) | |||
| Hello. Thanks for your help Duoas and anyone else! =) Duoas, how do I make the randoms something like 1-7 instead of 0-7? And do you mind explaining how Switch works? I've read it in the tutorial, but I don't really get it. Thanks! | |||
| Poke386 (77) | |||
| @bluezor Something like rand_int=rand()%7+1 should do it. a switch-case statement is like this: switch(some_variable) { case someconstant: statement1; break; case someotherconstant: statement2; break; ..... default: statement; break; } this is the same as if(some_variable==someconstant) { statement1; } else if(some_variable==someotherconstant) { statement2; } ...... else { statement; } easy enough and some times helpful, but not really necessary. | |||
| Duoas (1596) | |||
Yoinks! So it does. | |||
This topic is archived - New replies not allowed.
