Thread: c# making a loop with a sentinel and show only the letters

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    c# making a loop with a sentinel and show only the letters

    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. Anybody know how i could make this work?? Ive search in many places and i can't find an answer.





    Code:
    using System;
    class text
    {
        static void Main()
        {
            Console.WriteLine("Enter text");
            string line = Console.In.ReadLine();
            int text = Convert.ToInt32(line);
            int sum = 0;
            int i = 0;
            while (text != -9)
            {
                sum = sum + text;
                i++;
                line = Console.In.ReadLine();
                text = Convert.ToInt32(line);
            }
            Console.Write("The letters are {0}", sum);
            Console.In.Read();
            return;
        }
    }
    This code will only give:
    Enter text
    9949449
    -9
    The letters are 9949449

    Is there a way i can make the sentinel finish the loop on the same line and use another character like a period. Also is there a way to make it read other values than numbers, the string would logicly work but the != doesn't accept it. And if i enter letters it crashes.

    345u435hu43i$$$@@@.
    The letters are uhui

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Have you ever dealt with regular expressions? Google it and you should be able to find a regular expressions that will allow you to extract text only and use the C# support for Regex....

    Regular Expressions Usage in C#
    Last edited by darren78; 12-24-2010 at 05:20 PM.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Not got my compiler ATM...

    You can use the C# Char structure methods.

    Code:
    Console.WriteLine("Enter text");
    string line = Console.In.ReadLine();
    int Length = line.Length;
    
    for(int i=0;i<Length;i++)//check each character
    {
          if(line[i] == '.') //look for a period to exit
              //exit
         else if(Char.IsDigit(line[i]))//is a number
             //process input
         else 
             //handle bad input   
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    Quote Originally Posted by novacain View Post
    Not got my compiler ATM...

    You can use the C# Char structure methods.

    Code:
    Console.WriteLine("Enter text");
    string line = Console.In.ReadLine();
    int Length = line.Length;
    
    for(int i=0;i<Length;i++)//check each character
    {
          if(line[i] == '.') //look for a period to exit
              //exit
         else if(Char.IsDigit(line[i]))//is a number
             //process input
         else 
             //handle bad input   
    }
    ha i haven't thought about that thank you.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    LINQ could be useful too:
    Code:
    foreach(char ch in line.Where((c) => Char.IsLetter(c)))
      Console.Write(ch);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sentinel controlled repetition example
    By droseman in forum C Programming
    Replies: 7
    Last Post: 09-26-2008, 02:17 AM