C# Mono reading input

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C# Mono reading input

Postby Ram » Fri Mar 19, 2010 9:30 am

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: Select all
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.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Ram » Fri Mar 19, 2010 4:09 pm

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.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Jyde » Sat Mar 20, 2010 2:11 pm

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.
Jyde
 
Posts: 82
Joined: Mon May 21, 2007 10:09 pm

Postby Ram » Sat Mar 20, 2010 5:43 pm

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.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Bazza » Sat Mar 20, 2010 6:28 pm

Hi Ram...

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

Try this for starters:-

Code: Select all
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...
User avatar
Bazza
LXF regular
 
Posts: 1476
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Ram » Sat Mar 20, 2010 6:57 pm

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

Code: Select all
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.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Hudzilla » Sat Mar 20, 2010 8:15 pm

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 :)
User avatar
Hudzilla
Site admin
 
Posts: 265
Joined: Mon Apr 04, 2005 11:52 am
Location: LXF Towers

Postby shinso » Sat Mar 20, 2010 9:47 pm

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

Code: Select all
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.
shinso
 
Posts: 13
Joined: Wed Mar 10, 2010 9:47 pm
Location: UK

Postby Ram » Sun Mar 21, 2010 1:28 am

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.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Jyde » Sun Mar 21, 2010 1:12 pm

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.
Jyde
 
Posts: 82
Joined: Mon May 21, 2007 10:09 pm

Postby dandnsmith » Mon Mar 22, 2010 8:09 am

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
dandnsmith
LXF regular
 
Posts: 314
Joined: Fri Apr 15, 2005 10:01 am
Location: Berks, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron