• 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.