Number Limit - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Number Limit

uchetexuchetex Posts: 3Member
Does anyone have a sample code that prevents a user from entering a negative number and a number greater than 100 by displaying a message telling them to enter a positive number and a number less than 100?

Comments

  • emainoemaino Posts: 48Member
    What kind of message are you looking for? and What kind of application is this? WinForm? WebForm? Console app?

    I can easily provide a solution, just looking for a little more info.

    [size=3][italic]Eric Maino[/italic][/size]
    [b][blue]GVSU[/blue] Microsoft SA[/b]

  • uchetexuchetex Posts: 3Member
    : What kind of message are you looking for? and What kind of application is this? WinForm? WebForm? Console app?
    :
    : I can easily provide a solution, just looking for a little more info.
    :
    : [size=3][italic]Eric Maino[/italic][/size]
    : [b][blue]GVSU[/blue] Microsoft SA[/b]
    :
    :

    Ok I'm kinda new with C# below is exactly what my code looks like and it uses Console App


    using System;
    using System.Windows.Forms;
    class Week05
    {
    static void Main(string[] args)
    {

    char Grade;
    string Response;
    string Name;

    Console.WriteLine("

    Greetings and welcome to MIS225 - Week 5


    ");

    do
    {


    Console.Write("

    Enter student name: ");
    Name = Console.ReadLine();


    Grade = CalcStudentGrade();
    Console.WriteLine("{1}, your grade is {0}", Grade, Name);

    Console.Write("Enter scores for another student? ");
    Response = Console.ReadLine();

    Response = Response.ToUpper();


    } while (Response == "YES");

    Console.WriteLine("


    Thanks for using the program. See you next week!");

    }



    //---------------------------------------------------------
    //
    // CalcStudentGrade Input : Nothing
    // Output: char Grade
    //
    //---------------------------------------------------------
    static char CalcStudentGrade()
    {

    string Response;
    float Score;
    float Total;
    int Count;
    float Average;


    Count = 0;
    Total = 0;

    Response = "";
    do
    {


    Score = GetScore(Count);

    Total += Score;
    Count++;

    Console.Write("Enter another score? ");
    Response = Console.ReadLine();

    Response = Response.ToUpper();

    } while (Response == "YES");


    Console.WriteLine("


    ");

    Average = Total / Count;

    return GetGrade(Average);


    }



    //---------------------------------------------------------
    //
    // GetScore Input : int Count (Score Number for prompt)
    // Output: float Score (Between -1 and 100)
    //
    //---------------------------------------------------------
    static float GetScore(int Count)
    {

    string T;

    Console.Write("Enter score number {0}: ", Count + 1);
    T = Console.ReadLine();
    return float.Parse(T);

    //if (int.Parse(T)<1)
    //{
    //MessageBox.Show("The Number entered must not be a negative number or above 100. Please try again");
    //Environment.Exit(0);

    // }





    }




    //---------------------------------------------------------
    //
    // GetGrade Input : float Average
    // Output: char Grade
    //
    //---------------------------------------------------------
    static char GetGrade(float A)
    {

    char G;

    if (A >= 90)
    G = 'A';
    else if (A >= 80)
    G = 'B';
    else if (A >= 70)
    G = 'C';
    else if (A >= 60)
    G = 'D';
    else
    G = 'E';

    return G;

    }

    }

    So I just wanted to set a limit on the score to be entered so not to be negative number or above 100
    Thanks for your help
  • emainoemaino Posts: 48Member
    I have edited your GetScore method, to make it recursive. It will now only allow you to enter a score between 0 and 100 and it also double checks that you are entering a number. I hope this code points you in the right direction. It may not be exactly what you are looking for but I really hope it helps.
    [code]
    static float GetScore(int Count)
    {
    string T;

    Console.Write("Enter score number {0}: ", Count + 1);
    T = Console.ReadLine();

    try
    {
    float s = float.Parse(T);
    if (s < 0 || s > 100)
    {
    Console.Write("
    I am sorry but you have entered an incorrect score.
    Please enter a score between 0 and 100.

    ");
    return GetScore(Count--);
    }
    else
    return s;
    }
    catch (Exception e)
    {
    Console.WriteLine("
    I am sorry but you have not entered a proper value
    ");
    return GetScore(Count --);
    }
    }
    [/code]

    [size=3][italic]Eric Maino[/italic][/size]
    [b][blue]GVSU[/blue] Microsoft SA[/b]

  • uchetexuchetex Posts: 3Member
    [b][red]This message was edited by uchetex at 2003-9-30 8:28:47[/red][/b][hr]
    : I have edited your GetScore method, to make it recursive. It will now only allow you to enter a score between 0 and 100 and it also double checks that you are entering a number. I hope this code points you in the right direction. It may not be exactly what you are looking for but I really hope it helps.
    : [code]
    : static float GetScore(int Count)
    : {
    : string T;
    :
    : Console.Write("Enter score number {0}: ", Count + 1);
    : T = Console.ReadLine();
    :
    : try
    : {
    : float s = float.Parse(T);
    : if (s < 0 || s > 100)
    : {
    : Console.Write("
    I am sorry but you have entered an incorrect score.
    Please enter a score between 0 and 100.

    ");
    : return GetScore(Count--);
    : }
    : else
    : return s;
    : }
    : catch (Exception e)
    : {
    : Console.WriteLine("
    I am sorry but you have not entered a proper value
    ");
    : return GetScore(Count --);
    : }
    : }
    : [/code]
    :
    : [size=3][italic]Eric Maino[/italic][/size]
    : [b][blue]GVSU[/blue] Microsoft SA[/b]
    :
    :

    Emaino, That was exactly what I was looking for. But one more thing is how can the user enter a -1 to end the programm


Sign In or Register to comment.