-
January 16th, 2014, 04:44 PM
#1
Need some help with my program.
Hi all,
I've read the FAQ on homework questions but I am really stuck at the moment and to be honest I have no idea what I am doing. The task at hand is as follows.
"An Albanian social security number has a sequence of digits
followed by a letter. This 'check' letter is determined from
the values of the digits and is used to ensure that the numbers
are correct. Write a program that, when given a ten-digit
number and letter as input, calculates a unique 'check'
character corresponding to the given number according to the
following rules:
Add the five pairs of digits contained in the number
Take the remainder of dividing the result by 26
Select the letter in that position of the alphabet, starting
with 'A' in position 0, 'B' in position 1, and so on.
For example, if the input is 1122334455J, the program should
calculate (11 + 22 + 33 + 44 + 55) % 26 giving 9, and so 'J'
is the 'check' character. This means that the social security
number quoted is correct. If the input was 1122334455M, your
program should detect that the check letter is incorrect,
and therefore that this social security number is false."
This is what I've got so far.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CourseWork
{
class Program
{
static void Main(string[] args)
{
string socialNumber;
char check;
Console.WriteLine("please Write your social number without the letter."); //asks user to input social number without letter
string first = Console.ReadLine();
socialNumber = first.Substring(0, 10);
check = first[first.Length-1];
Console.WriteLine(check+socialNumber);
int sum = 0;
for (int i =0; i < socialNumber.Length-1; i++)
{
sum += Int32.Parse(socialNumber[i].ToString());
}
int remainder = sum % 26;
if (remainder == check)
{
Console.WriteLine("This is a correct number.. the check is {0} \\n the remainder is {1}", check, remainder);
}
else
{
Console.WriteLine("Please write your check letter.");
}
Console.WriteLine(sum);
Console.ReadLine();
}
}
}
So far I've asked the end user to input their social security number without the check letter.
What I can't get my head around is the division doesn't work and always returns the number 25 or it just closes the program. I don't know where to go from here >_< can somebody help me?
Could somebody possibly help me correct and finish the code for me with comments on within the program to whats been done to it?
Many thanks
-
January 16th, 2014, 08:04 PM
#2
Re: Need some help with my program.
Press F8 to STEP thru the code line by line, and then examine the value of this:
Code:
Int32.Parse(socialNumber[i].ToString());
It is probably not what you expect.
-
January 19th, 2014, 01:04 AM
#3
Re: Need some help with my program.
When you extract out the number in the sum += line, you need to extract out two characters at a time (and increment the index of he loop by 2).
-
January 19th, 2014, 05:29 AM
#4
Re: Need some help with my program.
Hi, could you possible delete this thread as I have solved the program by myself. Thank you
-
January 19th, 2014, 01:38 PM
#5
Re: Need some help with my program.
Originally Posted by harryc1127
Hi, could you possible delete this thread as I have solved the program by myself. Thank you
Instead of deleting the thread, how about you taking a few minutes to post your solution or at least state how you fixed it? That way, any folks searching for similar can find it (and the time spent replying to you isn't wasted).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|