Public Group

# Help with Strings to numbers

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey every! Please help I need some source code :D I need a small program that allows the user to enter a name and then is displayed as a number depending on what certain letters equal. So if they user entered Jamie and i had set that letter J = 6 letter a = 3 letter m = 8 letter i = 4 letter e = 9 the code should be displayed as 63849 Please help! :D

##### Share on other sites
Well, I'm not going to write you an algorithm. What I would do is convert the string to either upper-case or lower-case - your choice. Then, I would loop through each character in the string and see how far it's ASCII value is from 'a' or 'A', depending on whether you converted to upper case or lower case. That will give you your values. For example, take the string "test".

't' - 'a' = 20
'e' - 'a' = 5
's' - 'a' = 19
't' - 'a' = 20

##### Share on other sites
What you are looking for is a simple lookup table. You just make an array that you store the character's value in the index of the character itself. Here's the idea, you can make the source code youself, since this seems kinda HWish...

1 - Create an integer array that is the size of the highest character you will 'look up'. So if you are using all characters in the ASCII table, then the array size is 256.

2 - Manually assign the values of each character into the array with the desired value. Use the character that you are looking up as the index for the array and the desired value you want that character to have as the value being assigned to that location.

3 - When you need to look up a character, simple output the contents of the table at the index of the character you are looking up.

You are all done!

##### Share on other sites
right..

this is my source so far hopfully its clear what i need to do and some one can help me finish it :D

#include <iostream.h>
#include <string.h>
int main() {

char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
f = 6;
g = 7;
h = 8;
i = 9;
j = 10;
k = 11;
l = 12;
m = 13;
n = 14;
o = 15;
p = 16;
q = 17;
r = 18;
s = 19;
t = 20;
u = 21;
v = 22;
w = 23;
x = 24;
y = 25;
z = 26;

char User[9];

cin >> User;

cout << "The length of your name is" <<strlen(User);

cout << "The code of your string is"

return 0;
}

##### Share on other sites
Is this a homework problem?

I ask because it is against the forum rules to answer homework questions (the entire point of the work is that you should figure out the answer yourself, not get someone else to do it.)

I apologise if I've misinterpreted this, but the problem does seem to fit the general pattern of homework questions, in that it's arbitrary and pointless except as a contrived example of having to map one thing to another.

John B

##### Share on other sites
lol no its not i promise u...

##### Share on other sites
don't put your codes into seperate variables, put them into a std::map

with lines like:

codes['a'] = 3;

etc ...

I think that should be enough to get you going the right direction

##### Share on other sites
no that doesnt really help...

##### Share on other sites
Nobody is to provide an exact solution for this problem, pending suspension.

laurence900, write out a procedure for solving the problem in plain English. Break it into simple steps, then convert each step into code. Go ahead and post your steps here, then we can critique them for you and help you refine them. At the end of the day you'll have a working algorithm, and will have improved your problem-solving skills significantly.

##### Share on other sites
I'm pretty sure you can cast chars to ints in C++.

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633420
• Total Posts
3011793
• ### Who's Online (See full list)

There are no registered users currently online

×