C# GetVowels program

program used to enter a vowel and get a response back

Page 1 of 1

8 Replies - 5415 Views - Last Post: 21 October 2009 - 07:14 AM Rate Topic: -----

#1 lzoulek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

C# GetVowels program

Post icon  Posted 20 October 2009 - 06:33 PM

Ok, here I am again asking for help. three times in one day. I am really having a problem with these loops. this time i have a program that i am to enter a letter and if the letter is a vowel it returns "OK" if it is not a vowel, it returns back The letter is not a vowel. I am having problems getting it to loop and have spent days trying to figure it out. My book is not very clear on how to do this so here is what i have and anyone who can help is more than welcome to.

using System;
public class GetVowel
{
	public static void Main()
	{
		char exit;
		char vowel;
		string inputString;
		const double QUIT = 0;
		Console.WriteLine("Enter a vowel or type ! to exit:");
		inputString = Console.ReadLine();
		vowel = Convert.ToChar(inputString);
		{
			switch(vowel)
			{
				case 'a':
				case 'A':
					Console.WriteLine("OK");
					break;
				case 'e':
				case 'E':
					Console.WriteLine("OK");
					break;
				case 'i':
				case 'I':
					Console.WriteLine("OK");
					break;
				case 'o':
				case 'O':
					Console.WriteLine("OK");
					break;
				case 'u':
				case 'U':
					Console.WriteLine("OK");
					break;
				default:
					Console.WriteLine(" That letter is not a vowel");
				Console.ReadLine();
				break;
			}
			while (exit != QUIT)
			Console.ReadLine();
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: C# GetVowels program

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: C# GetVowels program

Posted 20 October 2009 - 06:42 PM

what is that "while" doing? are you trying to do a do/while loop?
Was This Post Helpful? 0
  • +
  • -

#3 lzoulek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: C# GetVowels program

Posted 20 October 2009 - 06:49 PM

I was trying to make it so that if you press ! you would exit out of the program and my text shows it being done with a while loop but I dont know if that is correct for this case.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: C# GetVowels program

Posted 20 October 2009 - 07:54 PM

here is how is one way of doing it...

static void Main()
{
    char vowel;

    do
    {
        Console.WriteLine("Enter a vowel or type ! to exit:");
        vowel = Convert.ToChar(Console.ReadLine());
        switch (vowel)
        {
            case 'a':
            case 'A':
                Console.WriteLine("OK");
                break;
            case 'e':
            case 'E':
                Console.WriteLine("OK");
                break;
            case 'i':
            case 'I':
                Console.WriteLine("OK");
                break;
            case 'o':
            case 'O':
                Console.WriteLine("OK");
                break;
            case 'u':
            case 'U':
                Console.WriteLine("OK");
                break;
            case '!':
                break;
            default:
                Console.WriteLine(" That letter is not a vowel");
                break;
        }
    } while (vowel != '!');

    Console.ReadLine();
}




and this is probably how I would have done it....without the switch(unless it was required, of course)..
static void Main()
{
    char vowel;
    char[] vowelArray = new char[] {'a','A','e','E','i','I','o','O','u','U' };

    do
    {
        Console.WriteLine("Enter a vowel or type ! to exit:");
        vowel = Convert.ToChar(Console.ReadLine());

        if (vowel == '!')
            break;

        if (vowelArray.Contains(vowel))
            Console.WriteLine("OK");
        else
            Console.WriteLine(" That letter is not a vowel");

    } while (vowel != '!');

    Console.ReadLine();
}


This post has been edited by eclipsed4utoo: 20 October 2009 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 lzoulek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: C# GetVowels program

Posted 21 October 2009 - 05:14 AM

I tried to change my code up the way you did the first one, but the ! still does not exit the program. That is the same problem that I was having. I cant use the second one because we have not done arrays yet so I dont know how to use them.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: C# GetVowels program

Posted 21 October 2009 - 05:24 AM

View Postlzoulek, on 21 Oct, 2009 - 08:14 AM, said:

I tried to change my code up the way you did the first one, but the ! still does not exit the program. That is the same problem that I was having. I cant use the second one because we have not done arrays yet so I dont know how to use them.


that's fine about the second snippet. However, the first code is tested and it works. Can you post the code that you have now after you made the changes?
Was This Post Helpful? 0
  • +
  • -

#7 lzoulek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: C# GetVowels program

Posted 21 October 2009 - 05:33 AM

Never mind, I got it to work now. I had that ReadLine at the end which was preventing it from exiting when i typed "!". I took out the ReadLine and the program works great now. Thank you for you help. I was getting so irritated with this program.

using System;
public class GetVowel
{
	public static void Main()
	{
		char vowel;
		do
		{
			Console.WriteLine("Enter a vowel or type ! to exit:");
			vowel = Convert.ToChar(Console.ReadLine());
			switch (vowel)
			{
				case 'a':
				case 'A':
					Console.WriteLine("OK");
					break;
				case 'e':
				case 'E':
					Console.WriteLine("OK");
					break;
				case 'i':
				case 'I':
					Console.WriteLine("OK");
					break;
				case 'o':
				case 'O':
					Console.WriteLine("OK");
					break;
				case 'u':
				case 'U':
					Console.WriteLine("OK");
					break;
				case '!':
					break;
				default:
					Console.WriteLine(" That letter is not a vowel");
					break;
			}
		} while (vowel != '!');

	}
}



Was This Post Helpful? 0
  • +
  • -

#8 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: C# GetVowels program

Posted 21 October 2009 - 07:06 AM

Just for your information since your in the process of learning the switch case.

There are some modifications that can be made to make your switch case a bit cleaner and still not use the arrays. I know they teach you to put a break between each case, but the break can be omitted in cases like this if the functionality is the same for the cases. Take a close look at my version and note that it runs exactly the same as your finished code. With alot less code :)
namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{

			char vowel;
			do
			{
				Console.WriteLine("Enter a vowel or type ! to exit:");
				vowel = Convert.ToChar(Console.ReadLine().ToUpper());
				switch (vowel)
				{
					case 'A':
					case 'E':  
					case 'I':   
					case 'O':
					case 'U':
						Console.WriteLine("OK");
						break;
					case '!':
						break;
					default:
						Console.WriteLine(" That letter is not a vowel");
						break;
				}
			} while (vowel != '!');

		}




	}
}



You'll get better at the loops over time.. they get used alot.

Aet
Was This Post Helpful? 0
  • +
  • -

#9 lzoulek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 19-October 08

Re: C# GetVowels program

Posted 21 October 2009 - 07:14 AM

wow, that IS a lot less code. Thank you, i will remember that for future reference. I am all about shorter and faster!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1