13 Replies - 1350 Views - Last Post: 15 October 2009 - 07:46 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Checking User String is Numeric

Posted 13 October 2009 - 11:53 AM

Well...I hope JackOfAllTrades doesn't close my post this time...Last time because of the TryParse(), my post got shut.

I tried the method a couple of you guys told me but it did not work.
This is what I tried: TryParse, While and If statements and the do-while statement too.

The problem is 1) to request TWO NUMBERS FROM THE USER, and then check those two numbers to see if they are numeric strings, and if they are some junks, 2) display a message that warns the user from entering the junk and he or she must enter numbers. 3) If the user keeps entering letters, characters or junk, the warning display message will keep popping up and keep telling the user to enter numbers until he or she does, then the message disappears.

NOTE: THIS IS NOT HE HOMEWORK...THE INSTRUCTOR DIDN'T REQUEST THIS. But as SixOfEleven stated, "When writing a program, the program must be robust and we don't want the program crash if anything happens.", and I think this is true. So in order to write robust codes, I must first do something to check and see what the user does is proper and accurate. So that comes the question above. The code or homework I did got shut, which is HERE.

I hope you guys could help me out because you guys have a lot of experience about this.

*** MOD EDIT: Took "credit" for closing the previous topics, so as to not besmirch SixOfEleven's good name ***

This post has been edited by JackOfAllTrades: 13 October 2009 - 01:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Checking User String is Numeric

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,254
  • Joined: 12-June 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 12:20 PM

It was answered in three areas... that's why your post was shut down.

What have you tried so far? Clearly you are doing something wrong.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 12:31 PM

did you even look up what TryParse() does? That should have been all of an answer that you needed.

http://msdn.microsof...y/f02979c7.aspx
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 01:36 PM

Before I close this YET AGAIN, because I totally admire and approve of your desire to create robust and cirrect code, I'm going to give you one last chance.

What is your current code? What is the problem with it? Is eclipsed4utoo's link to the documentation insufficient? What about the explanation given to you in this thread? You need to explain why the explanations you've already received do not meet your needs!
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 04:31 PM

I do know how the TryParse() works. And not worry about it. The instructor did tell us to use the MSDN as the source reference for programming. But what I could say is MDSN is not good for beginner because it contains lots of codes and some of them we have not yet learned. SO it might be useless.

I am not at school right now. SO I might not be able to provide the correct codes as I did in school. But here is what I did.

// To display what the program does, so I used Console.Write();
Console.Write("This is modulus program");

// Then, I have to prompt user for inputs
// and then do the calculation and get the result
// of the remainder or more accurate, the result
// of modulus. But before doing so, as you guys
// suggested, I should use TryParse(), so first
// I will declare two int variables, which would be used 
// to store the value if TryParse() successfully Parse
// the numbers.
Console.Write("\nPlease enter your first number: ");
int N1, N2;
bool number1 = int.TryParse(Console.ReadLine(), out N1);
Console.Write("\nPlease enter the second number: ");
bool number2 = int.TryParse(Console.ReadLine(), out N2);

// Now if the user entered some junk. ex letters, characters, 
// TryParse() wouldn't parse successfully because they are not
// numeric strings. Hence I used the do-while loop to check the condition
// and if the user keeps entering junks, the loop will keep looping or keep
// asking the user for the correct input, if he or she does, then the loop
// will stop and then follow the regular pattern
// of the flow to calculate the result. But here is what I got stuck with.

do
{
   if(number1 && number2)
				 break;
   
   Console.Write("Please enter numbers, not letters or characters");
   Console.Write("Please re-enter the first number: ");
   Console.Write("Please re-enter the second number: ");
   bool number1 = int.TryParse(Console.ReadLine(), out N1);
   bool number2 = int.TryParse(Console.ReadLine(), out N2);
   

}while(number1 || number2 == false)

