Thread: adding ASCII values

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    Question adding ASCII values

    how do you add ASCII values?

    what i mean by this is that you add the ASCII values of the characters entered.

    if the user enters "Hello", the ASCII values are "72, 101, 108, 108, 111".

    The function hash is supposed to add those values together. the total should be 500

    void hash ( char string[] )
    {
    int count ;

    for( count = 0 ; string[count] != 0 ; count++ )
    {
    printf( "\n##%c", string[count] ) ;

    }
    }

    this function is supposed to add the ascii values of the characters in string...
    Watshamacalit

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're almost there, look at this:
    Code:
    void hash(char mystring[])
    {
        int count, total;
    
        for (count = 0, total = 0; mystring[count] != 0; count++)
        {
            total += mystring[count];
            printf("\n##%c", mystring[count]);
        }
        
        printf ("total is %d\n", total);
    }
    Note that I changed the variable string to mystring, as string is reserved.

    [edit]
    Or a more compact version:
    Code:
    void hash(char *mystring)
    {
        int total = 0;
    
        while (*mystring)
            total += *mystring++;
        
        printf ("total is %d\n", total);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. adding ASCII
    By watshamacalit in forum C Programming
    Replies: 0
    Last Post: 12-26-2002, 05:25 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM