Click here to Skip to main content
12,399,975 members (48,867 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# string convert Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static Random newletter = new Random();
        static void Main(string[] args)
        {
            
            string[] alphabet;
            Console.WriteLine("Typing reflex game"); Console.WriteLine(); Console.WriteLine();
            Console.WriteLine("You must type the letter you see as fast as you can");
            Console.WriteLine("Hit enter to start");
            Console.ReadLine();
            Console.WriteLine("The game starts in: 3"); System.Threading.Thread.Sleep(1000);
            Console.WriteLine("The game starts in: 2"); System.Threading.Thread.Sleep(1000);
            Console.WriteLine("The game starts in: 1"); System.Threading.Thread.Sleep(1000);
            ConsoleKeyInfo inputletter; 
            alphabet = new string[26];
            alphabet[0] = "A";
            alphabet[1] = "B";
            alphabet[2] = "C";
            alphabet[3] = "D";
            alphabet[4] = "E";
            alphabet[5] = "F";
            alphabet[6] = "G";
            alphabet[7] = "H";
            alphabet[8] = "I";
            alphabet[9] = "J";
            alphabet[10] = "K";
            alphabet[11] = "L";
            alphabet[12] = "M";
            alphabet[13] = "N";
            alphabet[14] = "O";
            alphabet[15] = "P";
            alphabet[16] = "Q";
            alphabet[17] = "R";
            alphabet[18] = "S";
            alphabet[19] = "T";
            alphabet[20] = "U";
            alphabet[21] = "V";
            alphabet[22] = "W";
            alphabet[23] = "X";
            alphabet[24] = "Y";
            alphabet[25] = "Z";
            do
            {
                Console.Clear();
                int lol = newletter.Next(0, 25);
                Console.WriteLine("Typing reflex game"); Console.WriteLine(); Console.WriteLine();
                Console.WriteLine("                     TYPE THIS LETTER:"); Console.WriteLine();
                Console.Write("                              "+ alphabet[lol]);
                inputletter = Console.ReadKey();
                inputletter = Convert.ToString;
                alphabet[lol].ToString();
                if (inputletter == alphabet[lol])
                {
                   // Add a point, or something
                }
                Console.WriteLine(inputletter.Key.ToString());
             } while (inputletter.Key != ConsoleKey.Escape);
            
            
        }
    }
}

This part:

inputletter = Console.ReadKey();
                inputletter = Convert.ToString;
                alphabet[lol].ToString();
                if (inputletter == alphabet[lol])
                {
                }

I'm practicing for college work and I'm trying to make a app where the user types in the letter shown then it shows how many they got correct out of how ever many attempted (and I will try put in a timer system). I don't know how to make it check if the inputletter (the key input when the letter is shown) is equal to the alphabet[lol] (being a random chosen letter in the alphabet array)

please help Frown | :(
and any other help would be greatly appreciated
- first time using this site

I get 2 errors:
Cannot convert method group ToString to non-delegate type System.ConsoleKeyInfo. Did you intend to invoke the method?

and:
Operator == cannot be applied to operands of type System.ConsoleKeyInfo and string
Posted 15-Dec-11 19:02pm
Updated 15-Dec-11 19:16pm
v4
Comments
Balakrishnan Dhinakaran 16-Dec-11 1:45am
   
my vote 5 for your clear scenario..
Balakrishnan Dhinakaran 16-Dec-11 1:48am
   
you just copy my code and use I hope it ll work fine
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Those errors appears because inputletter is a System.ConsoleKeyInfo[^] and you're trying to compare it with a string.

You should use KeyChar[^] property of inputletter
inputletter = Console.ReadKey();
if (inputletter.KeyChar.ToString() == alphabet[lol])
{
}
  Permalink  
Comments
Balakrishnan Dhinakaran 16-Dec-11 1:41am
   
my vote 5
Balakrishnan Dhinakaran 16-Dec-11 1:42am
   
if (inputletter.ToString() == alphabet[lol])
{
// Add a point, or something
}

I use this and it works well
Joel Whatley- 16-Dec-11 10:31am
   
Thanks everyone :) What an awesome site this is! <3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static Random newletter = new Random();
        static void Main(string[] args)
        {
            string[] alphabet;
            Console.WriteLine("Typing reflex game"); Console.WriteLine(); Console.WriteLine();
            Console.WriteLine("You must type the letter you see as fast as you can");
            Console.WriteLine("Hit enter to start");
            Console.ReadLine();
            Console.WriteLine("The game starts in: 3"); System.Threading.Thread.Sleep(1000);
            Console.WriteLine("The game starts in: 2"); System.Threading.Thread.Sleep(1000);
            Console.WriteLine("The game starts in: 1"); System.Threading.Thread.Sleep(1000);
            ConsoleKeyInfo inputletter;
            alphabet = new string[26];
            alphabet[0] = "A";
            alphabet[1] = "B";
            alphabet[2] = "C";
            alphabet[3] = "D";
            alphabet[4] = "E";
            alphabet[5] = "F";
            alphabet[6] = "G";
            alphabet[7] = "H";
            alphabet[8] = "I";
            alphabet[9] = "J";
            alphabet[10] = "K";
            alphabet[11] = "L";
            alphabet[12] = "M";
            alphabet[13] = "N";
            alphabet[14] = "O";
            alphabet[15] = "P";
            alphabet[16] = "Q";
            alphabet[17] = "R";
            alphabet[18] = "S";
            alphabet[19] = "T";
            alphabet[20] = "U";
            alphabet[21] = "V";
            alphabet[22] = "W";
            alphabet[23] = "X";
            alphabet[24] = "Y";
            alphabet[25] = "Z";
            do
            {
                Console.Clear();
                int lol = newletter.Next(0, 25);
                Console.WriteLine("Typing reflex game"); Console.WriteLine(); Console.WriteLine();
                Console.WriteLine("                     TYPE THIS LETTER:"); Console.WriteLine();
                Console.Write("                              " + alphabet[lol]);
                inputletter = Console.ReadKey();
                
                alphabet[lol].ToString();
                if (inputletter.ToString() == alphabet[lol])
                {
                    // Add a point, or something
                }
                Console.WriteLine(inputletter.Key.ToString());
            } while (inputletter.Key != ConsoleKey.Escape);
            
        }
    }
}
  Permalink  
Comments
Joel Whatley- 16-Dec-11 10:31am
   
Thanks!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Also does anyone know of a way to make the alphabet[lol] more random?
It doesn't seem very random at all :(
  Permalink  
Comments
Sergey Alexandrovich Kryukov 29-Apr-13 18:44pm
   
Not an answer, should be removed. If you want to ask a question, ask a question. Posts like that will only give you down-votes and abuse reports.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 16 Dec 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100