1

I have a user's response regarding their age stored as int age;. I then ask the user to divide their age by some number and they input that number into my console. That number is stored as int rep;. From there, I want to ask if int rep; is an even or odd number. I realize that you cannot use a string in an if () statement. However, I have not be able to formulate my question properly to find a solution/post on the web that will help me understand how to check if a user's input that I store as a string can be compared to an answer that I expect.

To sum: is there an equivalent syntax for if () for a string?

//Second task
int age;
int rep;


Console.WriteLine ("How old are you? ");
age = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine ("What is the your age divided by your first number submitted in the previous question? ");
rep = Convert.ToInt32 (Console.ReadLine ());
if (rep == age / num01 ) {
    Console.WriteLine ("That is correct. Proceed to the next question. ");
} else
{
    Console.WriteLine ("That is incorrect. Start over. ");
}
Console.WriteLine ();

//Third task
string ans;

Console.WriteLine ("Is your answer to the previous question an even or odd number? ");
ans = Console.ReadLine ();
if (rep % 2 == 0 && ans == even)
{
    Console.WriteLine ("That is correct. ");
}
if (rep % 2 == 1 && ans == odd) 
{
    Console.WriteLine ("That is correct. ");
}   
DavidG
  • 113,891
  • 12
  • 217
  • 223
bmeredit
  • 83
  • 5
  • 2
    Why can't you do if("string" == "string") ? – deathismyfriend Jul 07 '15 at 23:36
  • you can also declare string local variable for odd and even. – M.kazem Akhgary Jul 07 '15 at 23:47
  • @deathismyfriend There is already existing question on ways to compare strings - http://stackoverflow.com/questions/44288/differences-in-string-compare-methods-in-c-sharp... And `==` is not the best option in most cases related to user input (as you tried to say in your answer). The rest of this question is somewhat less interesting - "how to use `if`" is almost off-topic for so... – Alexei Levenkov Jul 08 '15 at 00:21
  • @AlexeiLevenkov Yes there are other questions but i didnt do a search for them. Also i never said that it was the best way to use ==. I will add the better case to use in my question below. – deathismyfriend Jul 08 '15 at 00:26

2 Answers2

2

In your case i would change

ans = Console.ReadLine();

To this.

ans = Console.ReadLine().ToLower();

Then change your ITEs to this.

if (rep % 2 == 0 && ans == "even") {//code here.}
else if (ans == "odd") {//code here.} // This should also be else if not just if. With it being else if the extra check rep % 2 == 1 is not needed.

Alternatively for a better string comparison you should do this.

ans = Console.ReadLine();
if (rep % 2 == 0 && ans.Equals("even", StringComparison.OrdinalIgnoreCase)) {//code here.}
else if (ans == ans.Equals("odd", StringComparison.OrdinalIgnoreCase)) {//code here.} // This should also be else if not just if. With it being else if the extra check rep % 2 == 1 is not needed. 

The above will check for a comparison and ignore the case of the string so you do not need to use the ToLower and the problems with string comparisons using the == should not arise.

Thanks to Alexei Levenkov for pointing this out.

You can also check this out for future references on string comparisons. https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx

deathismyfriend
  • 2,182
  • 2
  • 18
  • 25
  • Great explanation. Sorry for the delay on accepting your solution. I'll have to take a look at the links for string comparisons. Thank you once again! – bmeredit Jul 08 '15 at 16:22
0

https://msdn.microsoft.com/en-us/library/system.string.op_equality%28v=vs.110%29.aspx

The == operator works on strings, so below is the modifications necessary to make your code work

//Second task
int age;
int rep;


Console.WriteLine ("How old are you? ");
age = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine ("What is the your age divided by your first number submitted in the previous question? ");
rep = Convert.ToInt32 (Console.ReadLine ());
if (rep == age / num01 ) {
    Console.WriteLine ("That is correct. Proceed to the next question. ");
} 
else
{
    Console.WriteLine ("That is incorrect. Start over. ");
}
Console.WriteLine ();

//Third task
string ans;

Console.WriteLine ("Is your answer to the previous question an even or odd number? ");
ans = Console.ReadLine ();
//change 1
if (rep % 2 == 0 && ans == "even")
{
   Console.WriteLine ("That is correct. ");
}
//change2
if (rep % 2 == 1 && ans == "odd") 
{
    Console.WriteLine ("That is correct. ");
}  
Snark
  • 1,664
  • 14
  • 27