Click here to Skip to main content
15,895,792 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i was trying to calculate the summation of characters of a name such as a=1,b=2...z=26
untill summation is less than 10.
the value of calculator function is returning corrcectly for first string but it's not working correctly for 2nd string...help please...thanks in advance..

What I have tried:

C++
#include <iostream>
#include
using namespace std;


double calculator(int n)
{
    int m=n;double num=0.0;
    while(m!=0)
    {
        int digit=m%10;
        num+=digit;
        m=m/10;

    }

    if(num>=10)
        calculator(num);
        else
        return num;
}


int main()
{
    string s1,s2;
    cin>>s1>>s2;
    transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
     transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
//cout<<s1<<" "<<s2;


        int len1=s1.size();
        int len2=s2.size();
        int number1=0,number2=0;
        int digit;
        for(int i=0;i<len1;i++)>
        {

            if(s1[i]=='a')
                digit=1;
            else if(s1[i]=='b')
            digit=2;
            else if(s1[i]=='c')
                digit=3;
            else if(s1[i]=='d')
                digit=4;
            else if(s1[i]=='e')
            digit=5;
            else if(s1[i]=='f')
                digit=6;
            else if(s1[i]=='g')
                digit=7;
                else if(s1[i]=='h')
            digit=8;
            else if(s1[i]=='i')
                digit=9;
            else if(s1[i]=='j')
                digit=10;
            else if(s1[i]=='k')
            digit=11;
            else if(s1[i]=='l')
                digit=12;
            else if(s1[i]=='m')
                digit=13;
                else if(s1[i]=='n')
            digit=14;
            else if(s1[i]=='o')
                digit=15;
            else if(s1[i]=='p')
                digit=16;
            else if(s1[i]=='q')
            digit=17;
            else if(s1[i]=='r')
                digit=18;
            else if(s1[i]=='s')
                digit=19;
                else if(s1[i]=='t')
            digit=20;
            else if(s1[i]=='u')
                digit=21;
            else if(s1[i]=='v')
                digit=22;
            else if(s1[i]=='w')
            digit=23;
            else if(s1[i]=='x')
                digit=24;
            else if(s1[i]=='y')
                digit=25;
                else if(s1[i]=='z')
                    digit=26;

            //int digit=(int)s1[i];
            number1+=digit;
        }

        double x=calculator(number1);
         for(int i=0;i<len2;i++)>
        {
            if(s2[i]=='a')
                digit=1;
            else if(s2[i]=='b')
            digit=2;
            else if(s2[i]=='c')
                digit=3;
            else if(s2[i]=='d')
                digit=4;
            else if(s2[i]=='e')
            digit=5;
            else if(s2[i]=='f')
                digit=6;
            else if(s2[i]=='g')
                digit=7;
                else if(s2[i]=='h')
            digit=8;
            else if(s2[i]=='i')
                digit=9;
            else if(s2[i]=='j')
                digit=10;
            else if(s2[i]=='k')
            digit=11;
            else if(s2[i]=='l')
                digit=12;
            else if(s2[i]=='m')
                digit=13;
                else if(s2[i]=='n')
            digit=14;
            else if(s2[i]=='o')
                digit=15;
            else if(s2[i]=='p')
                digit=16;
            else if(s2[i]=='q')
            digit=17;
            else if(s2[i]=='r')
                digit=18;
            else if(s2[i]=='s')
                digit=19;
                else if(s2[i]=='t')
            digit=20;
            else if(s2[i]=='u')
                digit=21;
            else if(s2[i]=='v')
                digit=22;
            else if(s2[i]=='w')
            digit=23;
            else if(s2[i]=='x')
                digit=24;
            else if(s2[i]=='y')
                digit=25;
                else if(s2[i]=='z')
                    digit=26;

            number2+=digit;
        }

        double y=calculator(number2);                  //why it's not returning exact value??

        if(x<y)>
        {
            double cal=(x/y)*100;
            printf("%.2lf%\n",cal);
        }
        else
        {
            double cal=y/x;
            cal*=100;
            printf("%.2lf%\n",cal);
        }
    //cout << "Hello world!" << endl;
    return 0;
}
Posted
Updated 4-May-16 22:33pm
v5
Comments
Sergey Alexandrovich Kryukov 5-May-16 1:25am    
No, this is not C++ code. This is... hopeless. Your main problem is not even lack of the clue what programming is; after all, you could learn it with some time and effort. Your problem is total lack of care. Guess how I know it?...
—SA
Raje_ 5-May-16 1:55am    
This is your homework. Isn't it?
George Jonsson 5-May-16 2:23am    
What does your strings contain?
Only letters, letters and digits or any character?
You need to show some samples.

