...

View Full Version : C++ Letters to Number



UltimateCoder
01-22-2012, 03:08 PM
I have this so far:


cin>>letter;
if(letter=="a") number=1;
if(letter=="b") number=2;
....
if(letter=="z") number=26;


any possible way to shorten this?

Apothem
01-22-2012, 03:20 PM
Make letter into a char (or just do *letter).
Go here: http://www.asciitable.com/
Subtract the ascii # with the input number. No conditionals really needed.

UltimateCoder
01-22-2012, 03:24 PM
Thanks I doubt you can do this but in C++ does a+1=b ?

Apothem
01-22-2012, 04:17 PM
Yes. You can also do that in C++. But do note it has to be a char, not a string/char*.

UltimateCoder
01-22-2012, 04:20 PM
This solves the problem thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum