Problem To Sum All Ascii

This is a discussion on Problem To Sum All Ascii within the C++ Programming forums, part of the General Programming Boards category; hi everone, i have a problem to sum all the ascii that converted from the name. if possible can someone ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Problem To Sum All Ascii

    hi everone, i have a problem to sum all the ascii that converted from the name.

    if possible can someone show as funtion.

    can anyone please show me the step please

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    
    int main()
    {
        string name;
        int sum = 0;
        
        //prompt user input
        
        cout<<"Please enter your name to generate password: ";
        cin>>name;
        
        
        for(int i=0; name[i]; i++)
        {
            cout<<int (name[i])<<endl;
        }
       		
            
            
    system("pause");
    return 0;
    }
    appreciate alot..

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    for(int i=0, length=name.size(); i<length; i++)
    {
         //use the += operator to 'accumulate' the ascii value into an integer variable
    }
    
    cout << The total value of all ascii is:  "  <<   //[use accumulated int var here]  ; 
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    For string types, you have to include <string>.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    is it like this?

    Code:
        for(int i=0, length=name.size(); i<length; i++)
        {
        	sum += length;
        }
        	cout<<sum<<endl;
    for example input Peter and the output will be 512...
    but the code that you provided is only 25.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No, length in that example is the length of the string and is only used to help make the loop stop at the right time. You want to sum the value of each character in the string.

  6. #6
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    
    int main()
    {
        char name[255];
        int sum = 0;
        
        //prompt user input
        
        cout<<"Please enter your name to generate password: ";
        cin.getline(name,255,'\n');
        
        
        for(int i=0; i<name.length(); i++)
       {
                 sum += (int)name[i] ;  //this should do the trick
         
       }
    
       		
            
            
    system("pause");
    return 0;
    }
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    wyvern, that code won't work (or compile). There is no need to switch to a C style string anyway.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    The STL accumulate function works nicely here. You should avoid calls to system if at all possible, a couple calls to cin.get() would work just as well in this instance.

    Code:
    #include <iostream>
    #include <numeric>
    #include <string>
    using namespace std;
    
    
    int main()
    {
        string name;
        int sum = 0;
        
        //prompt user input
        
        cout<<"Please enter your name to generate password: ";
        cin>>name;
        
        sum = accumulate(name.begin(),name.end(),0);
    
        cout << "Sum is: " << sum << endl;
            
        cin.get();
        cin.get();
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Code:
    accumulate()
    I love these magical magical functions that I've never heard of.

    Where can I get some details on these standard libraries like:
    <numeric>
    <algorithm>
    <ios>
    <limits>
    <so on... and so on>

    I could easily search for them one at a time, but that would require me to know of them first. So is there a site of libraries that explains what each of them are capable of?
    Sent from my iPadģ

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by SlyMaelstrom
    Code:
    accumulate()
    I love these magical magical functions that I've never heard of.

    Where can I get some details on these standard libraries like:
    <numeric>
    <algorithm>
    <ios>
    <limits>
    <so on... and so on>

    I could easily search for them one at a time, but that would require me to know of them first. So is there a site of libraries that explains what each of them are capable of?
    I learned much of what I know from reading "The C++ Standard Library: A Tutorial and Reference" by Nicolai Josuttis. It would be a good reference book for you to learn all about the capabilities of the functions hidden in the <algorithm> and <numeric> headers.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    hi everyone,
    as shown on the top that

    Code:
    sum = accumulate(name.begin(),name.end(),0);
    i dont understand the '0' at the back of the code. can someone tel me what it does?

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    as www.cppreference.com shows that accumulate is in #include <algorithm>
    and why hk_mp5kpdw used #include <numeric>?

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    sum = accumulate(name.begin(),name.end(),0);
    <clap, clap, clap> Very nice.

    So is there a site of libraries that explains what each of them are capable of?
    http://www.sgi.com/tech/stl/
    Check out the index by categories.

    i dont understand the '0' at the back of the code. can someone tel me what it does?
    It is the "initial value" you start adding on to. If the algorithm does this:

    initialValue = initialValue + element

    then initialValue has to be defined to start with.
    as www.cppreference.com shows that accumulate is in #include <algorithm>
    and why hk_mp5kpdw used #include <numeric>?
    I think it's an error. Josuttis says it's in the <numeric> header file, and if I use <algorithm> instead, I get a compiler error(VC++6).
    Last edited by 7stud; 12-02-2005 at 03:21 AM.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    yeah i got the error too, ok thanks, cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minimum Positive Subsequence Sum
    By CrazyNorman in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2008, 04:25 AM
  2. Sub-sum problem (NP)
    By stass in forum C Programming
    Replies: 2
    Last Post: 08-13-2008, 03:30 PM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. ASCII Exceptions problem
    By Bazza in forum C Programming
    Replies: 6
    Last Post: 07-10-2002, 08:58 AM

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