I got an error called "unable to convert string to char"
i used the statement
con = (char)(Console.Read());
Newbie here Please Help
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jun 7, 2013, 9:43 AM EDT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
char choice;
char con ='y';
Console.WriteLine("What is the command keyword to exit a loop in C#?");
Console.WriteLine("a.quit");
Console.WriteLine("b.continue");
Console.WriteLine("c.break");
Console.WriteLine("d.exit");
while (con=='y')
{
Console.Write("\nEnter your choice:");
string input = Console.ReadLine();
if (input.Length != 1) continue;
choice = input[0];
if (choice == 'c')
{
Console.WriteLine("Congratulation!");
}
else if (choice == 'q' || choice == 'e')
{
Console.WriteLine("Exiting...!");
break;
}
else Console.WriteLine("Incorrect!");
Console.Write("Again? press y to continue:");
input = Console.ReadLine();
if(input.Length == 1)
{
con = input[0];
}
else break;
}
}
}
}
VulpesRank #55 · 29.2K reputationPosted Jun 5, 2013, 10:17 AM EDT
If you'd have used Console.ReadLine() instead of Console.Read() then you would have got the "unable to convert string to char" error because that method returns a string rather than an int.
So, I suspect the error is not occurring on that line but somewhere else?
lendon medalle7 reputationPosted Jun 6, 2013, 11:02 AM EDT
Sorry for copying it.
by the time i input an answer
the output will be like
Incorrect!
Again? press y to continue:
-> then it just exit with no prompt.
Congratulations!
Again? press y to continue:
-> then it just exit with no prompt.
using System;
Console.Write("Again? press y to continue:");using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
char choice;
char con='y';
Console.WriteLine("What is the command keyword to exit a loop in C#?");
Console.WriteLine("a.quit");
Console.WriteLine("b.continue");
Console.WriteLine("c.break");
Console.WriteLine("d.exit");
while (con=='y')
{
Console.Write("Enter your choice:");
choice = (char)(Console.Read());
if (choice == 'c')
{
Console.WriteLine("Congratulation!");
}
else if (choice == 'q' || choice == 'e') { Console.WriteLine("Exiting...!"); break; }
else Console.WriteLine("Incorrect!");
con = (char)(Console.Read());
}
}
}
}
VulpesRank #55 · 29.2K reputationPosted Jun 6, 2013, 1:23 PM EDT
Try this:
lendon medalle7 reputationPosted Jun 7, 2013, 9:21 AM EDT
I put already a Console.ReadLine(); at the end of the while loop but still the program exit without permission with me.
lendon medalle7 reputationPosted Jun 8, 2013, 6:06 PM EDT
+1