Thread: c# help needed with a mix of loop terminated sentinal

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

    c# help needed with a mix of loop terminated sentinal

    Code:
    using System;
    class stringtest2
    {
        static void Main()
        {
            Console.Write("Enter something ? ");
            String line = Console.In.ReadLine();
            String list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            String finalstring = "";
            int i = 0;
            while (line != ".")
            {
                foreach (char a in line)
                {
                    if (list.IndexOf(a) != -1)
                    {
                        finalstring += a.ToString();
                    }
                }
                line = ".";
                Console.WriteLine("\n{0}", finalstring);
                i++;
                line = Console.In.ReadLine();
            }
        }
    }



    Hi, thats the code, i won't lie, i was able to get it with the precious help from some generous members of the forums ive been in (including this one). I modified it a little bit. The code work well almost like i need.

    When you enter a bunch of characters, it only shows up the letters and if you add more letters, it add them to the screen. Exactly what i want. When you enter a period, it exit the loop. Thats the main goal of this code but what i'm trying to do is the period sentinal in the text you type. In other words, the loop would only exit if in the bunch of characters you enter, theres a period. In this code, it only exist the loop if you enter the perdiod alone.


    Right now, thats what it does:

    #@$#@a@#$23432b#$#@$@#c.

    abc
    .
    Press any key to continue . . .


    Thats what i'm trying to do:
    #@$#@a@#$23432b#$#@$@#c.

    abc
    Press any key to continue . . .


    Is it possible to make this. Ive spend hours on it but i cant find a solution.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    line is, as you may have guessed, the line. Your loop checks whether or not the line (not any character or anything, the line) is a period. If you want to be checking individual characters, then you need to do that when you go through all the characters (i.e. your foreach loop).

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Opps, misread what you wanted.

    Tabstop is right.
    Last edited by novacain; 01-09-2011 at 12:34 AM. Reason: Misread the problem
    "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
    I have no clue how to do this lol. The foreach loop seem to only accept a char that is declared and a letter. But thanks anyway.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by xbox221 View Post
    I have no clue how to do this lol. The foreach loop seem to only accept a char that is declared and a letter.
    Well, yes, because that's the way you wrote it. It goes over every character in the line regardless of what it is, and does something to it -- specifically, checks whether or not it is in list (i.e. that big string of letters at the top). Since that's the way you wrote it, that means you can make it do something else. So you can add a check for whether a == '.', and if so what to do about it.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why not just:
    Code:
    while(line.IndexOf('.') == -1)
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    Quote Originally Posted by itsme86 View Post
    Why not just:
    Code:
    while(line.IndexOf('.') == -1)
    thanks lol i couldn't figure a way to terminate the while loop properly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  2. write a loop to a file, getopt()
    By kristy in forum C Programming
    Replies: 3
    Last Post: 08-15-2003, 03:57 PM
  3. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM