Calling a switch statement from a do-while loop.

I am not sure exactly where the problem is in this. I am creating a program to display the ascii key code and the morse code for any alphabetic character pressed and any number pressed 0 through 9. It needs to prompt the user they have not pressed a alphabet or number key and ask for another entry and if the '.' key is entered it is to end the program. Pretty simple but, I am new enough yet that I am having problems. Any help is greatly appreciated.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
					Program to provide the ascii code and morse code for an alphabetical character 
							and the base ten 0 through 9.
*/

#include "stdafx.h"
#include "string"

char morse(char);     //Morse function prototype.

int _tmain(int argc, _TCHAR* argv[])  //Main.
{
	char code;

	                //Do-while loop to get the character requested.
		do
		{
		printf("Please pick a letter from a to z or a number from 0 to 9 to get ASCII and"
			         " the Morris Code.  Press the '.' key to end.\n");
		scanf_s("%d", &code);
		code = morse(code);   //Morse function call.
		} while (code != '.');  //End of Loop
		
	return 0;
}

/***********************Function for the switch statement*****************************/
char morse(char code)
{
	switch (code)
	{
	case 'A': case 'a': printf("ASCII 65 upper case, 97 lower case, and Morse code is: *-.\n");
		return code;
		break;
	case 'B': case 'b': printf("ASCII 66 upper case, 98 lower case, and Morse code is: -***.\n");
		return code;
		break;
	case 'C': case 'c': printf("ASCII 67 upper case, 99 lower case, and Morse code is: -*-*.\n");
		return code;
		break;
	case 'D': case 'd': printf("ASCII 68 upper case, 100 lower case, and Morse code is: -**.\n");
		return code;
		break;
	case 'E': case 'e': printf("ASCII 69 upper case, 101 lower case, and Morse code is: *.\n");
		return code;
		break;
	case 'F': case 'f': printf("ASCII 70 upper case, 102 lower case, and Morse code is: **-*.\n");
		return code;
		break;
	case 'G': case 'g': printf("ASCII 71 upper case, 103 lower case, and Morse code is: --*.\n");
		return code;
		break;
	case 'H': case 'h': printf("ASCII 72 upper case, 104 lower case, and Morse code is: ****.\n");
		return code;
		break;
	case 'I': case 'i': printf("ASCII 73 upper case, 105 lower case, and Morse code is: **.\n");
		return code;
		break;
	case 'J': case 'j': printf("ASCII 74 upper case, 106 lower case, and Morse code is: *---.\n");
		return code;
		break;
	case 'K': case 'k': printf("ASCII 75 upper case, 107 lower case, and Morse code is: -*-.\n");
		return code;
		break;
	case 'L': case 'l': printf("ASCII 76 upper case, 108 lower case, and Morse code is: *-**.\n");
		return code;
		break;
	case 'M': case 'm': printf("ASCII 77 upper case, 109 lower case, and Morse code is: --.\n");
		return code;
		break;
	case 'N': case 'n': printf("ASCII 78 upper case, 110 lower case, and Morse code is: -*.\n");
		return code;
		break;
	case 'O': case 'o': printf("ASCII 79 upper case, 111 lower case, and Morse code is: ---.\n");
		return code;
		break;
	case 'P': case 'p': printf("ASCII 80 upper case, 112 lower case, and Morse code is: *--*.\n");
		return code;
		break;
	case 'Q': case 'q': printf("ASCII 81 upper case, 113 lower case, and Morse code is: --*-.\n");
		return code;
		break;
	case 'R': case 'r': printf("ASCII 82 upper case, 114 lower case, and Morse code is: *-*.\n");
		return code;
		break;
	case 'S': case 's': printf("ASCII 83 upper case, 115 lower case, and Morse code is: ***.\n");
		return code;
		break;
	case 'T': case 't': printf("ASCII 84 upper case, 116 lower case, and Morse code is: -.\n");
		return code;
		break;
	case 'U': case 'u': printf("ASCII 85 upper case, 117 lower case, and Morse code is: **-.\n");
		return code;
		break;
	case 'V': case 'v': printf("ASCII 86 upper case, 118 lower case, and Morse code is: ***-.\n");
		return code;
		break;
	case 'W': case 'w': printf("ASCII 87 upper case, 119 lower case, and Morse code is: *--.\n");
		return code;
		break;
	case 'X': case 'x': printf("ASCII 88 upper case, 120 lower case, and Morse code is: -**-.\n");
		return code;
		break;
	case 'Y': case 'y': printf("ASCII 89 upper case, 121 lower case, and Morse code is: -*--.\n");
		return code;
		break;
	case 'Z': case 'z': printf("ASCII 90 upper case, 122 lower case, and Morse code is: --**.\n");
		return code;
		break;
	case '0': printf("ASCII 30, and Morse code is: *-.\n");
		return code;
		break;
	case '1': printf("ASCII 31, and Morse code is: *-.\n");
		return code;
		break;
	case '2': printf("ASCII 32, and Morse code is: *-.\n");
		return code;
		break;
	case '3': printf("ASCII 33, and Morse code is: *-.\n");
		return code;
		break;
	case '4': printf("ASCII 34, and Morse code is: *-.\n");
		return code;
		break;
	case '5': printf("ASCII 35, and Morse code is: *-.\n");
		return code;
		break;
	case '6': printf("ASCII 36, and Morse code is: *-.\n");
		return code;
		break;
	case '7': printf("ASCII 37, and Morse code is: *-.\n");
		return code;
		break;
	case '8': printf("ASCII 38, and Morse code is: *-.\n");
		return code;
		break;
	case '9': printf("ASCII 39, and Morse code is: *-.\n");
		return code;
		break;
	case '.': code = '.';    //Case to end session.
		return code;
		break;
		                    //Default to let user know they need to choose the proper character.
	default: printf("Please choose a letter A through Z or a number 0 through 9.\n");
		return code;
		break;
	}
}
scanf_s("%d", &code);
%d is specifier for int, not char. You need %c for chars.
Also: do not use C input facilities for C++; you do not need string header here and if you insist on using C input, include proper header: stdio.h / cstdio
Ok, Thank you. I will confess that the first 8 weeks of this class we are doing C and then switching to C++ but, we are still using the same compiler. Thank you for your assistance.

That definitely helped because now I am not continuously looping. Now, it is just not going to the function and printing out the right case statement and re-asking for a new input.

Is there another site I should go to for just C?
Last edited on
Topic archived. No new replies allowed.