I am new to C# and am having trouble with some code that I am writing. Below is the code. I want the user to be able to enter a number between 10 and 50. Once the user does this, I want it to evaluate to Acceptable if it is within the range and Unacceptable if it is not. I have written the code for this in the CheckRange method.
Can anyone tell me what I am doing wrong?
Thanks.

static void Main()
        {
            bool choice = true;
            char character = ' ';
            int number = 0;
            string validInput;

            DisplayInfo();
            
            while (choice)
            {
                number = PromptForInput(ref character);
                validInput = CheckRange(number);
                choice = GetInput();
            }//End of while loop
       }// End of Main
            
        static void DisplayInfo()
        {
            Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***");
            Console.WriteLine("***  This program anables you to enter as    ***");
            Console.WriteLine("***  many values as you wish....Between the  ***");
            Console.WriteLine("***  range of 10 and 50.                     ***");
            Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***");
            Console.WriteLine();

            //Console.Read();  
        }// End of DisplayInfo

        static bool GetInput()
        {
            char input;
            bool choice;

            Console.Write("\nWould you like to enter another value?" +
                           "\nPlease enter y for yes or any other letter for no:");
            input = char.Parse(Console.ReadLine());
            Console.WriteLine();

            switch (input)
            {
                case 'y':
                case 'Y': choice = true;
                    break;
                default: choice = false;
                    break;
            }
            return choice;
            }//end of GetInput

        
        public static int PromptForInput(ref char character)
        {
            string input = " 1 ";
            int number;
            Console.Write("Please enter a value between the range of 10 and 50: ");
            Console.WriteLine("Value entered: " + Console.ReadLine() + " - " );
            number = int.Parse(input);
            return number;
            
        }// End of PromptforInput

        static string CheckRange(int inValue)
        {
            //bool validInput;
            if (inValue < 10 || inValue > 50)
                return "UnAcceptable";
            else
                return "Acceptable";
        }//End of CheckRange

    }// End of class
}//End of namespace

Recommended Answers

All 6 Replies

Your CheckRange method works just fine. How ever I don't think your printing the results of that to the console. You are not doing anything with the information that CheckRange is providing.

I see this now but I can't figure out how to get it to print. I had it printing out earlier but it was always coming back false. Now I did some changing around in the code and am "brain cramped" on how to get it to print.
Any suggestions?
Thanks

You mean printing on the console (display).?
you can use the placeholder that console.writeline("{0,20}",value) provides.
you can arrange it in a sequence using the above, or the other way round is using \t and \n.

This seems to work the way you expect it too now. The real problem here is that the way you were handling input and output was confusing and hard to extend. You should look into improving the way you design your applications. I may rewrite this quickly to show you how I might do it. Not saying my way is best but it couldn't hurt to see things done a bit cleaner.

Anyways here is a hacked version of your code.

using System;
using System.Collections.Generic;

public class MyClass
{
static void Main()
    {
        bool choice = true;
        char character = ' ';
        int number = 0;
        string validInput;

        DisplayInfo();
        
        while (choice)
        {
            number = PromptForInput(ref character);
            validInput = CheckRange(number);
	    ShowOutput(number, validInput);
            choice = GetInput();
        }//End of while loop
   }// End of Main
        
    static void DisplayInfo()
    {
        Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***");
        Console.WriteLine("***  This program anables you to enter as    ***");
        Console.WriteLine("***  many values as you wish....Between the  ***");
        Console.WriteLine("***  range of 10 and 50.                     ***");
        Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***");
        Console.WriteLine();

        //Console.Read();  
    }// End of DisplayInfo

    static bool GetInput()
    {
        char input;
        bool choice;

        Console.Write("\nWould you like to enter another value?" +
                       "\nPlease enter y for yes or any other letter for no:");
        input = char.Parse(Console.ReadLine());
        Console.WriteLine();

        switch (input)
        {
            case 'y':
            case 'Y': choice = true;
                break;
            default: choice = false;
                break;
        }
        return choice;
        }//end of GetInput

    
    public static int PromptForInput(ref char character)
    {
        string input = " 1 ";
        int number;
        Console.Write("Please enter a value between the range of 10 and 50: ");
		input = Console.ReadLine();
        number = int.Parse(input);
        return number;
        
    }// End of PromptforInput
	
	public static void ShowOutput(int numberEntered, string isValid)
	{
		Console.WriteLine("Value entered: {0} - {1}", numberEntered, isValid);
	}
	
    static string CheckRange(int inValue)
    {
        //bool validInput;
        if (inValue < 10 || inValue > 50)
            return "UnAcceptable";
        else
            return "Acceptable";
    }//End of CheckRange

}// End of class

On another note: PromptForInput(ref char character), the parameter character is never used in the method, so why are you passing it?

Here is a cleaner version in my own opinion.

using System;
using System.Collections.Generic;

public class MyClass
{
    static bool isRunning = true;
    static int minNumber = 10;
    static int maxNumber = 50;
    static int userInput;
    static bool valueInRange;
	
    public static void Main()
    {
	DisplayInfo();
		
	while(isRunning)
	{
            userInput = GetInput();
	    valueInRange = InRange(userInput, minNumber, maxNumber);
	    DisplayOutput(userInput, valueInRange);
	    isRunning = ContinueProgram();
	}
    }
	
    public static void DisplayInfo()
    {
        Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***");
        Console.WriteLine("***  This program Enables you to enter as    ***");
        Console.WriteLine("***  many values as you wish....Between the  ***");
        Console.WriteLine("***  range of {0} and {1}.                     ***", minNumber, maxNumber);
        Console.WriteLine("***  *** *** *** *** *** *** *** *** *** *** ***\n");
    }
	
    public static int GetInput()
    {
	Console.Write("\nEnter value in range ({0} to {1}): ", minNumber, maxNumber);
	string input = Console.ReadLine();
	return int.Parse(input);
    }
	
    public static void DisplayOutput(int userInput, bool valueInRange)
    {
	if(valueInRange)
	{
            Console.WriteLine("You entered a valid number of {0}.", userInput);
	}
	else
	{
	    Console.WriteLine("Invalid number please try again.");
	}
    }
	
    public static bool ContinueProgram()
    {
        Console.Write("\nWould you like to enter another value?" +
	              "\nPlease enter y for yes or any other letter for no: ");
	char input = char.Parse(Console.ReadLine());
	switch(input)
	{
	    case 'y':
	    case 'Y':
	        return true;
	    default:
		return false;
	}
    }
	
    public static bool InRange(int value, int min, int max)
    {
	if(value < min || value > max)
	{
	    return false;
	}
	else
	{
	    return true;
        }
    }
}

Thank you for posting your version of the code. Being new to C# I am sure that my way of handling input lacks some to be desired and looking at how other people do their code really helps me a lot.
Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.