First off, get rid of the long if..else if...else if... code for converting a char to an int:
C++
if (s1[i] >= 'a' && s1[i] <= 'z')
   {
   digit = (int)(s1[i] - 'a') + 1;
   }
Will do it.
Then sort out your indentation! At the moment that is very hard to read as the code is all over the place and it's not obvious at all.
Then extract your code for processing a string into a function which accepts a string and returns the number value - call that twice, once for each string in the same way that you have the calculator function.
That way, you are using the same code for processing each string.
If you still have a problem, then use the debugger to follow the code through and look at exactly what it is doing at each stage.
Yes, I could do that for you - but I don't know what you are typing as the two strings, and it's your homework! :laugh:
Give it a try - see what you can find!
If you are still stuck, come back to us and explain exactly where it goes wrong, and what values you are getting.
 
Share this answer
 
Comments
George Jonsson 5-May-16 2:31am    
Speaking of indentation. :-D
Also the function isalpha() could be used in the if-statement.
+5
OriginalGriff 5-May-16 2:46am    
My indentation is correct - it's Whitesmiths. :laugh:
George Jonsson 5-May-16 3:00am    
Thought that style was long dead. Hehe
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to see what calculator is doing.

Advice: Think about what you do. you duplicate the code for 2 strings.
What would you do with 3 strings ? copy once more ?
And with 5 strings? with 10 strings ? with 100 strings ?
By making a function, you ensure that both strings are processed the same way.
 
Share this answer
 
The calculator function is flawed. Try
C++
int calculator(int n)
{
    int m = n,  num=0;
    while( m !=0 )
    {
        int digit= m % 10;
        num += digit;
        m = m / 10;

    }

    if( num >= 10 )
      return calculator(num);
    else
      return num;
}



[update]
Since you've already used the algorithm header, you may also try
C++
 #include <iostream>
 #include <algorithm>
 #include <iomanip>
 using namespace std;

int accumulator(const int & cur_value, const char & c)
{
  return (cur_value + (::tolower(c) - 'a' + 1));
}

int modul(int n)
{
  int result = 0;
  while (n)
  {
    result += n % 10;
    n /= 10;
    result = result % 10 + result / 10;
  }
  return result;
}

int main()
{
    string s1,s2;
    cin >> s1 >> s2;

    int i1, i2;

    i1 = accumulate(s1.begin(), s1.end(), 0, accumulator);
    i2 = accumulate(s2.begin(), s2.end(), 0, accumulator);

    cout << i1 << ", " << i2 << endl;

    int x1, x2;
    x1 = modul(i1);
    x2 = modul(i2);

    cout << x1 << ", " << x2 << endl;

    if ( x1 > x2) swap(x1,x2);
    double cal = 100.0 * x1 / x2;

  cout << fixed << setprecision(2) <<  cal << "%" << endl;
}

[/update]
 
Share this answer
 
v3
To make the code more readable you can put the exit condition first.
That way it is clear what will cause the recursion to stop.

C++
int calculator(int n)
{
    // Exit condition
    if (n < 10)
        return n;

    int m = n,  num = 0;
    while (m != 0)
    {
        num += m % 10;
        m = m / 10;
    }
 
    return calculator(num);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900