// Normal process of calculating the remainder
if(N2 % N1 == 0)
{
  Console.Write("\nThe first number is divisible by the second number.");
}else{
  Console.Write("\nThe first number is not divisible by the second number.");



I got errors and knowing that this is incorrect, but how can I fix it or is there any other way to do it? I cannot come up with a solution.
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 08:28 PM

You are trying too hard to write short code. You should use more variables and give them better names. One problem you have in you code is that you are redefining number1 and number2 inside the while loop, that will cause an error. This is a bit of a refactored version of your program. It shows the principals behind creating bullet proof code, with comments.

Console.WriteLine("This is modulus program"); // You should have used WriteLine here and forget about the \n

int value1, value2;	 // variables to hold the converted values
bool result1 = false;   // variable to hold the result of the conversion of value1
bool result2 = false;   // variable to hold the result of the conversion of value2

do
{
	string inputString1; // the string for inputting the first item
	string inputString2; // the string for inputting the second item

	Console.WriteLine("Please enter your first number: "); // Prompt for the first number and read it in
	inputString1 = Console.ReadLine();	

	Console.WriteLine("Please enter the second number: "); // Prompt for the second number and read it in
	inputString2 = Console.ReadLine();

	result1 = int.TryParse(inputString1, out value1);  // Try and convert the first number
	result2 = int.TryParse(inputString2, out value2);  // Try and convert the second number

	if (!result1 || !result2) // If either the first conversion failed or the second conversion failed print a warning
	   Console.Write("Please enter numbers, not letters or characters");   

}while(result1 == false || result2 == false); // While either result is false repeat the process

// Normal process of calculating the remainder
if(value2 % value1 == 0)
{
  Console.Write("\nThe first number is divisible by the second number.");
}else{
  Console.Write("\nThe first number is not divisible by the second number.");


Was This Post Helpful? 0
  • +
  • -

#7 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Checking User String is Numeric

Posted 13 October 2009 - 09:41 PM

I will try to get this compiled tomorrow and see if that works. Thanks a bunch for helping me out with this. And I have been thinking about this or figuring out how to do this for more than a few days.

I do know how to use the do-while loop, but cannot really invoke the potential of its usage.

SO Here comes a question or come a few questions:
- Is it possible to declare the string type variables outside of the do-while loop? I can't quite remember the principal of the scope rule.
- I really didn't realize that the not logical operator can be used in this case. So if the bool value is false after the TryParse doesn't parse successfully, you use the not logical operator to force it to be true in the boolean expression within the if condition and print the warning message, right?

Darn if somebody could have hinted at that(the logical operator), I think the work could have been done easier. But I was not really able to figure out that we could use the not logical operator. You are so smart and prudent SixOfEleven


But if there isn't a break to break it out of the loop? Won't it loop infinitely?
Since if the condition is false, the loop will keep looping. But what about if I wanna change the statement, I meant the contents within the Console.Write(" "); Instead of "Please enter the first number, I would like to say something like "Please re-enter the first number". In this case, it is more sensible that the user knows he or she did something wrong, so he or she must correct the mistake. Is there a way to do that?
Was This Post Helpful? 0
  • +
  • -

#8 mclang  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 11-January 08

Re: Checking User String is Numeric

Posted 14 October 2009 - 01:39 AM

It won't loop indefinitely because } while(result1 == false || result2 == false); stops the looping when BOTH result1 and result2 are true. On further note, you can declare inputString1 and inputString2 outside of the loop. And you can do it something like this:
Console.WriteLine("This is modulus program");
int value1, value2;
bool result1 = false, result2 = false;
string inputString1, inputString2;

Console.WriteLine("Please enter your first number: ");
inputString1 = Console.ReadLine();	
Console.WriteLine("Please enter the second number: ");
inputString2 = Console.ReadLine();

result1 = int.TryParse(inputString1, out value1);
result2 = int.TryParse(inputString2, out value2);

while(result1 == false || result2 == false) {
	Console.WriteLine("One or Both your inputs were invalid. Try again!");

	Console.WriteLine("Please re-enter your first number: ");
	inputString1 = Console.ReadLine();	
	Console.WriteLine("Please re-enter the second number: ");
	inputString2 = Console.ReadLine();
	result1 = int.TryParse(inputString1, out value1);
	result2 = int.TryParse(inputString2, out value2);
}
/*** Rest of your program logic ***/


I haven't compiled this, so there may be errors in it Hope it helps though!
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Checking User String is Numeric

Posted 14 October 2009 - 05:48 AM

Quote

Is it possible to declare the string type variables outside of the do-while loop? I can't quite remember the principal of the scope rule.

Of course it's possible. It's not necessarily a good idea, though, because you don't NEED the strings outside of the scope of the do-while loop.

Quote

I really didn't realize that the not logical operator can be used in this case.

Huh? TryParse() returns bool. bool is either true or false, which means you use logical operators to test the return value.

I agree with SixOfEleven regarding your OCD in trying to make short code. If you don't understand some of these basic principles of programming, you're jumping ahead of yourself in trying to make compact, robust code. Learn to make rudimentary working code before optimizing it. Google for "premature optimization"...you're taking it to a whole new extreme.
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Checking User String is Numeric

Posted 14 October 2009 - 08:25 AM

Well...You guys might find this reasonable and not a big deal. But I think I still don't really understand this, and hope you guys could enlighten me with concept.

result1 = int.TryParse(inputString1, out value1);  // Try and convert the first number
	result2 = int.TryParse(inputString2, out value2);  // Try and convert the second number

	if (!result1 || !result2) // If either the first conversion failed or the second conversion failed print a warning
	   Console.Write("Please enter numbers, not letters or characters");  

}while(result1 == false || result2 == false); // While either result is false repeat the process



Well, for variables, when you assign the a value to it, the value will be permanent unless you change the value inside it. For instance, int x = 30; so if you never change it, x will always be 30 no matter what. Unless you do something like int x = 35; and then x changes to 35, and the value 30 is lost. Is it correct?

Well...for the if(!result1 || !result2); I am quite confused with it. As we know, only the condition is true do the statements work in the body of the if, do-while, while and so forth...so if TryParse() returns false, the result1 and result2 will be assigned to the if condition because the program does one thing at a time, after getting the input from the user and TryParse it, if TryParse couldn't pass it, it returns false and the next statement is if statement, so the program proceeds with the if statement. So SixOfEleven uses the logical unary ! not operator to reverse the value of the bool false to true, so if TryParse returns false, the condition in the if statement is always true, so the Console.Write() is executed. But here is what my question is:

Since result1 and result2 has been reversed to false because of the if statement, so when it comes to the while loop, the !result is true, so while(true == false) cannot equal to false...I just don't get it because once the variable or the value has been assigned, it is fixed unless you change it, or !result is reversed to true, the bool true value is fixed, but since we didn't change it back to false, how can the while loop still work?
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Checking User String is Numeric

Posted 14 October 2009 - 10:05 AM

No no no...this:

Quote

So SixOfEleven uses the logical unary ! not operator to reverse the value of the bool false
is where you're wrong. It's the logical negation operator. It doesn't change any variable, it simply takes its argument(s) and compares to the value false. If the argument is false, it returns true, and vice-versa. See here, in the spec.

if (!result1 || !result2) is the exact same as if (result1 == false || result2 == false)
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Checking User String is Numeric

Posted 14 October 2009 - 04:56 PM

View PostJackOfAllTrades, on 14 Oct, 2009 - 11:05 AM, said:

No no no...this:

Quote

So SixOfEleven uses the logical unary ! not operator to reverse the value of the bool false
is where you're wrong. It's the logical negation operator. It doesn't change any variable, it simply takes its argument(s) and compares to the value false. If the argument is false, it returns true, and vice-versa. See here, in the spec.

if (!result1 || !result2) is the exact same as if (result1 == false || result2 == false)


To further clarify. In the if statement when I do !result1 I'm not doing an assignment. When you are working with variables in a condition like in the if statement and the while statement you are just making comparisons, you are not making assignments. That fact is something that many beginners get confused about. That is why to check for equality in languages like C# they use the == operator instead of just =. Trying to make assignments inside of a condition, while perfectly legal, is bad practice because it could lead to confusion later on when you come back to your program and need to change things.

For example it is bad practice to do the following:

if (number++ > 10)
	// Do something



That would be bad practice because you are modifying the variable you are using inside of a condition. It would be much better to do the following:

if (number > 10)
	// Do something

number++;



You might save a few instructions by doing the first case but it is safer to do the second.

As JackOfAllTrades has said though. Getting the basics down first is probably what you should be working on. Once you understand the basics then work on writing robust code. Once you understand how to write robust code that will not crash the end user's computer then go for optimization and try and speed things up.
Was This Post Helpful? 0
  • +
  • -

#13 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Checking User String is Numeric

Posted 14 October 2009 - 10:22 PM

View Postkenryuakuma, on 14 Oct, 2009 - 07:25 AM, said:

Well, for variables, when you assign the a value to it, the value will be permanent unless you change the value inside it. For instance, int x = 30; so if you never change it, x will always be 30 no matter what. Unless you do something like int x = 35; and then x changes to 35, and the value 30 is lost. Is it correct?


The above explanations by JackOfAllTrades andSixOfEleven are great. I'll just provide one more example using the integers like you used in the above quote.

As we know, this changes the value of x:
x = 35;

But how about this?
int y = (x - 15);

Well, it doesn't. And that's because we don't reassign x. x is still 35 and not 35-15=20.
And the same goes for booleans as well, like mentioned in above posts:
if (!result1) { } // no assigns here: doesn't change the value
result1 = !result1 // reassign: changes the value
Was This Post Helpful? 0
  • +
  • -

#14 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Checking User String is Numeric

Posted 15 October 2009 - 07:46 PM

Awesome! Immaculate explanations that a lot of you have offered me. Thanks for all of you helping me to make this clear. Now I always include or use the same principle that SixOfEleven told me in my real programming practice. It is simple but is really effective especially track down those users who enter junk.

Again thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1