Hi, this i'm trying to make a loop that would read a list of character (including numbers) that you type in terminated by a period and that would only write the letters and ignore the other characters. How would i do this?? Thats the code ive been doing but there seem to be a problem with != operator saying it can't work with a string or int.

For example: 345u435hu43i$$$@@@.
would only show: uhui

using System;
class text
{
static void Main()
{
Console.WriteLine("Enter text");
string line = Console.In.ReadLine();
string text = Convert.ToString(line);
int i = 0;
while (text != .)
{
line = Console.In.ReadLine();
text = Convert.ToString(line);
}
Console.WriteLine("The letters are {0}", text);
Console.In.Read();
return;
}
}

Recommended Answers

All 4 Replies

What is line 8 for? Since the variable line is a string (you define it as such in line 7), why are you converting it to a string?

Line 10 you need put quotes around what you are comparing.

using System;
using System.Text;

namespace Text {
    class Program {
        static void Main() {
            StringBuilder sb = new StringBuilder();
            Console.WriteLine("Enter your text:");
            String input = Console.ReadLine();

            foreach (Char c in input) {
                if (Char.IsLetter(c)) {
                    sb.Append(c);
                }
            }

            Console.WriteLine("The letters are {0}", sb.ToString());

            Console.ReadLine();
        }
    }
}

Line 11 loops through each character in the input string. Line 12 checks if the current character is a letter. Line 13 adds it to the output string if it is (we use StringBuilder since we need a mutable string).

Thanks.

1.
      using System;
   2.
      class text
   3.
      {
   4.
      static void Main()
   5.
      {
   6.
      Console.WriteLine("Enter text");
   7.
      string line = Console.In.ReadLine();
   8.
      string text = Convert.ToString(line);
   9.
      int i = 0;
  10.
      while (text != .)
  11.
      {
  12.
      line = Console.In.ReadLine();
  13.
      text = Convert.ToString(line);
  14.
      }
  15.
      Console.WriteLine("The letters are {0}", text);
  16.
      Console.In.Read();
  17.
      return;
  18.
      }
  19.
      }

While Momerath's post is fine, ONE of the issues with your post is how you are reading the string variable text on line # 8.

If you are going to compare the string to a period, you have to enclose it in single quotes.

while(text != '.')
{
....
}

k thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.