Search code examples
c#stringinputcastingcharacter

How to type different words as variable entry data


I’m new to programming and I’m trying to write a code that does this:

Entry data

Name 1

Time 1

Name 2

Time 2

Output

Show the name that has the best time.

Now, I’ve managed to create a code that shows the correct result if I write the entry data like this:

A

200

B

300

(numbers are random)

Here is what I have tried so far -

 string inputdata = Console.ReadLine(); 
char name = Convert.ToChar(inputdata); 
inputdata = Console.ReadLine();
 double time1 = Convert.ToDouble(inputdata); 
inputdata = Console.ReadLine(); 
char name2 = Convert.ToChar(inputdata); 
inputdata = Console.ReadLine(); 
double time2 = Convert.ToDouble(inputdata); 
inputdata = Console.ReadLine(); 
if (time1 < time2) { Console.WriteLine(name); } 
else { Console.WriteLine(name2);

I don’t know how to type into the console the full name (any full word) so I don’t get “String must be exactly one character long”.

I’ve been strugling a bit with this and can’t seem to find the right answer. Thank you!


Solution

  • I think that this should do the trick.

    Here is the link so you can try it out.

        Console.WriteLine ("Name 1: ");
        string name1 = Console.ReadLine(); //put name into a string, so we can use it after
    
        Console.WriteLine ("Time 1: ");
        int time1 = Convert.ToInt32(Console.ReadLine()); //put time into an int, so we can see who has better time
    
        Console.WriteLine ("Name 2: ");
        string name2 = Console.ReadLine();
    
        Console.WriteLine ("Time 2: ");
        int time2 = Convert.ToInt32(Console.ReadLine());
        
        if(time1 > time2) //checking who has better time
         Console.WriteLine(name1 + " has the better time."); //writing the name with the message
        else if(time1 < time2)
         Console.WriteLine(name2 + " has the better time.");
        else
          Console.WriteLine(name1 + " and " + name2 + " have the same time");