Help me with my code

Started by
4 comments, last by Dreamseeker 7 years, 1 month ago

Hi! Take a look at my code, my problem is that commaLocation1 is taking the value "2" when is supossed to be "4". Please help me to find the bug. Thanks in advance.


namespace Lab8
{
    class Program
    {
        static void Main(string[] args)
        {
            //Read user input
            Console.Write("Type the values with the next format: <pyramid slot number>,<Block letter>,<Whether or not the block should be lit> ---> ");
            string userInput = Console.ReadLine();

            //Store pyramid slot number
            int commaLocation0 = userInput.IndexOf(',');
            int slotNumber = int.Parse(userInput.Substring(0, commaLocation0));

                //Print slot number
                Console.WriteLine("Pyramid slot number: " + slotNumber);

            //Store block letter
            int commaLocation1 = userInput.IndexOf(',', commaLocation0 + 1);
            char blockLetter = char.Parse(userInput.Substring(commaLocation0 + 1, 1));

                //Print block letter
                Console.WriteLine("Block letter: " + blockLetter);

            //Store whether or not the block should be lit
            /*bool blockLit = bool.Parse(userInput.Substring(commaLocation1 + 1));

            //Print block lit
            Console.WriteLine("Should the block be lit?: " + blockLit);*/

            Console.ReadKey();
Advertisement

Never programmed c#, but given you have only one assignemt to 'commaLocation1', I am betting it's line 19.

To understand the result, dump each letter with its index of the 'userInput' string just before the line, so you can literally see what letter is at each position.

Then also print "commaLocation0 + 1" so you can manually look what indexOf returns, and why.

Likely something like


for (int idx = 0; idx < userInput.length; idx++) { // As I never coded in C#, names of variables and methods may be wrong, but you should be able to find the equivalent method in C#, I hope.
    console.printLine(userInput.at(idx) + ": " + idx);
}
console.printLine("indexOf startpoint:" + (commaLocation0 + 1));
Dreamseeker: That code looks and works fine for me (even the commented out part works fine). What did you use for userInput when it didn't work?

(BTW, you can simplify the char.Parse line by just using userInput[commaLocation0+1] as your expression instead of using Substring and char.Parse)

Dreamseeker: That code looks and works fine for me (even the commented out part works fine). What did you use for userInput when it didn't work?

(BTW, you can simplify the char.Parse line by just using userInput[commaLocation0+1] as your expression instead of using Substring and char.Parse)

I use "15,m,true". The 15 is fine. The program crashes with the second value!. The IDE (Visual Studio) is telling me comaLocation1 is getting a value of "2" after the 19th line of code... I'm reading the c# documentation over and over but I can't see the problem...

"15,m,true" (typed in without quotes, of course) worked perfectly for me. I copy+pasted your code directly into Visual Studio 2017.

You're definitely using the string.IndexOf functions correctly. I don't see anything wrong with that.

I guess the next thing I would do if I were you would be to recreate the VS project from scratch (and copy/paste your code in) and see if that helps. Perhaps your project settings are messed up somehow.

"15,m,true" (typed in without quotes, of course) worked perfectly for me. I copy+pasted your code directly into Visual Studio 2017.

You're definitely using the string.IndexOf functions correctly. I don't see anything wrong with that.

I guess the next thing I would do if I were you would be to recreate the VS project from scratch (and copy/paste your code in) and see if that helps. Perhaps your project settings are messed up somehow.

I ran it again and added the hints you gave and it works fine... I don't understand what happened jeje thank you mate!

This topic is closed to new replies.

Advertisement