Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I am trying to make a simple shotgun game where the user vs the CPU and the both pick shot, shield or reload but In my GetOptionFromUser I keep getting the error Cannot implicitly convert type string to;ShotgunGame.Program.ShotgunOption from the cases in my switch statement.


Any Guidance would be appreciated

The error happens where it says
C#
case "SHOOT":, case "RELAOD":, case "SHIELD":


C#
//Declare Variables
            Console.Title = "Welcome To The Shotgune Game";
            int CPUBullets = 3, userBullets = 3;
            ShotgunOption UserOption;
            int computerChoice, userScore = 0;
            bool QUIT = false;
            double gameCount = 0.0;
            Random computer = new Random();
           
            Console.Clear();
            Console.WriteLine("SHOOT RELOAD SHIELD");

            UserOption = GetOptionFromUser();
            ShotgunOption CPUOption = (ShotgunOption)computer.Next(1, 3); // 1 is Shot, 2 is Reload, 3 is Shield

             do
            {
                

                //if (UserOption == "QUIT")
                //{
                   // break;
                //}
                
                do
                {
                //Console.Write("Please enter choice, or enter QUIT to quit: ");
                
             


                  switch (UserOption)
                {
                    case "SHOOT":
                        if((int)CPUOption == 1)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shoot. It was a tie!", userChoice);
                            ; userBullets --;CPUBullets --; ++gameCount;
                        }
                        else if ((int)CPUOption == 2)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                            ++userScore; ++gameCount;
                        }
                        else if ((int)CPUOption == 3)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                            ++gameCount;
                        }
                        break;
                    case "RELAOD":
                        if((int)CPUOption == 1)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                             ++userScore; ++gameCount;
                        }
                        else if ((int)CPUOption == 2)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Reload. You Both Gain A bullet", userChoice);
                            userBullets++; CPUBullets++; ++gameCount;
                        }
                        else if ((int)CPUOption == 3)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                           
                        }
                        break;
                    case "SHIELD":
                        if((int)CPUOption == 1)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shoot. You lose!", userChoice);
                            ++gameCount;
                        }
                        else if ((int)CPUOption == 2)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Reload. You win!", userChoice);
                            ++userScore; ++gameCount;
                        }
                        else if ((int)CPUOption == 3)
                        {
                            Console.WriteLine("You chose {0} and the computer chose Shield. No Damage!", userChoice);
                            ++gameCount;
                        }
                        break;                  
                       
                }
              }
                while (UserOption != ShotgunOption.Shield || CPUOption != ShotgunOption.Shield);
            } while (QUIT == false || gameCount == 3);
Posted
Updated 24-Jun-15 7:54am
v2
Comments
[no name] 24-Jun-15 13:58pm    
The error is very clear and specific. So..... what exactly is your question?

OK...
Your switch block uses UserOption which is declared as:
C#
ShotgunOption UserOption;

But your
case
statements all use strings:
C#
case "SHOOT":
    ...
case "RELAOD":
    ...
case "SHIELD"

So unless ShotgunOption is actually a string (which it isn't, string is a sealed class) means that you can't directly compare it's values to strings. It's liek trying to compare integers to strings - would you expect
C#
if (123 == "hello!")
   {
   ...
To compile, or even make any sense in the real world? :laugh:
What you're doing is in fact exactly that, I suspect: ShotgunOption is almost certainly an
enum - which means that it's really an integer, but with added names.

I suspect that what you want to try is this:
C#
case ShotgunOption.Shoot:
    ...
case ShotgunOption.Reload:
    ...
case ShotgunOption.Shield
Or at least something very similar!
 
Share this answer
 
You're trying to compare the enum type ShotgunOption with a string.  Instead use the different enum values in your case statements.

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900