C++ alphabet animals failure, switch, case, break

Apr 1, 2011 at 2:38am
I use Dev-CPP aka bloodshed

What I have to do to get this C++ right? I also did all the letters.

Have the user enter a letter for a minimum of 10 letters, send the letter
as a parameter to a function, and in the function, print out an
animal that begins with that letter. Letters can be entered in upper or
lower case. Keep asking the user to enter a letter until they enter x, then
tell them there is no animal that begins with x, and end the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdlib>
#include <iostream>
using namespace std;
void fun1(char X);
int main(int argc, char *argv[])
{
	char x, X;
	cout<<"enter a letter"<<endl;
	cin>> x;
	while (x!='x' && x!='X')
	(
	fun1(X)
	);
	if (x=='x' || x=='X')
	cout<<"There is no animal for X"<<endl;
	system("PAUSE");
	return EXIT_SUCCESS;
}
void fun1(char X)
{
 	switch (X)
 	{
 	case 'a' : case 'A':
      	cout<<"alligator";
      	break;
 	case 'b' : case 'B':
      	cout<<"Boobie";
      	break;
 	case 'c' : case 'C':
      	cout<<"Cat";
      	break;
 	case 'd' : case 'D':
      	cout<<"Dog";
      	break;
 	case 'e' : case 'E':
      	cout<<"Elephant";
      	break;
 	case 'f' : case 'F':
      	cout<<"Fox";
      	break;
 	case 'g' : case 'G':
      	cout<<"Giraffe";
      	break;
 	case 'h' : case 'H':
      	cout<<"Hawk";
      	break;
 	case 'i' : case 'I':
      	cout<<"Iguana";
      	break;
 	case 'j' : case 'J':
      	cout<<"Jaguar";
      	break;
 	case 'k' : case 'K':
      	cout<<"Kangaroo";
      	break;
 	case 'l' : case 'L':
      	cout<<"Leopard";
      	break;
 	case 'm' : case 'M':
      	cout<<"Mamba";
      	break;
 	case 'n' : case 'N':
      	cout<<"Nautilus";
      	break;
 	case 'o' : case 'O':
      	cout<<"Octupus";
      	break;
 	case 'p' : case 'P':
      	cout<<"Panther";
      	break;
 	case 'q' : case 'Q':
      	cout<<"Quale";
      	break;
 	case 'r' : case 'R':
      	cout<<"Rhino";
      	break;
 	case 's' : case 'S':
      	cout<<"sheepie";
      	break;
 	case 't' : case 'T':
      	cout<<"Tarantula";
      	break;
 	case 'u' : case 'U':
      	cout<<"Unagi";
      	break;
 	case 'v' : case 'V':
      	cout<<"Vampire Bat";
      	break;
 	case 'w' : case 'W':
      	cout<<"walrus";
      	break;
 	case 'y' : case 'Y':
      	cout<<"Yak";
      	break;
 	case 'z' : case 'Z':
      	cout<<"Zebra";
      	break;
       }
}


What it does right now is when I insert a letter it does an infinite loop of fox. Or if I enter the lowercase of x it doesn't end.
Last edited on Apr 1, 2011 at 2:39am
Apr 1, 2011 at 2:43am
You're using some mysterious, uninitialized variable X.
Apr 1, 2011 at 2:44am
You need to put lines 8-9 in the loop (and use a do-while loop, or asign a dummy value to x):
1
2
3
4
5
6
7
8
char x = 'a'; //you don't need big X in this scope (only in the fun1 function)
while (x!='x' && x!='X')
(
//ask the user for input over and over
cout<<"enter a letter"<<endl;
cin>> x;
fun1(x) //and here you need to pass little x as the argument (not big X)
);


Please let me know if you don't understand something...
Last edited on Apr 1, 2011 at 2:47am
Apr 1, 2011 at 3:21am
Thanx Math.
of course we both had our little errors in our code there. Looks like it should be this instead of what u put in. Works perfectly though, thanx again..
1
2
3
4
5
6
7
        char x= 'a';
	while (x!='x' && x!='X')
	{
	cout<<"enter a letter"<<endl;
	cin>> x;
	fun1(x);
	}
Last edited on Apr 1, 2011 at 3:22am
Apr 1, 2011 at 4:07am
Lol, I copied your code and forgot to add the semicolon on line 7... :)
Your welcome.
Last edited on Apr 1, 2011 at 4:08am
Topic archived. No new replies allowed.