• 0

switch, loop, and alphabet


Question

Alright, I was able to get the switch to work until I started my conditionals.  What I am trying to do is create an alphabet tutor (Halloween style) program.  ex) A is for Apparition, B is for Brains....etc.  If it is not one of the designated characters then it prints out "That is not a letter, please try again!".  If it is a 1, it exits the program.  This is what I have

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main()
{
	char ch;

	printf("Please enter a letter:\n");
	printf("Please enter 1 to exit:\n");
	ch = getch(); //gets the character from the user

	switch(ch) //starts the switch
	{
	case 'a': 
	case 'A':
		printf("A is for Apparition\n");
		break;
	case 'b':
	case 'B':
		printf("B is for Brains\n");
		break;
	case 'c':
	case 'C':
		printf("C is for Canabal\n");
		break;
	case 'd':
	case 'D':
		printf("D is for Dracula\n");
		break;
	case 'e':
	case 'E': 
		printf("E is for Elf\n");
		break;
	case 'f':
	case 'F':
		printf("F is for Frankenstein\n");
		break;
	case 'g':
	case 'G':
		printf("G is for Grotesque\n");
		break;
	case 'h':
	case 'H':
		printf("H is for Hobgoblin\n");
		break;
	case 'i':
	case 'I':
		printf("I is for Incantation\n");
		break;
	case 'j':
	case 'J':
		printf("J is for Jinx\n");
		break;
	case 'k':
	case 'K':
		printf("K is for Knife\n");
		break;
	case 'l':
	case 'L':
		printf("L is for Lycanthrope\n");
		break;
	case 'm':
	case 'M':
		printf("M is for Mainiac\n");
		break;
	case 'n':
	case 'N':
		printf("N is for Ninja\n");
		break;
	case 'o':
	case 'O':
		printf("O is for Occult\n");
		break;
	case 'p':
	case 'P':
		printf("P is for Psycho\n");
		break;
	case 'q':
	case 'Q':
		printf("Q is for Quake\n");
		break;
	case 'r':
	case 'R':
		printf("R is for Revolting\n");
		break;
	case 's':
	case 'S':
		printf("S is for Stitches\n");
		break;
	case 't':
	case 'T':
		printf("T is for Tarantula\n");
		break;
	case 'u':
	case 'U':
		printf("U is for Uneasy\n");
		break;
	case 'v':
	case 'V': 
		printf("V is for Voodoo\n");
		break;
	case 'w':
	case 'W':
		printf("W is for Wizzard\n");
		break;
	case 'x':
	case 'X':
		printf("X is for Xenophobia\n");
		break;
	case 'y':
	case 'Y':
		printf("Y is for Yuck\n");
		break;
	case 'z':
	case 'Z':
		printf("Z is for Zombie\n");
		break;
	default: //meassage that is prompted if one of the characters is not a letter
		printf("That is not a letter, please try again!");
		}
		
		{
			while(ch == 1);
			system("exit"); //if the character is 1 program is terminated
	}
		if (ch!=1; ch != )
				{
					continue;
			}
	
				
}

The problem I am having is

IF user inputs a 1 then exit program

IF user inputs an acceptable letter the continue program

If user inputs an invalid character then prompt user to try again and continue.....

 

I'm not quite sure how to do this.  Am I on the right track?

Link to comment
https://www.neowin.net/forum/topic/1179315-switch-loop-and-alphabet/
Share on other sites

6 answers to this question

Recommended Posts

  • 0
while (1)
{
    if (ch == '1')
    {
        //Exit
    }
    else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        //Put switch here
    }
    else
    {
        //Invalid
    }
}
  • 0
  On 27/09/2013 at 20:01, Phouchg said:

 

Perhaps...

do {
  switch (ch) {
    // '1' - system(exit);
    // all letters
    // default - invalid
  }
} while true;

That's likely the approach I would have taken. 

while (ch != '1')
{


}

printf("The user has exited the program.");
return 0;
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I’m so glad to see the direction this political image thread has gone. 
    • Explorer patcher is a free alternative that also works great. I get that you use startisback it’s more for others that see this and are curious.
    • PeaZip 10.5.0 by Razvan Serea PeaZip is an open source file and archive manager. It's freeware and free of charge for any use. PeaZip can extract most of archive formats both from Windows and Unix worlds, ranging from mainstream 7Z, RAR, TAR and ZIP to experimental ones like PAQ/LPAQ family, currently the most powerful compressor available. Open and extract 200+ archive formats: 001, 7Z, ACE(*), ARC, ARJ, BZ2, CAB, DMG, GZ, ISO, LHA, PAQ, PEA, RAR, TAR, UDF, WIM, XZ, ZIP ZIPX - view full list of supported archive file formats for archiving and for extraction. PeaZip provides fast, high compression ratio multi-format archiving - view file compression and decompression benchmarks for more information. PeaZip is localized in 29 languages and is capable of handling all most popular archive formats (180+ file types), supporting a wide array of advanced file and archive management features (search, bookmarks, thumbnail viewer, find duplicate files and compute hash/checksum value, convert archive files...), especially focused on security (strong encryption, two factor authentication, encrypted password manager, secure file deletion...). PeaZip 10.5.0 release notes: PeaZip 10.5.0 brings a major update in the file manager, boosting archive browsing performances, improving archive editing, adding new functions to open file as archive from within archives, to prevent running a custom list of file extensions without confirmation, and to hide or show hidden files. On macOS all file browser styles and icon sizes are now available as on other platforms. On Windows it is now possible to explore NTFS Alternate Data Streams to gather additional information on files, including a dedicated tools to batch scan the input for Zone.Identifier (Mark of The Web) to detect files coming from remote systems. In addition, the option to include the Zone.Identifier metadata in archives is now enabled by default for all formats supporting it (RAR, WIM). Backend were updated to Pea 1.25. Full list of changes and fixes is available at https://peazip.github.io/changelog.html Download: PeaZip 64-bit | PeaZip Portable 64-bit | ~13.0 MB (Open Source) Download: PeaZip 32-bit | PeaZip Portable 32-bit View: PeaZip Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I’m still waiting for “small taskbar icons” don’t need a huge ass task bar. Been using explorer patcher for years simply for this functionality. Cmon ms, not everyone uses touch screens.
  • Recent Achievements

    • One Month Later
      jezzzy earned a badge
      One Month Later
    • First Post
      CSpera earned a badge
      First Post
    • One Month Later
      MIR JOHNNY BLAZE earned a badge
      One Month Later
    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      624
    2. 2
      ATLien_0
      280
    3. 3
      +FloatingFatMan
      179
    4. 4
      Michael Scrip
      149
    5. 5
      Steven P.
      118
  • Tell a friend

    Love Neowin? Tell a friend!