CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    7

    Using a char as the control variable in a loop.

    Hello, I am new to programming and c#, so please bear with me. I have a problem using a char as the control variable in a loop. I am able to get the code to work fine when using a string as the control variable, but can someone tell me what is wrong in the original code to cause the error I'm experiencing. It seems when using a char, the loop goes through two extra iterations for each input.

    Original Code:

    Code:
    //This program asks for a character input
    //then determines if said character is a 
    //lower case letter.  Typing an ! will close
    //the program.
    
    using System;
    
    namespace EnterLowerCaseLetters
    {
        class Program
        {
            static void Main(string[] args)
            {
                const char CLOSE = '!';
                char c;
                do {
                    Console.Write("Enter a lower case letter: ");
                    c = Convert.ToChar(Console.Read());
                    if (Char.IsLower(c) && Char.IsLetter(c))
                        Console.WriteLine("Ok");
                    else if (c != CLOSE)
                        Console.WriteLine("Error");
                } while (c != CLOSE);
                Console.ReadLine();
                Console.ReadLine();
            }
        }
    }
    Output:

    Code:
    Enter a lower case letter: g
    Ok
    Enter a lower case letter: Error
    Enter a lower case letter: Error
    Enter a lower case letter: G
    Error
    Enter a lower case letter: Error
    Enter a lower case letter: Error
    Enter a lower case letter: 1
    Error
    Enter a lower case letter: Error
    Enter a lower case letter: Error
    Enter a lower case letter: #
    Error
    Enter a lower case letter: Error
    Enter a lower case letter: Error
    Enter a lower case letter: !
    Working Code:

    Code:
    //This program asks for a character input
    //then determines if said character is a 
    //lower case letter.  Typing an ! will close
    //the program.
    
    using System;
    
    namespace EnterLowerCaseLetters
    {
        class Program
        {
            static void Main(string[] args)
            {
                const char CLOSE = '!';
                string input;
                char c;
                do {
                    Console.Write("Enter a lower case letter: ");
                    input = Console.ReadLine();
                    c = Convert.ToChar(input);
                    if (Char.IsLower(c) && Char.IsLetter(c))
                        Console.WriteLine("Ok");
                    else if (c != CLOSE)
                        Console.WriteLine("Error");
                } while (input != "!");
                Console.ReadLine();
                Console.ReadLine();
            }
        }
    }
    Output:

    Code:
    Enter a lower case letter: g
    Ok
    Enter a lower case letter: G
    Error
    Enter a lower case letter: 1
    Error
    Enter a lower case letter: %
    Error
    Enter a lower case letter: !

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using a char as the control variable in a loop.

    Step through the code in a debugger and see what the values of c are. Or display what c is to the console window.

  3. #3
    Join Date
    Oct 2013
    Posts
    7

    Re: Using a char as the control variable in a loop.

    Thanks for the reply! I added some code to show the value of c, but I'm still not sure what's wrong.

    Code:
    //This program asks for a character input
    //then determines if said character is a 
    //lower case letter.  Typing an ! will close
    //the program.
    
    using System;
    
    namespace EnterLowerCaseLetters
    {
        class Program
        {
            static void Main()
            {
                const char CLOSE = '!';
                char c;
                do {
                    Console.Write("Enter a lower case letter: ");
                    c = Convert.ToChar(Console.Read());
                    if (Char.IsLower(c) && Char.IsLetter(c))
                        Console.WriteLine("Ok");
                    else if (c != CLOSE)
                        Console.WriteLine("Error");
                        Console.WriteLine("c = " + c);
                } while (c != CLOSE);
                Console.ReadLine();
            }
        }
    }
    Code:
    Enter a lower case letter: g
    Ok
    c = g
    Enter a lower case letter: Error
    c =
    Enter a lower case letter: Error
    c =
    
    Enter a lower case letter: G
    Error
    c = G
    Enter a lower case letter: Error
    c =
    Enter a lower case letter: Error
    c =
    
    Enter a lower case letter:

  4. #4
    Join Date
    Oct 2013
    Posts
    16

    Re: Using a char as the control variable in a loop.

    Try this
    Code:
               do
               {
                   Console.Write("Enter a lower case letter: ");
                   c = Console.ReadLine().ToCharArray()[0];
                   if (Char.IsLower(c) && Char.IsLetter(c))
                       Console.WriteLine("Ok");
                   else if (c != CLOSE)
                       Console.WriteLine("Error");
               } while (c != CLOSE);

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using a char as the control variable in a loop.

    Reading up on this in msdn. Console.Read() doesn't return until the enter key is pressed (then the contents of the while loop are executed for each character in the read buffer).

    What you want is Console.ReadKey();

    Code:
    ConsoleKeyInfo cki;
    
    do
    {
      cki = Console.ReadKey(true);
      Console.WriteLine("{0}", cki.KeyChar);
    }
    while (cki.KeyChar != '!');
    If I type in "this is a test!", the following output is produced.

    Code:
    t
    h
    i
    s
    
    i
    s
    
    a
    
    t
    e
    s
    t
    !

  6. #6
    Join Date
    Oct 2013
    Posts
    7

    Re: Using a char as the control variable in a loop.

    Thanks again for the relies. I was able to get the program working with both the ways suggested.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Using a char as the control variable in a loop.

    Quote Originally Posted by Back Rain View Post
    Thanks again for the relies. I was able to get the program working with both the ways suggested.
    Notice the difference it both approaches. One approach requires that you press the enter key after each key is pressed, while the other approach doesn't. Experiment and understand both approaches (so you know which one to use later).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured