7 Replies - 789 Views - Last Post: 07 April 2014 - 11:49 AM Rate Topic: -----

#1 lort20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 14

C# - Need help with prompting while using Method.

Posted 06 April 2014 - 08:57 PM

This was a working code before I added the displayMenu().

I'm trying to get num to work with the displayMenu but I'm not having any luck. The displayMenu() should be able to use the int num.

What I'm trying to do is prompt for an If selection using the displayMenu after calling it under the Play().

Please tell me what I'm doing wrong and thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment_4
{
    class HighLowUI
    {
        public void Welcome()
        {
            Console.WriteLine("**********************************");
            Console.WriteLine("* THE ULTIMATE HIGH AND LOW GAME *");
            Console.WriteLine("*    Try to guess the number     *");
            Console.WriteLine("**********************************");

        }

        public void Play()
        {
            int guess = 0;

            Welcome();
            HighLow myGame = new HighLow();

            while (myGame.Goal != guess)
            {
                guess = PromptforInteger(myGame.HighLimit);

                myGame.Compare(guess);
            
            Console.WriteLine(myGame.Result);
            }
            Console.WriteLine("The guessing number was: {0}", myGame.Goal);
        }

        public int displayMenu(int num)
        {
            if (num == 1)
            {
                Console.WriteLine("");
            }

            else if (num == 2)
            {
                Console.WriteLine("");
            }

            else if (num == 3)
            {
                Console.WriteLine("");
            }

            return num;
        }
        
        public int PromptforInteger(int prompt)
        {
            int value;
            Console.WriteLine("Enter a number between 1 and " + prompt);

            int.TryParse(Console.ReadLine(), out value);

            return value;
        }

        public void PlayGame()
        {
            string choice = "Y";
            int num;

            while (choice == "Y")
            {
                displayMenu(num);
                Play();

                Console.WriteLine("");
                Console.WriteLine("Play Again? \t Y/N: ");

                num = int.Parse("");
                choice = Console.ReadLine();
            }
        }
    }
}


Line 75 has num in blue and that is the only error I'm getting.

It says Use of unassigned local variable 'num'.

Which I tried using the parse and tryparse but it failed.

Is This A Good Question/Topic? 0
  • +

Replies To: C# - Need help with prompting while using Method.

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# - Need help with prompting while using Method.

Posted 06 April 2014 - 11:13 PM

Well, line 71 declares num, but it doesn't give it a value. Get in the habit of initializing your variables when you declare them. That's why you get the error on 75 because num doesn't have a value.

I'm not sure what the point of your DisplayMenu method is. It takes an int, writes a blank line if it is 1, 2 or 3, then returns the very same value you sent it.
Was This Post Helpful? 0
  • +
  • -

#3 lort20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 14

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 04:54 AM

View PostMomerath, on 06 April 2014 - 11:13 PM, said:

Well, line 71 declares num, but it doesn't give it a value. Get in the habit of initializing your variables when you declare them. That's why you get the error on 75 because num doesn't have a value.

I'm not sure what the point of your DisplayMenu method is. It takes an int, writes a blank line if it is 1, 2 or 3, then returns the very same value you sent it.



I've tried giving num a value like. Console.ReadLine() and Convert.ToInt16() but it doesn't work.
What num's value should be is whatever the person prompts.

The displayMenu has three option. Easy, Medium, and Hard mode which would allow the person to prompt from 10, 50 to 100.

Does anyone know what I should put in to make num work?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7054
  • View blog
  • Posts: 23,982
  • Joined: 05-May 12

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 05:31 AM

Show us your attempt to use Console.ReadLine() and Convert.ToInt16(), it's quite possible, in the words of Steve Jobs, "you are holding it wrong".
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 09:43 AM

You've already got a PromptForInteger method. You can re-use that to prompt for whatever value num should be. Or you could make a slightly different method that shows a different prompt (like "Enter 1 for easy, 2 for medium, 3 for hard") and parse and return the value the exact same way.
Was This Post Helpful? 0
  • +
  • -

#6 lort20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 14

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 11:09 AM

View PostSkydiver, on 07 April 2014 - 05:31 AM, said:

Show us your attempt to use Console.ReadLine() and Convert.ToInt16(), it's quite possible, in the words of Steve Jobs, "you are holding it wrong".



        public void PlayGame()
        {
            string choice = "Y";
            int num;

            while (choice == "Y")
            {
                displayMenu(num);
                Play();

                Console.WriteLine("");
                Console.WriteLine("Play Again? \t Y/N: ");

                num = Console.ReadLine();
                choice = Console.ReadLine();
            }


It says cannot implicitly convert type 'string' to 'int'.

View PostCurtis Rutland, on 07 April 2014 - 09:43 AM, said:

You've already got a PromptForInteger method. You can re-use that to prompt for whatever value num should be. Or you could make a slightly different method that shows a different prompt (like "Enter 1 for easy, 2 for medium, 3 for hard") and parse and return the value the exact same way.


        public void PlayGame()
        {
            string choice = "Y";
            int num;

            while (choice == "Y")
            {
                displayMenu(num);
                Play();

                Console.WriteLine("");
                Console.WriteLine("Play Again? \t Y/N: ");

                num = PromptforInteger();
                choice = Console.ReadLine();
            }


The one above is with Console.ReadLine(). This is with PromptforInteger().
PromptforInteger() is in red and says: No overload for method 'PromptforInteger' takes 0 arguments.
Even when I put another varible in there, it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 11:31 AM

Did you copy the code from a tutorial or have it provided by an instructor? I'm just asking because it seems that you don't understand how the code actually works, since it's already doing most of what you want.

That method expects an integer parameter. It uses that in its prompt to the user. So if you don't pass it a number, it won't work. The number is used in this line:

Console.WriteLine("Enter a number between 1 and " + prompt);


See how it's concatenating prompt with the rest of the string? That prompt variable is the parameter for the function. So it's going to print whatever number you pass it.

If you want to ask the user to enter a value between 1 and 3, you would call PromptForInteger(3).

But I'm thinking you're prompting in the wrong spot. Your DisplayMenu function shouldn't be expecting a number, it should be prompting for one and returning the choice (I assume). Then you can use the result to set the difficulty level.
Was This Post Helpful? 0
  • +
  • -

#8 lort20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 31-March 14

Re: C# - Need help with prompting while using Method.

Posted 07 April 2014 - 11:49 AM

Yes, I got most of the code from an instructor. That's what I want done with my DisplayMenu. I will keep playing around with the code some more until I get something.
Thank you for your help and understanding of this.

This post has been edited by Curtis Rutland: 07 April 2014 - 11:51 AM
Reason for edit:: use the reply button at the bottom, don't Quote+Reply the last reply in the thread

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1