C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Number Limit Posted by uchetex on 28 Sept 2003 at 3:22 PM
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?
Report
Re: Number Limit Posted by emaino on 28 Sept 2003 at 4:48 PM
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.

Eric Maino
GVSU Microsoft SA

Report
Re: Number Limit Posted by uchetex on 29 Sept 2003 at 6:56 AM
: 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.
:
: Eric Maino
: GVSU Microsoft SA
:
:

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("\n\nGreetings and welcome to MIS225 - Week 5\n\n\n");

do
{


Console.Write("\n\nEnter 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("\n\n\nThanks 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("\n\n\n");

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
Report
Re: Number Limit Posted by emaino on 29 Sept 2003 at 4:10 PM
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.
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("\nI am sorry but you have entered an incorrect score.\nPlease enter a score between 0 and 100.\n\n");
			return GetScore(Count--);
		}
		else 
			return s;
	}
	catch (Exception e) 
	{
		Console.WriteLine("\nI am sorry but you have not entered a proper value\n");
		return GetScore(Count --);
	}
}


Eric Maino
GVSU Microsoft SA

Report
Re: Number Limit Posted by uchetex on 30 Sept 2003 at 8:28 AM
This message was edited by uchetex at 2003-9-30 8:28:47

: 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.
:
: 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("\nI am sorry but you have entered an incorrect score.\nPlease enter a score between 0 and 100.\n\n");
: 			return GetScore(Count--);
: 		}
: 		else 
: 			return s;
: 	}
: 	catch (Exception e) 
: 	{
: 		Console.WriteLine("\nI am sorry but you have not entered a proper value\n");
: 		return GetScore(Count --);
: 	}
: }
: 

:
: Eric Maino
: GVSU Microsoft SA
:
:

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





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.