Dumb Question

I'm an extreme newbie, but im trying to make this app thats like a multiplication table. user has to input a letter then a number to get output which might be two numbers or a number with letter. i'm certain i'll be dealing with arrays and maybe pointers?
basically I want to make this into an app that way i can input D and 7 to get 6E as my output. i want the most simplified coding (not to be confused with optimised) dont want small, short coding but the most basics so as i can understand this better..
   0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
A  6F  6D  65  20  6F  66  20  74  68  65  0A  66  61  63  69  6C 

B  69  74  69  65  73  20  61  76  61  69  6C  61  62  6C  65  20
                                  
C  6F  6E  20  79  6F  75  72  20  73  63  72  65  65  6E  2E  0A                                      
 
D  43  6C  69  63  68  20  6F  6E  20  74  68  65  20  66  6C  61                                      
 
E  73  68  69  6E  67  20  48  41  52  44  57  41  52  45  20  62                                     
  
F  75  74  74  6F  6E  2E  0A  00  46  69  72  73  74  20  74  68                                   
 
G  69  6E  67  73  20  66  69  72  73  74  20  2D  20  79  6F  75                                      
 
H  20  63  61  6E  20  6D  6F  76  65  20  74  68  69  73  20  74                                      
  
I  75  74  6F  72  69  61  6C  0A  6F  6E  20  74  68  65  20  73                                     
  
J  63  72  65  65  6E  20  62  79  20  6C  65  66  74  2D  63  6C                                     
 
K  69  63  6B  69  6E  67  20  6F  6E  20  74  68  65  20  74  69                                     
 
L  74  6C  65  20  62  61  72  2E  0A  52  69  67  68  74  20  63                                      
  
M  6C  69  63  6B  20  74  6F  20  70  75  74  20  69  74  20  64                                    
  
N  6F  77  6E  20  61  67  61  69  6E  2E  00  00  57  65  6C  63                                      
  
O  6F  6D  65  20  74  6F  20  74  68  65  20  55  70  6C  69  6E  
                                   
P  6B  20  54  75  74  6F  72  69  61  6C  2E  0A  54  68  69  73 
 
Q  20  70  72  6F  67  72  61  6D  20  73  68  6F  75  6C  64  20 
 
R  68  65  6C  70  20  79  6F  75  20  74  6F  20  67  65  74  20 
  
S  73  74  61  72  74  65  64  2E  0A  43  6C  69  63  6B  20  6E 
 
T  65  78  74  20  74  6F  20  63  6F  6E  74  69  6E  75  65  2E 

U  00  00  00  00  50  E5  48  00  80  F8  4C  00  A0  F8  4C  00 
    
V  B0  F8  4C  00  C0  F8  4C  00  D0  F8  4C  00  E0  F8  4C  00                                    
           
W  00  E6  48  00  30  E6  48  00  57  41  52  4E  49  4E  47  3A                            
         
X  20  55  70  6C  69  6E  6B  54  61  73  6B  3A  3A  53  65  74 
           
Y  54  61  72  67  65  74  20  63  61  6C  6C  65  64  20  28  62                                  
           
Z  61  73  65  20  63  6C  61  73  73  29  0A  00  E0  E6  48  C0     

Sounds like all your need to do is multiply a number by the letter's "value" and then convert it to whatever base you need to output.
Not sure that'll get me what i need. the above is a keycard for a game i own which i must refer to everytime i start a new game. I'm trying to figure out how to input a letter as the row and a number as the column to get the necessary "key".


The simplest way I can think of would be to convert your data to an array (as you suspected)

1
2
3
4
5
6
7
8
const int data[26][16] = {
/*        0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15 */
/* A  */ {6F, 6D, 65, 20, 6F, 66, 20, 74, 68, 65, 0A, 66, 61, 63, 69, 6C}, 
/* B  */ {69, 74, 69, 65, 73, 20, 61, 76, 61, 69, 6C, 61, 62, 6C, 65, 20},
                                  
// SNIP                               
           
/* Z  */ {61, 73, 65, 20, 63, 6C,  61, 73, 73, 29, 0A, 00, E0, E6, 48  C0} };


Then, given a letter and a number, you can access the element using data[row][col], exploting the fact that (char) ch - 'a' = 0 - 25 for 'a' - 'z' (and similarly for the capital letters)

P.S. I was too lazy to add the necessary 0x- prefixes to all the numbers, so this won't compile "out of the box"...
Last edited on
Here's my test code so far, and as you can tell its not good. but it gives me what i need. Now is there a way to optimize the "if()" functions to smaller code that initializes the input a - z
to 0 - 25?

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
#include <iostream>
using namespace std;

char *key_card[5][16] ={
{ "6F", "6D",  "65",  "20",  "6F",  "66",  "20",  "74",  "68",  "65",  "0A",  "66",  "61",  "63",  "69", "6C",},  
 {"69",  "74",  "69",  "65",  "73",  "20", "61",  "76",  "61",  "69",  "6C",  "61",  "62",  "6C",  "65",  "20",},
 {"6F",  "6E",  "20",  "79",  "6F",  "75",  "72",  "20",  "73",  "63",  "72",  "65",  "65",  "6E",  "2E",  "0A",},
 {"43",  "6C",  "69",  "63",  "68",  "20",  "6F",  "6E",  "20",  "74",  "68",  "65",  "20",  "66",  "6C",  "61",},
{"73",  "68",  "69",  "6E",  "67",  "20",  "48",  "41",  "52",  "44",  "57",  "41",  "52",  "45",  "20",  "62",} }; 
                                     
int main() {
	char let;
	int num;
	
	cout << "enter letter: " <<endl;
	cin >> let;
	if('a')
		let = 0;
	if('b')
		let = 1;
	if('c')
		let = 2;
	if('d')
		let = 3;
	if('e')
		let = 4;
	cout << "enter num: " <<endl;
	cin >> num;
	num = num -1;

	cout << key_card[let][num] << endl;

return 0;
}
closed account (DSLq5Di1)
What andy was trying to point out is, that you can subtract 'a' (or 'A' in the case of capitals) from your input to get your row index.

1
2
3
4
5
	if('a')
		let = 0;
        ...

	cout << key_card[let-'a'][num] << endl;

It would be prudent to check your input is actually a letter with isalpha(), and perhaps use tolower() to avoid problems with capital letters. #include <cctype> for both these functions.
Wow. That seems to have worked.. What can I read to understand why subtracting 'a' makes it work? Thanks guys, This forum is way cool. got answers in less than a day. and reaffirmed that even though I'm still crawling, i'm heading in the right direction..


Edit:nevermind. just did the math after viewing a ANSCII codes table.
Last edited on
The ascii code for the character 'a' = 97, 'b' = 98, 'c' = 99, ...

So subtracting 'c' - 'a' is the same as 99 - 97 = 2.

http://en.wikipedia.org/wiki/Ascii
Topic archived. No new replies allowed.