Jump to content

Part of the code being skipped

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Fledgling Pidgeon

Fledgling Pidgeon

    Newbie

  • Members
  • Pip
  • 3 posts
Righty, my first real post aside from introduction. I'm running into an issue with a confirmation message in a menu. I remember running into this with C++ about two or three months ago and I circumvented it through use of numbers instead of letters as the variable to confirm and leave the loop. I'd like to figure out why the heck letters won't work though, since I can't think of a logical reason why it wouldn't.

Here's the code as is:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace String_Reduction_Test

{

    public class Variables

	{

        public static char confirmation = 'n';

	}

    public class Functions

    {

        public static void string_to_char()

        {

            string input_text;

            char[] output_character = new char[256];

            input_text = Console.ReadLine();

            for (int i = 0; i < input_text.Length; i++)

            {

                output_character[i] = input_text[i];

            }

            Variables.confirmation = output_character[0];

            input_text = null;

            output_character = null;

        }

        public static void opening_menu()

        {

            Console.WriteLine("PlaneGates#");

            Console.WriteLine("\n");

            while (Variables.confirmation != 'y' || Variables.confirmation != 'Y')

            {

                Console.WriteLine("Start New Game? Y/N");

                Functions.string_to_char();

                if (Variables.confirmation != 'Y' && Variables.confirmation != 'N' && Variables.confirmation != 'y' && Variables.confirmation != 'n')

                {

                    Console.WriteLine("Error");

                }

                if (Variables.confirmation == 'n' || Variables.confirmation == 'N')

                {

                    Console.WriteLine("\"Negative Message\"");

                }

                if (Variables.confirmation != 'y' && Variables.confirmation != 'Y')

                {

                    Variables.confirmation = 'n';

                }

                Console.Read();

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Functions.opening_menu();

        }

    }

}

Now, basically what should happen is the console should ask the user if he wants to start a new game and:

If he types any letter other than y or Y, it loops and asks again.
If he types y or Y, it leaves the loop.

What IS happening, as any quick compilation will show you, is the code is looping regardless of what is pressed and it is skipping the entire Functions.string_to_char method and assuming an Error message, i.e. it is entering the "if" loop for Variables.confirmation different from n, N, y or Y. Any reasons why this happens? I'm imagining it is regarding Variables.confirmation as null after the first round of the "while" loop, but I don't understand why.

And please bear in mind I am a newbie. Don't crush my hopes and dreams... Too harshly. And any "good practice" hints that I may not have known about are welcome.

I'm using Visual Studio 2010 Ultimate, if that makes much of a difference.

#2
haltox

haltox

    Newbie

  • Members
  • PipPip
  • 29 posts
I can see a couple of mistakes in this code.
 

while (Variables.confirmation != 'y' || Variables.confirmation != 'Y')

{

//code

}


For this loop you should use && instead of ||.
No matter what is the value of confirmation it will never be equal to both uppercase and lowercase y. Thus, he will enter the loop.


 for (int i = 0; i < input_text.Length; i++)

            {

                output_character[i] = input_text[i];

            }

            Variables.confirmation = output_character[0];


You shouldn't need to loop there. Just test the length and take the first character directly.

For your ifs, you could make it more like this :

if (Variables.confirmation != 'y' && Variables.confirmation != 'Y'){

//code

}else if (Variables.confirmation == 'n' || Variables.confirmation == 'N'){

//code

}else{

//code

}


Now, as the main problem, that it enters the error if, you should try to output confirmation casted as an int. I don't have experience with C# but perhaps you are not selecting the kind of character you are expecting

#3
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
using System;


namespace ConsoleApplication1 {

    class Program {

        static void Main(string[] args) {

            char result = 'n';


            do {

                result = DisplayMenu();

            } while ("YyNn".IndexOf(result) < 0);


            if (result == 'y' || result == 'Y') {

                Console.WriteLine("They want to play");

            } else {

                Console.WriteLine("They don't want to play");

            }


            Console.ReadLine();

        }


        static char DisplayMenu() {

            Console.WriteLine("PlaneGates#\n");

            Console.WriteLine("Start New Game? Y/N");


            String answer = Console.ReadLine();


            return answer.Length > 0 ? answer[0] : ' ';

        }

    }

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users