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.
.
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.
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.