Unintentionally, each score does not come out, but the combined score comes out.

What I wanted to do was take 10 arrays of structures and get the number of each array of structures.
For example, entering love would get 12 + 15 + 22 + 5 = 54 score!
I wrote three sentences, but each score does not come out, but the combined score comes out.
I tried to get each score, but I don't know what to do here.
Help me Plz!!

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
   #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    struct data
    {
       char names[20]; 
       int score; 
    };

    int main()
    {
       int z[10];
       struct data a[] = { {"Hardwork"},{"Knowledge"} ,{"Love"} ,{"Luck"} ,{"Money"} ,{"Leadership"} , {"attitude"} ,{""} ,{""} ,{""} };
    
       scanf("%s", a[7].names);
       scanf("%s", a[8].names);
       scanf("%s", a[9].names);
    
       int sum = 0;
    
       {
       for (int j = 0; j <= 9; j++)
    
          for (int i = 0; i <= 19; i++)
          {
             char t[20];
             t[i] =a[j].names[i];
             if ('a' <= t[i] && t[i] <= 'z')
             {
                t[i] = t[i] - 96;
             }
             else if ('A' <= t[i] && t[i] <= 'Z')
             {
                t[i] = t[i] - 64;
             }
             else if (t[i] == ' ')
                t[i] = 0;
             else
                break;
             sum = sum + t[i];
             z[j] = sum;
          
          }
       
       }
       printf("Input words : %s scroe : %d\n", a[0].names, z[0]);
       printf("Input words : %s score : %d\n", a[1].names, z[1]);
       printf("Input words : %s score : %d\n", a[2].names, z[2]);
       printf("Input words : %s score : %d\n", a[3].names, z[3]);
       printf("Input words : %s score : %d\n", a[4].names, z[4]);
       printf("Input words : %s score : %d\n", a[5].names, z[5]);
       printf("Input words : %s score : %d\n", a[6].names, z[6]);
       printf("Input words : %s score : %d\n", a[7].names, z[7]);
       printf("Input words : %s score : %d\n", a[8].names, z[8]);
       printf("Input words : %s score : %d\n", a[9].names, z[9]);
    
       return 0;
    }
Last edited on
What is your expected output, and what is the output you are getting? "each score does not come out, but the combined score comes out." is a bit vague.
The result I wanted was to enter love and change each alphabet to a number so that the sum is 54.
But it's not as good as I expected, so it's hard to get something you want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int score( string word )
{
   int sum = 0;
   for ( char c : word )
   {
      if ( isalpha( c ) ) sum += 1 + tolower( c ) - 'a';
   }
   return sum;
}

int main()
{
   for ( string s : { "Hardwork", "Knowledge" , "Love" , "Luck" } )
   {
      cout << s << ": " << score( s ) << '\n';
   }
}
Hardwork: 98
Knowledge: 96
Love: 54
Luck: 47
Thank you so much for the answer I wanted.
It was simple to think of it in c ++, not c language.
I will practice writing code in c language.
Thank you for letting me know the isalpha () function that I didn't think of.
Have a nice day today as always. lastchance!
Last edited on
Topic archived. No new replies allowed.