Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C# Mono reading input

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1661
Location: Guisborough

PostPosted: Fri Mar 19, 2010 9:30 am    Post subject: C# Mono reading input Reply with quote

I have the following routine to catch input from the keyboard, if the key is not a Y or y print of a msg to the user.

Code:

string reset = Console.ReadLine();
    if ((reset != "y") || (reset != "Y")) {
        Console.WriteLine("MSG for user" ;
        return;
   }



A key of Y or y still prints the user msg.

Any idea where I've gone wrong...
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1661
Location: Guisborough

PostPosted: Fri Mar 19, 2010 4:09 pm    Post subject: Reply with quote

I've rewrote the whole method, now this if loop rountine checks for Y/y instead of not Y/y and prints the user msg in the else part.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Jyde



Joined: Mon May 21, 2007 11:09 pm
Posts: 82

PostPosted: Sat Mar 20, 2010 2:11 pm    Post subject: Reply with quote

Don't know if you are still interested, but it should've been an && instead of ||... otherwise it will always be true.

CJ
_________________
Cheers
________________________________
PCLinuxOS, KDE, Firefox, on
AMD Athlon XP, NVidia Quadro FX
SB X-Fi XtremeMusic, Live! Vista IM Pro Cam, and beer.
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1661
Location: Guisborough

PostPosted: Sat Mar 20, 2010 5:43 pm    Post subject: Reply with quote

I only wanted to test if it was either (not a capital Y) or (not a lowercase y)

|| = or " either "
&& = and " both "

As I said, I rewrote the whole method and the test routines.

My initial test was checking for a N or n. Which was not to reset and exit. Problem with that was if you accidentally hit another key and pressed enter you'd have reset. So I thought I'd test for not Y or not y but it failed to work has I'd expected.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1462
Location: Loughborough

PostPosted: Sat Mar 20, 2010 6:28 pm    Post subject: Reply with quote

Hi Ram...

Firstly you have a bracket missing in "Console.WriteLine(... ")".

Try this for starters:-

Code:
string reset = Console.ReadLine();
    if ((reset == "y") || (reset == "Y")) {
        Console.WriteLine("You pressed a Y key...") ;
    else
        Console.WriteLine("You pressed any other key...") ;
    return;
    }


Hope this helps as an idea... :)

Watch for wordwrapping.
_________________
73...

Bazza, G0LCU...

Team AMIGA...
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1661
Location: Guisborough

PostPosted: Sat Mar 20, 2010 6:57 pm    Post subject: Reply with quote

Cheers Bazza, here is the code I settled on, after the re-write.

Code:

string reset = Console.ReadLine();
if ((reset == "y") || (reset == "Y")) {
   File.Delete("MyFile.txt");
} else { 
  PrintUsage();
  return;
}


ps The initial code was not a cut n paste, just a typo on posting in a rush - I was still late for work
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Hudzilla
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 265
Location: LXF Towers

PostPosted: Sat Mar 20, 2010 8:15 pm    Post subject: Reply with quote

Hey!

Just in case someone stumbles upon this thread later and isn't sure what the solution is, Jyde is correct: it's a simple logic error, mixing up && and ||.

- Imagine you went to a zoo.
- You want to see the lions and the tigers.
- You say to the zoo keeper "show me all the animals that are lions and tigers".
- You see the lions and the tigers.
- Now imagine that zoo keeper was a computer.
- You say the same thing.
- It looks at every animal. Is this one a lion? No. Is this one a lion? Yup. Is it also a tiger? No, it's only a lion. Ignore it.
- Etc.

Hopefully you can see that computers understand "and" and "or" differently to us: you want to see animals that are lions *or* tigers, not animals are that are lions *and* tigers.

You have the opposite problem: you're using "or" like it was "and". In your code, if you replace "y" with "Bob" and "Y" with Sam, it might make more sense: "if I am not Bob or I am not Sam, then print a message." If you are Bob, then you're not Sam, so the message prints. If you're Sam, then you're not Bob, so the message prints. In fact, unless you manage to magically type "y" and "Y" with a single key press, your condition won't ever work.

You want &&: "If I am not Bob and I am not Sam, then print a message."


Paul

PS: I'm a great believer in checking that something is true rather than checking that something is false - your new code is better, IMHO Smile
Back to top
View user's profile Send private message Visit poster's website
shinso



Joined: Wed Mar 10, 2010 9:47 pm
Posts: 13
Location: UK

PostPosted: Sat Mar 20, 2010 9:47 pm    Post subject: Reply with quote

Am I correct in thinking you could also have done it like this?

Code:

string reset = Console.ReadLine();
if (reset.ToUpper() == "Y")  {
   File.Delete("MyFile.txt");
} else {
  PrintUsage();
  return;
}


Rather than checking for lower and upper case, I mean.
_________________
I meant to do that.
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1661
Location: Guisborough

PostPosted: Sun Mar 21, 2010 1:28 am    Post subject: Reply with quote

Sorry Jyde, you where correct. With Paul analogy I can see the error in my thinking.. My initial test was for reset == N or reset == n, on reversing the logic to, reset not equal N or reset not equal n I should have reversed the or to and.

Sorry again, I still did see it with your post.


Yes, Shinso you are correct I could and I was trying to do it that way but was getting complier errors, which I failed to resolve hence the use of the long winded way of testing for either.


Thanks Paul for analogy.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Jyde



Joined: Mon May 21, 2007 11:09 pm
Posts: 82

PostPosted: Sun Mar 21, 2010 1:12 pm    Post subject: Reply with quote

Ram wrote:
Sorry Jyde, you where correct.
...
Sorry again, I still did see it with your post.
...


No need to apologise at all. I shall shamefully admit that, even after some 20 years of sporadic programming, I can still waste indecent amounts of time on a == / = beginners blooper.

One thing that has always helped me in and/or logic is from philosophical algebra:

X and Y ===
!(X or Y)

Or vice versa. If you understand that - i.e. how to make an And into an Or, or vice versa - you tend not to have these issues.

Anyway, I digress... good you have it solved.

CJ
_________________
Cheers
________________________________
PCLinuxOS, KDE, Firefox, on
AMD Athlon XP, NVidia Quadro FX
SB X-Fi XtremeMusic, Live! Vista IM Pro Cam, and beer.
Back to top
View user's profile Send private message
dandnsmith
LXF regular


Joined: Fri Apr 15, 2005 11:01 am
Posts: 299
Location: Berks, UK

PostPosted: Mon Mar 22, 2010 8:09 am    Post subject: Reply with quote

Quote:
One thing that has always helped me in and/or logic is from philosophical algebra:

X and Y ===
!(X or Y)

Or vice versa. If you understand that - i.e. how to make an And into an Or, or vice versa - you tend not to have these issues.


X and Y === !(!X or !Y)
surely
_________________
Derek
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast