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. ");
}   
4
  • 2
    Why can't you do if("string" == "string") ? Commented Jul 7, 2015 at 23:36
  • you can also declare string local variable for odd and even. Commented Jul 7, 2015 at 23:47
  • @deathismyfriend There is already existing question on ways to compare strings - stackoverflow.com/questions/44288/…... 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... Commented Jul 8, 2015 at 0: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. Commented Jul 8, 2015 at 0:26

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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. ");
}  

2 Comments

This can bug if you type in Even instead of even. Also this is exactly what i posted above except for the modification i have.
Ah, I hadn't refreshed my browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.