Jump to content

Numbers in range c#

Daniboi

Hi, i have wrote a piece of code however i cant remember how you get some data validation in. i want my numbers to stay within -10 and +10 and it should the display "invalid answer" and then ignore that user input however it just says in valid answer.

 

      bool bUserInput;
            sbyte sbNumber;

   
   if (sbNumber < -11 || sbNumber > 11)
                {
                    //the error message
                    Console.WriteLine("Your are out of range please stay between -10 and +10");

                    bUserInput = false;
                }

 

Link to comment
Share on other sites

Link to post
Share on other sites

Errm.... Your question doesn't really make sense. But I believe the answer your looking for it the method int.TryParse(). Make sbNumber into an int not a sbyte.

 

 

This documentation should help you with it. If you have any more questions feel free to ask!

https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netframework-4.8

 

Link to comment
Share on other sites

Link to post
Share on other sites

if(number > 11)

Means number 11 will be the maximum allowed number. Same for the -11 if-statement you're running.

You're probably looking for >= / <= or simply if(number > 10).

 

I can't remember if sbyte can be compared to an int too, so be sure to double check that part too.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Ecchi said:

Errm.... Your question doesn't really make sense. But I believe the answer your looking for it the method int.TryParse(). Make sbNumber into an int not a sbyte.

 

 

This documentation should help you with it. If you have any more questions feel free to ask!

https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=netframework-4.8

 

hi,

 

yeah so i am trying to make sure that it stays within range of those numbers like the flowchart seen bellow

Datavalidation.jpg

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Daniboi said:

 

Datavalidation.jpg

Hi,

 

This piece of code should work.

 

using System;

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Number: ");
		string number = Console.ReadLine();
		bool UserInput;
		int realNum;
		
		if (int.TryParse(number, out realNum))
		{
			if (realNum <= -10 || realNum >= 10)
			{
				//the error message
				Console.WriteLine("Your are out of range please stay between -10 and +10");
				UserInput = false;
			}
		}
	}
}

 

Hope this makes sense.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Ecchi said:

Hi,

 

This piece of code should work.

 


using System;

public class Program
{
	public static void Main()
	{
		Console.WriteLine("Number: ");
		string number = Console.ReadLine();
        //hate to ask but the use of string? why?
        
		bool UserInput;
		int realNum;
		
		if (int.TryParse(number, out realNum))
		{
			if (realNum <= -10 || realNum >= 10)
			{
				//the error message
				Console.WriteLine("Your are out of range please stay between -10 and +10");
				UserInput = false;
			}
		}
	}
}

 

Hope this makes sense.

it sorta does

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, Daniboi said:

it sorta does

 

public class Program
{
  static void Main()
  { 
    Console.WriteLine("Please enter a number in the range [-10, 10]");
    int userNum;
    
    while (true)
    {
      string userInput = Console.ReadLine();
      
      if (Int32.TryParse(userInput, out userNum))
      {
        if (InputIsInRange(userNum, -10, 10))
          break;
        
        else
          Console.WriteLine("Invalid Number");
      }
      
      else
        Console.WriteLine("Input is not a number. Please input a number between -10 and 10.");
    }
    
    // If we're here, we're clear to do stuff with userNum.
  }
  
  bool InputIsInRange(int num, int lowerInclusive, int upperInclusive) => ((num >= lowerInclusive) && (num <= upperInclusive));
}


This appears to be homework. I would not recommend copying my use of expression bodied members (the "=>" syntax on the InputIsRange declaration) because it may raise a red flag to your teacher.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/3/2020 at 5:31 PM, straight_stewie said:

 


public class Program
{
  static void Main()
  { 
    Console.WriteLine("Please enter a number in the range [-10, 10]");
    int userNum;
    
    while (true)
    {
      string userInput = Console.ReadLine();
      
      if (Int32.TryParse(userInput, out userNum))
      {
        if (InputIsInRange(userNum, -10, 10))
          break;
        
        else
          Console.WriteLine("Invalid Number");
      }
      
      else
        Console.WriteLine("Input is not a number. Please input a number between -10 and 10.");
    }
    
    // If we're here, we're clear to do stuff with userNum.
  }
  
  bool InputIsInRange(int num, int lowerInclusive, int upperInclusive) => ((num >= lowerInclusive) && (num <= upperInclusive));
}


This appears to be homework. I would not recommend copying my use of expression bodied members (the "=>" syntax on the InputIsRange declaration) because it may raise a red flag to your teacher.

And they usually have software to check if it's plaguerised, plus it's not really learning if copy/paste without understanding it :P

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, fringie said:

And they usually have software to check if it's plaguerised, plus it's not really learning if copy/paste without understanding it :P

ah no so they actually told me to it this way

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Erik Sieghart said:

There's some fundamental understanding issues with your code. I would read @straight_stewie's code to reference the individual parts that are different from yours. These misunderstandings are:

  • The use of sbyte instead of int.
  • Not obtaining the input from the user.
  • Believing that the input can be stored as an integer.

so i was told sbyte and this is only a very small bit of my code

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/5/2020 at 5:35 AM, Erik Sieghart said:

There's some fundamental understanding issues with your code. I would read @straight_stewie's code to reference the individual parts that are different from yours. These misunderstandings are:

  • The use of sbyte instead of int.
  • Not obtaining the input from the user.
  • Believing that the input can be stored as an integer.

Don't associate me without that code

Image result for pepehands gif

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×