Hassan A.
Hassan A.

Reputation: 23

Getting user input from a bool method and using it in a different bool method

So I have a bool method that checks if the user has a club membership, then I have another bool method which is supposed to tell what's supposed to be printed if the user does have a membership and play the little game to check if the user wins a prize. The problem is, when I run the program it prints out the "Welcome to the VIP only, sir." twice and skips the rest of the code, so what can I do to fix this issue.

static void Main(string[] args)
{
    LoyaltyDiscount();
    EnterContest(RandomClass);
}
    private static bool LoyaltyDiscount()
    {
        string input;
        Write("\nAre you a member of our Futility Club frequent shopper program? ('Y' if yes) ");
        input = ReadLine().ToUpper();
        if(input == "YES" || input == "Y")
        {
            //if user does have a club membership
            WriteLine("Welcome to the VIP only, sir.");
            return true;
        }
        else
        {
            //if user does not have a club membership
            WriteLine("Thats okay, we still appreciate your purchase.");
            return false;
        }
    }
    private static bool EnterContest(Random chanceGenerator)
    {
        string input = LoyaltyDiscount().ToString();
        int randomNumber;
        const int gameMin = 1;
        const int gameMax = 10;
        Random RandomClass = new Random();
        randomNumber = RandomClass.Next(gameMin, gameMax);

        if ((input == "YES") || (input == "Y"))
        {
            //prompt if user answers "yes" to if they a club membership and ask to play the game
            Write("Do you want to play a game and win a prize?");
            int.TryParse(ReadLine(), out int guess);
            if (guess < gameMin || guess > gameMax)
            {
                //if user guesses number out of boundary
                WriteLine("Guess out of boundary");
                return false;
            }
            if (guess == randomNumber)
            {
                //if user guesses correct number
                WriteLine("Correct guess.");
            }
            else
            {
                //if user guesses wrong number
                Write("Wrong guess.");
                return false;
            }
        }
        return true;
    }

The goal is to get the user input from LoyaltyDiscount() method and ask if the user does have a membership, then if the user does have a membership, use it in EnterContest() method only if they have a membership.

Upvotes: 1

Views: 339

Answers (2)

Tuanyang Qiu
Tuanyang Qiu

Reputation: 13

1. you called the method 'LoyaltyDiscount()' for first time here.

static void Main(string[] args)
{
    LoyaltyDiscount();
    EnterContest(RandomClass);
}

Then you called it again here. That's why it printed the sentence twice.

private static bool EnterContest(Random chanceGenerator)
{
    string input = LoyaltyDiscount().ToString();
    .
    .
}

2. The return value of this method 'LoyaltyDiscount()' is bool, and it was transformed to string type to be 'True' or 'False'. if you print the result of LoyaltyDiscount().Tostring() you will see what happened.

string input = LoyaltyDiscount().ToString();
...Omit some codes...
Console.WriteLine(input);//it will be either 'True' or 'False'
//So here you could change the 'if' condition to be like this:
//if (input=="True")

if ((input == "YES") || (input == "Y"))
{
 .......
}

Upvotes: 1

Theepag
Theepag

Reputation: 309

You calling LoyaltyDiscount() method in two places once in main method then in EnterContest() method. that's the reason it prints two times.

Upvotes: 1

Related Questions