Sum cicle

Hi :).
I´m trying to make a cycle that sum each int of a letter within a phrase.

I have eliminated the spaces out of the phrase, but I don´t know how to make the cycle that sum each int of letter within a phrase.

Could you please help me?

Row 51 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string.h>
using namespace std;





int main(){
    char palabra[250];

    cin.getline(palabra, 250);
    cout<<endl;


    int y = strlen(palabra);
    cout<<endl;
    cout<<"Tamano total de la frase -> "<<y<<endl;
    cout<<"*********************************"<<endl;



    int i = 0;
    int w = 0;
    while(i < y){
            if(int(palabra[i]==32)){
               w++;}
            i++;
    }
     cout<<"El numero de espacios dentro de la frase es: "<<w<<endl;
     cout<<"El tamaño del nuevo arreglo es: "<<y-w<<endl;
     cout<<"*********************************"<<endl;
     cout<<endl;
     cout<<endl;

     int z = y-w;
     cout<<"z = "<<z<<endl;
     char palabra2[z];

     i = 0;
     int j = 0;
    for (int i = 0, j = 0; i<y; i++,j++){
        if (palabra[i]!=' ')
            palabra2[j] = palabra[i];
        else
            j--;
    }

     cout<<"La nueva palabra sin espacios es: "<<palabra2<<endl;

    int sum;

   for (int i = 0; i <= z; i++){

    sum = int(palabra2[i]);
    }


cout<<"El peso de la palabra es: "<<sum<<endl;
}

sum is supposed to be:  int(palabra2[0])+int(palabra2[1]).....
Last edited on
You need to initialize sum with 0 on line 51 int sum = 0;
Line 55 should add the current sum with palabra2[i] sum += int(palabra2[i]);
Thanks kevinkjt2000 for your reply :), that worked well.

Now, I can´t sum all those values into a one single variable. Any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
    int sum = 0;

   for (int i = 0; i <= z; i++){

    if(palabra2[i] >= 97 && palabra2[i] <= 122){
          sum = int(palabra2[i]);
    }


cout<<"El peso de la palabra es: "<<sum<<endl;
}
}


I mean sum = sum of all int of each letter
Do you want 29 sums for each lowercase letter of the Spanish alphabet? Will you also want uppercase letters?
Last edited on
I only want the sum of the all lowercase letters of the palabra2. For example:

palabra2 = abcd

So I get:
sum[0] = 97
sum[1] = 98
sum[2] = 99
sum[3]= 100

But I´m trying to get a kind of "sumtotal" = sum[0]+sum[1]+sum[2]+sum[3]...

Do you understand me xD?

I initialized a new variable and tried to sum there, but I can´t get it :(
Last edited on
palabra2[0]+palabra2[1]+palabra2[2]+palabra2[3]+...
h+e+l+l+o...
104+101+108+108+111...
If that is what you want, then do what I suggested earlier to line 55 or line 6 on your second post.
Last edited on
I´m sorry, I didn't see the "+=" that you suggested me. Now, the program is working well.

Thanks kevinkjt2000, you saved me :P
Topic archived. No new replies allowed.