Homework - Number to letter assignment (ABCD -> 0 1 2 3)

Beginner Question:

As part of a basic encoding assignment I need to write a program that accepts a single word (capital letters) and outputs its assigned number (A=0 , B=1...Z=25) with spaces in between. I've written this but don't understand why it's not working? Thanks in advance for the help, really appreciate it.
.
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
#include <iostream>
#include <string>

using namespace std;


int main(){
	string str;
	cin >> str;
	int counter = 0;

	 for (char &c: str){

		 if (c == 'A'){
			str[c] = 0;
		}
		if (c == 'B'){
			str[c] = 1;
		}
		if (c == 'C'){
			str[c] = 2;
		}
		if (c == 'D'){
			str[c] = 3;
		}
		if (c == 'E'){
			str[c] = 4;
		}
		if (c == 'F'){
			str[c] = 5;
		}
		if (c == 'G'){
			str[c] = 6;
		}
		if (c == 'H'){
			str[c] = 7;
		}
		if (c == 'I'){
			str[c] = 8;
		}
		if (c == 'J'){
			str[c] = 9;
		}
		if (c == 'K'){
			str[c] = 10;
		}
		if (c == 'L'){
			str[c] = 11;
		}
		if (c == 'M'){
			str[c] = 12;
		}
		if (c == 'N'){
			str[c] = 13;
		}
		if (c == 'O'){
			str[c] = 14;
		}
		if (c == 'P'){
			str[c] = 15;
		}
		if (c == 'Q'){
			str[c] = 16;
		}
		if (c == 'R'){
			str[c] = 17;
		}
		if (c == 'S'){
			str[c] = 18;
		}
		if (c == 'T'){
			str[c] = 19;
		}
		if (c == 'U'){
			str[c] = 20;
		}
		if (c == 'V'){
			str[c] = 21;
		}
		if (c == 'W'){
			str[c] = 22;
		}
		if (c == 'X'){
			str[c] = 23;
		}
		if (c == 'Y'){
			str[c] = 24;
		}
		if (c == 'Z'){
			str[c] = 25;
		}
	}
	

	cout << str << endl;

	return 0;
}
Last edited on
too much work you are doing.

char ch = 'Z';

int numfromletter = ch - 'A'; //'Z' - 'A' = 25

but you either need an ugly or an int array to do the job.
char 0 may break the string class, not sure but it PROBABLY uses C-strings under the hood. Cstrings are really picky about char(0).

so what I would do is this...
vector<int> xref(str.length()); //is it length, or size? anyway, go with the concept here
for(blah)
{
xref[i] = str[i]-'A';
}

...
for(blah)
cout << xref[i] << " ";

it MAY be possible to put the values back in str and do a loop print:

for...
str[i] = str[i] - 'A';

and then

for(...
cout << (int)(str[i]) << " ";

you can try it, but it may not like it since 0-32 are 'unprintable' characters and strings are a bit grumpy about being used to store 'data'. The one thing you CANNOT do is print the string with a single cout statement; it does not work because it wants to print characters and you want to see the ascii value of the characters.


Last edited on
Thanks for answering.
We haven't covered vectors yet, I've figured it out (ironically just after I posted this)

[code]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


int main(){
string str;
cin >> str;
string output;

for (char c: str){

if (c == 'A'){
//str[c] = 0;
output = output + "0 ";
}
if (c == 'B'){
//str[c] = 1;
output = output + "1 ";
}
if (c == 'C'){
//str[c] = 2;
output = output + "2 ";
}
if (c == 'D'){
//str[c] = 3;
output = output + "3 ";
}
if (c == 'E'){
//str[c] = 4;
output = output + "4 ";
}
if (c == 'F'){
//str[c] = 5;
output = output + "5 ";
}
if (c == 'G'){
//str[c] = 6;
output = output + "6 ";
}
if (c == 'H'){
//str[c] = 7;
output = output + "7 ";
}
if (c == 'I'){
//str[c] = 8;
output = output + "8 ";
}
if (c == 'J'){
//str[c] = 9;
output = output + "9 ";
}
if (c == 'K'){
//str[c] = 10;
output = output + "10 ";
}
if (c == 'L'){
//str[c] = 11;
output = output + "11 ";
}
if (c == 'M'){
//str[c] = 12;
output = output + "12 ";
}
if (c == 'N'){
//str[c] = 13;
output = output + "13 ";
}
if (c == 'O'){
//str[c] = 14;
output = output + "14 ";
}
if (c == 'P'){
//str[c] = 15;
output = output + "15 ";
}
if (c == 'Q'){
//str[c] = 16;
output = output + "16 ";
}
if (c == 'R'){
//str[c] = 17;
output = output + "17 ";
}
if (c == 'S'){
//str[c] = 18;
output = output + "18 ";
}
if (c == 'T'){
//str[c] = 19;
output = output + "19 ";
}
if (c == 'U'){
//str[c] = 20;
output = output + "20 ";
}
if (c == 'V'){
//str[c] = 21;
output = output + "21 ";
}
if (c == 'W'){
//str[c] = 22;
output = output + "22 ";
}
if (c == 'X'){
//str[c] = 23;
output = output + "23 ";
}
if (c == 'Y'){
//str[c] = 24;
output = output + "24 ";
}
if (c == 'Z'){
//str[c] = 25;
output = output + "25 ";
}
}


cout << output << endl;

return 0;
}

[code]
Nevertheless, @jcar, I should have a look at what @jonnin actually wrote. Your code is about 26 times longer than it needs to be.
you can still condense it.

to do it this way,

int value = str[i] - 'A';
and then you can convert value back to a string with

stringstream ss;
ss << value;
str += ss.str(); //I think += is string concatenate? I don't do a lot of text processing.
str += " ";
a little fooling with that should net you a 5 line solution.

Don't be afraid to do something you have not seen in class yet. Play with it, learn it, get ahead of the class. If all you learn is what you see in class, you won't be prepared for the workforce, so get used to google and learning stuff on your own. I have never had a professor complain that I did something before it was covered. I had a few bad ideas that were rightfully smacked down, but never syntax or getting ahead of the game.



Last edited on
Topic archived. No new replies allowed.