tags:

views:

89

answers:

4

Hello, was referred to this site from a friend of mine, I hope to stay here long =)

I have a homework that I just can't figure out how to do. I'm not sure if the teacher wrote it wrong(he is a bad typer) or if there is a way to accomplish the task.

I work in Visual C# 2010 Express - Console Application

My task is to:

Read a four digit integer, such as 5893, from the keyboard and display the digits separated from one another by a tab each. Use both integer division and modulus operator % to pick off each digit. If the user enters 4567, the output looks like:

4567

4 5 6 7

Sure I know how to seperate the numbers by using \t aswell as reading the input and displaying it to the user. But how am I supposed to 'pick off' each digit with division and the remainder operators??? Maybe he means something else, but not sure. Have to ask him if noone here knows a solution... hehe

And another question...

How do a make sure that what the user types in is a number and not a letter? Do i have to use "Char.IsLetter", because I couldn't figure out how to use it on a parsed string.

example:

        string number1;
        int x;
        Console.Write("Please enter a number to calculate: ");
        number1 = Console.ReadLine();
        x = Int32.Parse(number1);

What method am I supposed to use and where do i put it in? Beacause now i only get an error and the application shuts down if I try to enter e letter....

Thanks in advance!

/Anthony

P.S. I'm very new to C# programming, though I do understand logic...

+1  A: 

System.Int32.TryParse() might be a better choice when converting the String.

Crag
Hmm... I'll give it a go, though I just figured out that I maybe don't need the string I'm using, I can just stick to the integer. Thank you for your answer!
A: 

Yes, your assignment makes sense. Say you have an integer x = 4567. To pick out the digit at position A you would use:

result = (int)((x / (A * 10)) % 10);

The first part of this (x / (A * 10)) "shifts" x to the right. So a value of A = 1 would divide 4567 by 10 which results in 456.7. You then use the modulo operator "%" to pick out the unit part of that number. Finally you convert to an int to remove the fractional part.

Groky
Why the downvote?
Groky
Ahh, now I see. That makes sense to me now =) I'll try to implement it and work with it a bit. Thank you for such a quick and clear answer!
@Groky - Probably because it's homework, and giving someone the exact answer to their homework is discouraged here. Instead, provide hints and guidance, like Evgeny did.
Smashery
True - guess I'm used to giving people answers on here... Sorry fierflash, please do make sure you understand before using it :)
Groky
+5  A: 

The first question is really more about maths than programming. You know what the division and modulus operators do. Think about how you could use them to get the last (least significant) digit. Once you've done that you can apply a similar technique for the 2nd digit (tens) and then the same for hundreds and thousands and so on.

You've already found Char.IsLetter, but there is also Char.IsDigit, which does what you want more directly. You can do this check for one character and what you have is a string of characters. Have a look at a foreach loop.

Evgeny
Thanks, I will give it a go and see how it works out, really nice response time!
Voted your comment up as I like the idea of explaining without giving code. After all, this is homework so giving the (partial) code would ruin a great deal :).
bastijn
A: 

Ok, I won't give the whole solution (after all, this is homework ;)).

This code would check if there's four characters in input:

        string input;
        do
        {
            input = Console.ReadLine();
            if (input.Length != 4)
                Console.WriteLine("You have to enter FOUR digits");
        } while (input.Length != 4);

This could be one way of checking the digits:

        bool isOk = true;
        foreach (char c in input.ToArray())
        {
            if (!Char.IsDigit(c))
            {
                Console.WriteLine("You have to enter four DIGITS");
                isOk = false;
                break;
            }
        }

This approach doesn't deal with math but you could do that just as well:

int num = int.Parse(input);
int tensOfThousands = num / 10000;
int thousands = (num - tensOfThousands) / 1000;
int lastDigit = num % 10;
//the rest of the digits are up to you ;)
veljkoz