setting an int value to chars

closed account (2EyCpfjN)
I'm trying to create a function that will set a variable like so:
'a' would set the variable to 1.
'b' would set the variable to 2.
'c' would set the variable to 3.
I'm sure there's probably a correct term for this, share?
I temporarily named the variable 'sendBack'.


When I run this code, I always get the default in return.

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
#include "stdafx.h"
#include <iostream>
using namespace std;

void intro (void)
{
	cout << "Enter your message: " << endl;
} // asks for a user to enter a message
int messageConverter (char letter)
{
	int sendBack = -2;
	switch (letter)
	{
		case 'a':{
				sendBack = 1; break;} // a to 1 to identify
		case 'b':{
				sendBack = 2; break;} // b to 2 to identify
		case 'c':{
				sendBack = 3; break;} // c to 3 to identify
		case 'd':{
				sendBack = 4; break;} // d to 4 to identify
		case 'e':{
				sendBack = 5; break;} // e to 5 to identify
		case 'f':{
				sendBack = 6; break;} // f to 6 to identify
		case 'g':{
				sendBack = 7; break;} // g to 7 to identify
		case 'h':{
				sendBack = 8; break;} // h to 8 to identify
		case 'i':{
				sendBack = 9; break;} // i to 9 to identify
		case 'j':{
				sendBack = 10; break;} // j to 10 to identify
		case 'k':{
				sendBack = 11; break;} // k to 11 to identify
		case 'l':{
				sendBack = 12; break;} // l to 12 to identify
		case 'm':{
				sendBack = 13; break;} // m to 13 to identify
		case 'n':{
				sendBack = 14; break;} // n to 14 to identify
		case 'o':{
				sendBack = 15; break;} // o to 15 to identify
		case 'p':{
				sendBack = 16; break;} // p to 16 to identify
		case 'q':{
				sendBack = 17; break;} // q to 17 to identify
		case 'r':{
				sendBack = 18; break;} // r to 18 to identify
		case 's':{
				sendBack = 19; break;} // s to 19 to identify
		case 't':{
				sendBack = 20; break;} // t to 20 to identify
		case 'u':{
				sendBack = 21; break;} // u to 21 to identify
		case 'v':{
				sendBack = 22; break;} // v to 22 to identify
		case 'w':{
				sendBack = 23; break;} // w to 23 to identify
		case 'x':{
				sendBack = 24; break;} // x to 24 to identify
		case 'y':{
				sendBack = 25; break;} // y to 25 to identify
		case 'z':{
				sendBack = 26; break;} // z to 26 to identify
		default:{
				sendBack = -3; break;}
	}
	return sendBack;
}
int main()
{
	intro();
	int message;
	cin >> message;
	cout << messageConverter(message);
	return 0;
}
closed account (2EyCpfjN)
oh wow...
the int message;
should have been char message;
right?
Instead of setting setback to a number and breaking, you can just return the number directly.

Or better yet:
1
2
3
4
5
int messageConverter (char letter)
{
  if (letter>='a' && letter<='z')return letter-'a'+1;
  return -3;
}
Last edited on
1
2
3
	char c;
	cin >> c;
	cout << (int)c - 96;
Topic archived. No new replies allowed.