the below code is a c++ program that calculates the sum of the letters in given text.
#include <iostream>
using namespace std;
int letter_sum(string text)
{
int Psum = 0;
int n;
while (text[n] != '')
{
n++;
Psum = Psum + text [n];
}
int Rsum = Psum - (96*n);
return Rsum;
}
int main ()
{
string text;
cout << "type the text: ";
cin >> text;
int displayvalue =letter_sum(text);
cout << displayvalue << endl;
}
as per output I was expecting a num(which is sum of letters in given text ofcourse), but I got a error message showing segmentation fault.im a beginner to coding.so please explain in some easy terms and if possible tell me how to solve this problem.
New contributor
Zenz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1