0

I just want to somehow calculate the sum of the chars in the array by changing them into variables (c = 2, d = 3), in this case it should be 12 ie: (c + c + d + c + d) = (2 + 2 + 3 + 2 + 3). How can I do this? I need something to add to this code.

#include <iostream>

using namespace std;

const int c = 2;
const int d = 3;

int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i > j; i++)
        k += s[i];                 // end result should be 12

}
Seb
  • 1,966
  • 2
  • 17
  • 32

5 Answers5

3

You simply have to transform your char into int type, for exemple with function charToInt:

#include <iostream>

using namespace std;

const int c = 2;
const int d = 3;

int charToInt(char c){
swith (c){
case '1' return 1;
case '2' return 2;
case '3' return 3;
case '4' return 4;
case '5' return 5;
case '6' return 6;
case '7' return 7;
case '8' return 8;
case '9' return 9;
default return 0;
}
}

int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i > j; i++)
        k += charToInt(s[i]);                 // end result should be 12
cout<<k<<endl;
}
Anton Kizema
  • 1,072
  • 3
  • 13
  • 27
2

Well to calculate 12 without doing any conversions, (your program looks liek it doesn't need them), just use simple if statements:

#include <iostream>

using namespace std;

const int c = 2;
const int d = 3;

int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i < j; i++){
        if(s[i]== 'c'){
            k += 2 ;                 // Check if the char is c and add accordingly
        }
        if(s[i]== 'd'){
            k += 3 ;                 // Check if the char is d and add accordingly
        }
    }
    cout << k;
}

You'll get 12 as your output.

Here's a link to the live program: http://ideone.com/Y79WFg

turnt
  • 3,235
  • 5
  • 23
  • 39
1

The simplest way is to change one line:

k += s[i]-97;
Beta
  • 96,650
  • 16
  • 149
  • 150
0
/* It should be noted that no ethically-trained software engineer would ever
   consent to write a DestroyBaghdad procedure. 
   Basic professional ethics would instead require him to write a DestroyCity 
   procedure, to which Baghdad could be given as a parameter.   
                 -- Nathaniel S. Borenstein, computer scientist*/
const int c = 2;
const int d = 3;

int getOrdinal(char c)
{
   switch(c){
    case 'c': return c;
    case 'd': return d;
    default: return 0;
   }
}
int main()
{
    char s[5] = {'c', 'c', 'd', 'c', 'd'};

    int j = sizeof(s) / sizeof(s[0]);
    int k = 0;
    for (int i = 0; i < j; i++)
        k += getOrdinal(s[i]);                 // end result should be 12
    cout << k; /*now prints 12*/
    return 0;
}
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

If I am only understanding you question correctly (which I am probably not)... First, you are trying to reference a variable by a string...

"char s[5] = {'c', 'c', 'd', 'c', 'd'};"

That is impossible, the compiler does not keep variable names.

Try the following:

const int c = 2;
const int d = 3;

int main() {

    const int s[5] = {c, c, d, c, d};

    for (int i = 0 i < (sizeof(s)/sizeof(s[0] ++i)
        k += s[i];                 // end result should be 12

}

Also if you are trying to make the variables match with the letters of the alphabet... Do this:

#include <iostream>

int main() {

    char *String = "Hello World";

    for (char Pos = 0; Pos < sizeof(String)/sizeof(String[0]) ++Pos)
        cout << String[Pos]-'a'; // Outputs the char values combined (a-0, b-1, c-2 etc)

}
Mitch
  • 44
  • 6