+ 2

Char to Int

How i block the user to write a char variable in int variable?becas if i write characteres in variable int, the programang is go to error..(bad english sry)

9th Mar 2018, 3:55 AM
Bruno Guilherme
Bruno Guilherme - avatar
5 ответов
+ 5
you could always use a try catch block were an exception occurs this way you can ask the user to input the specifics using the println statement inside the catch block
9th Mar 2018, 6:34 AM
D_Stark
D_Stark - avatar
+ 1
@Timon Thanks, I wasn't sure. I usually cast them anyways for consistency.
9th Mar 2018, 5:54 AM
Lewis
Lewis - avatar
0
Although I'm not entirely sure I understand what you're asking.. In C/C++ you shouldn't have any problem storing a char type value inside an int type since it's the same value just stored inside a larger container. You might need a cast first though like so.. int example = (int) 'A'; The cast is where "int" is in parentheses. This tells the compiler to treat the char literal "A" as an int type, thus casting the character into an integer. If I've misunderstood your question please provide more information and I'll try to help if I can.
9th Mar 2018, 4:42 AM
Lewis
Lewis - avatar
0
In this case, you don't need a cast.
9th Mar 2018, 5:18 AM
Timon Paßlick
0
It sounds like you need user input validation. Before you try to cast you should check the value is within an allowed range.
9th Mar 2018, 9:28 AM
Kerrash
Kerrash - avatar