Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6228189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:20:46+00:00 2026-05-24T09:20:46+00:00

Ii am new to c# and trying to create a validation through use of

  • 0

Ii am new to c# and trying to create a validation through use of a while loop with the range from 0 – 5, my problem is that when this range is used it also accepts string characters, how can I stop this and only allow integers 0 – 5?

my code so far

   static void Main()

    {
        int Rateing1, Rateing2;

         Console.Write("Please rate from 0 - 5:   ");
         Rateing1 = valid_rating();

         Console.Write("\nPlease rate from 0 - 5:    ");
         Rateing2 = valid_rating();

         Console.WriteLine("\nRateing1 is {0}", Rateing1);
         Console.WriteLine("Rateing2 is {0}", Rateing2);
     }

    static int valid_rating()

          {
            int rating;

            int.TryParse(Console.ReadLine(), out rating);
                while (rating < 0 || rating > 5)
                     {
                       Console.Write("\nInvalid Input, please input an integer from 0 - 5");
                       Console.Write("\nPlease enter new rating:   ");
                       int.TryParse(Console.ReadLine(), out rating);
                     } 
            return rating;
          }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:20:47+00:00Added an answer on May 24, 2026 at 9:20 am

    Change your loop to this:

    while (!int.TryParse(Console.ReadLine(), out rating) || rating < 0 || rating > 5)
    {
        Console.WriteLine();
        Console.WriteLine("Invalid Input, please input an integer from 0 - 5");
        Console.Write("Please enter new rating:   ");
    }
    

    That is, you want the user to enter input again if any of the following is true:

    • They entered an invalid number.
    • The number they entered is less than 0.
    • The number they entered is greater than 5.

    Your problem was that you were not using the value returned by int.TryParse (which is a bool indicating success or failure); you were simply using the value written to rating which, in the case of failure, is 0 (and !(0 < 0)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a new project in NetBeans PHP from existing sources. When
I am trying to create a new branch using the API, and have used
I am trying to create a new custom validation in which I can compare
I'm new to js. trying to create mini validation function which will check fields
When trying to create a new team project with any of the default project
I'm trying to create a new object of type T via its constructor when
I am trying to create a new dict using a list of values of
I am trying to create a new Element in my javascript code and append
I am trying to create new Event objects to be persisted in the database
I'm trying to create a new log file every hour with the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.