I need some advice from people who know what they're are talking about. The question I am struggling with is below and my attempt is below that. Can you tell where I'm going wrong? Thanks.
I'm trying to acheive this:
(A) Read the first name and the surname of a person.
Calculate and output n1 = the integer representing the first letter (counting
from the left) of the first name in the ASCII table.
For example, if the first name is “James”, then n1=74 (i.e. the value representing ‘J’ is
74 in the ASCII table).
Note that there is no restriction that the first letter entered must be a capital letter.
(B) Calculate and display n2 = the integer representing the first letter of the
surname in the ASCII table.
For example, if the surname is “Bond”, then n2 = 66 (i.e. the value representing ‘B’ is
66 in the ASCII table).
(C) Calculate and display n3 = the squared digit length of n1.
The following process determines the squared digit length of an integer. Take any
integer and add up the squares of its digits. This will give you another integer. Repeat
this procedure until the number you end up with is 1 or 4. The number of times this
process has to be repeated before it gets to 1 or 4 is the squared digit length. For
example, if we start with 85, we get:
82 + 52 = 89
82 + 92 = 145
12 + 42 + 52 = 42
42 + 22 = 20
22 + 02 = 4
This process shows that the squared digit length of 85 is 5.
According to the experts, this process will always eventually reach either 1 or 4. The
squared digit length of 1 and 4 is zero, since we don't actually have to apply the
process to them to reach the stopping condition (interestingly, though, if we did apply
the process, to 1, we would keep getting 1, but if we applied it to 4, we would get a
repeating sequence: 4, 16, 37, 58, 89, 145, 42, 20, 4).
(D) Calculate and display n4 = the squared digit length of n2.
(E) Calculate and display n5 = the largest prime factor of n3+n4.
A prime factor of integer n is a factor of n which is a prime number. A prime number
is any integer greater than 1 and only divisible by itself and 1 (e.g. 2, 3, 5, 7, 11, 13,
17 etc). For example, 3 is the largest prime factor of 27 and 7 is the largest prime
factor of 49.
And this is what I have so far:
#include <iostream>
#include <cmath>
using namespace std;
bool largest_prime_factor(int b);
int squared_digit_length(int a);
int main()
{
char ch1, ch2;
int n1, n2, n3, n4, n5; // used for the 5 steps of the task
cout << "Enter your first name and surname: " << flush;
cin >> ch1; // read the first letter of the first name
cin.ignore(100, ' '); // ignore the rest characters in the first name
cin >> ch2; // read the first letter of the surname
n1 = (int)ch1;
n2 = (int)ch2;
n3 = squared_digit_length(n1);
n4 = squared_digit_length(n2);
n5 = n4 + n5;
cout << n1 << n2 << endl;
squared_digit_length(n1);
squared_digit_length(n2);
largest_prime_factor(n5);
return 0;
}
int squared_digit_length(int a)
{
int result = 0;
while (a)
{
int t = a % 10;
result += t * t;
a /= 10;
}
return result;
}
bool largest_prime_factor(int a)
{
int main()
{
int n3,n4,n5;
cout << "n3=" ;
cin >> n3 ;
cout << "n4= " ;
cin >> n4;
n5 = n3+n4;
cout << largest_prime_factor(n5) << endl;
return 0;
}
}