Click here to Skip to main content
14,829,981 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
The problem is if I enter 0,The instuctions appear but when I exit them,I find out that the value has entered Invalid also.I do not want that.What am I doing wrong and what should I do?


C#
Console.WriteLine("Choose what type of attack");
           decisiontr = Console.ReadLine();
           if (decisiontr == "1" || decisiontr == "2" || decisiontr == "3" || decisiontr == "4" || decisiontr == "5" || decisiontr == "6" ||
               decisiontr == "7" || decisiontr == "8" || decisiontr == "9")
           {
               decision = int.Parse(decisiontr);
           }
           else if (decisiontr == "0")
           { instructions(); }
           if (decisiontr != "1" && decisiontr != "2" && decisiontr != "3" && decisiontr != "4" &&
                   decisiontr != "5" && decisiontr != "6" && decisiontr != "7" && decisiontr != "8" && decisiontr != "9" && decisiontr!="0")
           { HeroATTAX.Invalid(); }
           Console.Clear();
           HeroATTAX attack = new HeroATTAX();
Posted
Comments
[no name] 11-Aug-14 12:08pm
   
What is decisiontr after you return from instructions()?
swapnil999 11-Aug-14 12:21pm
   
It's the same.
I only press enter.
Dilan Shaminda 11-Aug-14 12:30pm
   
your code works fine.Just check whether you are assigning some value to decisiontr variable inside your instroctions() funtion

1 solution

What you are doing wrong? You write code in a monstrous, inaccurate way, hard-code immediate constants and don't use the debugger to solve your simple problem. You also give bad names to variables.

For example, your first condition could look better:
C#
const byte decisionMin = 1;
const byte decisionMax = 9;

//...

string decisionInput = Console.ReadLine();
byte decision;
if (!byte.TryParse(out decision))
   return; // or do something else, say, ask to repeat input
if (decisionMin <= decision && decision <= decisionMax)
   //...

And so on…

Just write everything 1) accurately; 2) using your brain; 3) understanding what you are doing in every line; and you will get your results. In even the slightest concerns about your runtime, always use the debugger.

—SA
   
v5
Comments
Richard Deeming 11-Aug-14 13:02pm
   
Your code has a few problems:

* Console.ReadLine is a method group. You could assign it to a delegate variable, but you can't assign it to an integer.
* Calling Console.ReadLine() returns a string; you still can't assign it to an integer.
* You can't compare a string to an integer.

:)
Sergey Alexandrovich Kryukov 11-Aug-14 14:15pm
   
Damn! This is yet another good lesson for me: never trust the code you copy; it was quite stupid of me.
Fixed. I really appreciate your help, Richard.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900