I don't see any tildes anywhere in that code.
But I can recreate your problem. It seems to be some weird incompatibility with the listings package, since there is no problem if you don't use the lstlisting environment.
Two things to try, as I see it.
The first is to compile with XeLaTeX and remove the inputenc line altogether. (Use the
Fontspec package to choose a font with the appropriate characters.)
Another thing you could try, if you want to keep using regular latex/pdflatex and don't need to use any unusual utf8 characters, is to save the file as Latin 1 (ISO-8859-1) encoding. (See "convert to" in the file menu of Kile.) and change the inputenc line to:
\usepackage[latin1]{inputenc}