Code: result = number1 / number2; I am new to C++, but I have some knowledge in Java. What my program does is, the program asks the user two numbers. The user could only select, one, two, or zero. Then the user would select an arithmetic operator: plus, minus, times, divide. I figured everything out except the division part. When the user select one divide by two, the answer should come out as 0.5, but it comes out as 0. All three variables are int values, so I would need to convert them into a float or a double. But how would I cast them? I've tried doing this, float(number1), and many more similar to that, but it wouldn't work. Thanks, Sam
Itz a wrong procedure...Dnt have to use so many casting...only one casting will do...like the below:- Code: ((double)number1/number2) Now, let me tell the reason...the implicit casting follows the cascading rule from int to float.. i.e. if one number is float and other is int then the int part is converted to float first and then the result is evaluated...so many casting are not needed...I just need to convert any of the number to double thatz all...On the other hand if you are storing the result to "result" variable then the variable should be double.... Thank you!!
I agree that my method does extra casting but I don't thing it is wrong. Your syntax also does the same thing without actually specifying it i.e. converts each number into the double.
I didn't say itz wrong..i said itz a wrong procedure...If you keep on increasing the code complexity like this then I dnt think that itz should be said as a good procedure...if you give such solutions to a newbie then the person would get confused...u should have given him the explanations why u did like that...u placed double wherever u found the space for it.....