Jump to content
  • Advertisement
Sign in to follow this  
Dreamseeker

Help me with my code

This topic is 560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();

Share this post


Link to post
Share on other sites
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));

Share this post


Link to post
Share on other sites
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) Edited by Nypyren

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites
"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. Edited by Nypyren

Share this post


Link to post
Share on other sites

"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!

Edited by Dreamseeker

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!