# Problem To Sum All Ascii

• 12-01-2005
gtr_s15
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.

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..
• 12-01-2005
The Brain
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]  ; ```
• 12-01-2005
7stud
For string types, you have to include <string>.
• 12-01-2005
gtr_s15
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.
• 12-01-2005
Daved
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.
• 12-01-2005
wyvern
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; }```
• 12-01-2005
Daved
wyvern, that code won't work (or compile). There is no need to switch to a C style string anyway.
• 12-01-2005
hk_mp5kpdw
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; }```
• 12-01-2005
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?
• 12-01-2005
hk_mp5kpdw
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.
• 12-02-2005
gtr_s15
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-02-2005
gtr_s15
as www.cppreference.com shows that accumulate is in #include <algorithm>
and why hk_mp5kpdw used #include <numeric>?
• 12-02-2005
7stud
Quote:

sum = accumulate(name.begin(),name.end(),0);
<clap, clap, clap> Very nice.

Quote:

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.

Quote:

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