HEEEELP: Letters to ASCII decimal equivalents?

This is a discussion on HEEEELP: Letters to ASCII decimal equivalents? within the C++ Programming forums, part of the General Programming Boards category; Hi to everyone. I am a newbie in c++, I have an assesment for my software engineerin subject. We have ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Exclamation HEEEELP: Letters to ASCII decimal equivalents?

    Hi to everyone.

    I am a newbie in c++,

    I have an assesment for my software engineerin subject.
    We have to write a program in c++ where when you type your name it will show the ASCII decimal equivalents for each letter and then will add them.
    I have tried every possible loop but it seems that it is not working. I always get 204.
    Any suggestions?

    If you need to see the code tell me and I will post it here.

    See ya

    Mazerius

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    ok I will post it tomorrow. I am not in my house now.
    Hope that I can get some help

    Mazerius

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Exclamation

    Hi

    Sorry for not posting the code yesterday.
    Here it is

    code
    Code:
    int main()						
    
    {		
    cout << endl;
    
    	char Name[50];
    	
    	cout << "Please type in your name....:  ";
    	
    	cin.getline (Name, 50, '\n');				  		
    	
    	cout << endl;
    
    	cout << "You have typed.....:\t\a" << Name << endl;					
    												  
    	cout << endl;
    
    //********** Begining of the loop for the ASCII *********
    //********** decimal equivalents                *********
    
    	for (int i=0; i<256; i++)
    
    	{
    		if ((char)i==Name[50])
    	
    		{
    			cout << Name << "," << i << endl;
    		}
    
    	}
    
    	cout << endl << endl;
    
    	cout << " Press any key if you want to quit!" << endl;
    
    cin.ignore();
    
    return 0;
    
    }
    The problem is that i can not make the loop work so i can change the letters to ascii decimal equivalents.
    I know that that the loop is wrong but I can not find any solution.
    Any suggestions?

    See ya

    Mazerius

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    try typecasting

    Code:
    int a = 0;
    char b = 'b';
    a = int(b);
    It works the other way with char(a);

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Thanks

    I have to replace the if statement with the code you suggested?

    See ya

    Mazerius

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Hey Trauts

    I try your suggestion but know it gives me always
    1245004 for any name.

    What I am trying to do is the following:

    Let's say you type "Ben"

    The programme shows back "Ben"
    and then shows for each letter of the name the ascii number
    like this B, 66
    e, 98
    n, 119
    and the it gives the total for this numbers

    P.s. the numbers in the example are random I do not know the correct ascii values.

    Any suggestions?

    See ya

    Mazerius

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Posting code.... read this first....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    jadzia
    Guest
    Replace the for-loop with this

    Code:
    int sum=0;
    for (int i=0; i<strlen(Name); i++)
    {
      cout << Name[i] << ',' << (int)Name[i] << '\n';
      sum += Name[i];
    }
    cout << sum << '\n';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. decimal ascII character
    By byfreak in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2008, 11:36 PM
  3. decimal ascII character
    By byfreak in forum C Programming
    Replies: 2
    Last Post: 05-24-2008, 07:37 PM
  4. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 06:19 PM
  5. Replies: 11
    Last Post: 03-24-2006, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21