In: Computer Science
Please assist with getting my code to correctly check to validate input. Everything else works fine, but when I enter a character, it should say Invalid entry.
Everything else should also work. It should not allow more or less than 9 digits, and it should ask again for input if it doesn't like the input. Please help my code and explain how it is working. I also need to keep the recursive function.
#include <stdio.h>
#include <ctype.h >
int SecNumSum(long long int num) {
//If num is single digit
if (num < 10) {
return num;
}
else {
//return unit place digit + sum of
rest of the number
return (long long int) num % 10 +
SecNumSum(num / 10);
}
}
//Return square of number
int square(int num) {
return num * num;
}
int main() {
//Input social security number and store it in num
variable
//Type of num is long long int to take large value in
input
long long int num;
int count = 0;
printf("Please enter social security number\n");
scanf_s("%lld", &num);
int digits = num;
// run loop until digits is greater
than 0
do
{
count++;
digits /=
10;
} while (digits != 0);
while (count != 9 || count >
9)
{
printf("Invalid number. Please enter social security number
\n");
scanf_s("%d",
&num);
digits =
num;
count = 0;
do
{
count++;
digits /= 10;
} while (digits
!= 0);
}
int ssnSum =
SecNumSum(num);
printf("Sum of all digits: %d\n",
ssnSum);
printf("Your security code will be: %d\n", square(ssnSum));
return 0;
}
An easier approach to solve this would to read the security number as a string (array of characters) instead of reading it as a number. Here we can easily check the length using the strlen function. Also, we can use the isdigit() function, to check if each character is a digit. If every character is a digit, and it is of length 9, then it is a valid security number and we can simply convert it into integer data type using atoi (ascii to integer) function. We then proceed to compute the sum and the square as in the code above.
Code has been shared below.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int SecNumSum(long long int num) {
//If num is single digit
if (num < 10) {
return num;
}
else {
//return unit place digit + sum of rest of the
number
return (long long int) num % 10 + SecNumSum(num /
10);
}
}
//Return square of number
int square(int num) {
return num * num;
}
// if the security number is invalid returns -1 else returns
0
int isValidSecNum(char digits[])
{
// checks the length
int length = strlen(digits);
if (length != 9)
{
printf("Invalid length.\n");
return -1;
}
// checks if each character is a digit
for (int i = 0; i < length; i++)
{
if (!isdigit(digits[i]))
{
printf("Not a
valid security number.\n");
return -1;
}
}
return 0;
}
// reads the security number as an array of characters
long long int readSecNum()
{
char num[10];
while (1)
{
printf("Please enter social
security number\n");
scanf_s("%s", num);
// breaks out of loop is the
security number is valid
if (isValidSecNum(num) == 0)
break;
}
return atoi(num);
}
int main() {
//Input social security number and store it in num
variable
//Type of num is long long int to take large value in
input
long long int num;
num = readSecNum();
int ssnSum = SecNumSum(num);
printf("Sum of all digits: %d\n", ssnSum);
printf("Your security code will be: %d\n",
square(ssnSum));
return 0;
}
Screenshot (included for readability)
Output