0

C# Practice 12: Code Lesson

In this practice i have to write code for solving the area of a circle. The problem is, there is a bug. The Input of the same variable is not the same as the output. Anyone had this problem too?

18th Oct 2021, 10:20 AM
Anton Lieske
Anton Lieske - avatar
16 ответов
+ 7
Anton Lieske, console.Read() returns an integer. And there is a problem with it converting to double like that. It could be done using Parse. Or in this case it is easier to just use ReadLine instead: radius=Convert.ToDouble(Console.ReadLine());
18th Oct 2021, 10:41 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
Anton Lieske Aleksei Radchenkov Console.Read() returns as int and it reads only single character so when you enter 5 it considered as a character which print ASCII value. 53 is a ASCII value of 5 so Console.Write(Console.Read()) //print 53 when you enter 5 @Anton - This is not a bug.
18th Oct 2021, 11:54 AM
A͢J
A͢J - avatar
+ 4
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟, Oh, I didn't know that about read() returning character code))) Thanks :)
18th Oct 2021, 11:56 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Nice👍
18th Oct 2021, 10:53 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Anton Lieske This program will print ASCII value of 0-9 digits https://code.sololearn.com/cZMu8YBbQu7H/?ref=app
18th Oct 2021, 4:02 PM
A͢J
A͢J - avatar
+ 1
Anton Lieske Where is your code? And which language?
18th Oct 2021, 10:24 AM
A͢J
A͢J - avatar
+ 1
Anton Lieske, mate he meant which programming language. And we can't help you unless you show us your code.
18th Oct 2021, 10:27 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Ah sorry.. i forget to tell that it is c#
18th Oct 2021, 10:29 AM
Anton Lieske
Anton Lieske - avatar
+ 1
Anton Lieske I thought you were comparing input with test cases output. Now I understood you wanted to check your input is right or not.
18th Oct 2021, 11:51 AM
A͢J
A͢J - avatar
+ 1
Ok i didnt know that the int value of the string "5" is "53". nice to know, if something like this happens again. I forget to convert and he gives numerical values back, so i didnt doubt my code. That was the problem. Now it works fine. 👌
18th Oct 2021, 3:28 PM
Anton Lieske
Anton Lieske - avatar
+ 1
Thanks ✌️
18th Oct 2021, 7:43 PM
Anton Lieske
Anton Lieske - avatar
0
Thats the code: const double pi = 3.14; double radius; double solution; radius=Console.Read(); Console.Write(radius); Input: 5 Output:53
18th Oct 2021, 10:28 AM
Anton Lieske
Anton Lieske - avatar
0
Anton Lieske 1 - Convert radius input to double 2 - you don't have to print input, you have to print area of circle so use formula pi * r * r
18th Oct 2021, 10:30 AM
A͢J
A͢J - avatar
0
I know how to solve it. But the problem is that Input isnt the same as output.
18th Oct 2021, 10:32 AM
Anton Lieske
Anton Lieske - avatar
0
Please.. understand, that the output with a value of 53 doesnt makes sense when input was 5. There was no calculation in this code. So input has to be output.
18th Oct 2021, 10:34 AM
Anton Lieske
Anton Lieske - avatar
0
Ok now it works: 3.14; double solution; double radius = Convert.ToDouble(Console.ReadLine()); solution = radius * radius * pi; Console.Write(solution);
18th Oct 2021, 10:51 AM
Anton Lieske
Anton Lieske - avatar