-1

im learning and i want to make so that if the age entered is 15 it will make a special message i just started yesterday so im trying to do random things to be sure i understand but i didnt learn that yet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meme
{
class Program
{
    static void Main(string[] args)
    {
        string yourName;
        Console.WriteLine("What is your name?");
        yourName = Console.ReadLine();
        Console.WriteLine("Hello {0}", yourName);
        Console.WriteLine("What is your age?");
        int yourAge = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Hello {0}, you are {1} years old", yourName, yourAge);
        Console.WriteLine("What is your rank?");
        int yourRank;
        yourRank = Convert.ToInt32(Console.ReadLine());
        const int Y = 15;
        if ((yourAge = Y))
        {
            Console.WriteLine("NO");
        }
    }
}

}

5
  • 1
    Comparison is == not =
    – juharr
    Feb 25, 2020 at 19:37
  • yourAge = Y is your problem, think about what operator belongs here if you are trying to compare something
    – maccettura
    Feb 25, 2020 at 19:37
  • For what it's worth, you don't need the extra set of parentheses either. What you want is if (yourAge == Y)
    – Flydog57
    Feb 25, 2020 at 19:41
  • 3
    As a novice, the time to get into good habits is now. You should use int.TryParse to convert user-supplied strings to integers, because that way you have an opportunity to inform the user if they type something that cannot be converted to integer. This will then give you an opportunity to learn about loops! Feb 25, 2020 at 19:47
  • You might also wish to learn about the more modern way to interpolate a string. If you put a $ before the literal then you can do this: $"Hello {yourName} you are {yourAge} years old", which is somewhat easier to read. Feb 25, 2020 at 19:51

1 Answer 1

3

C# uses == to mean "compare for equality" and = to mean "assign value to variable or property". You've mixed them up.

Moreover, in C# (and many other languages), x = y is an expression that assigns the value of y to variable x and then produces the assigned value as a result. So if (yourAge = Y) means "assign Y to yourAge, then use the integer value assigned to make the decision in the if". But the condition of an if is required to be convertible to a bool, and int is not. Hence the error.

It would have been better to special-case this behaviour in the compiler so that accidental use of = instead of == produces a better error message. C# does so in this situation, but only this situation:

bool b = whatever;
if (b = true)

There it says, sensibly, that you are probably using = where == is intended. I wish we had made the compiler give this kind of error in more situations. (Though if (b==true) is only marginally better; the right thing to type there is if (b)!)

6
  • 2
    @RAIZE you thank people by clicking the green checkmark at the upper left of their answer. Feb 25, 2020 at 19:41
  • 1
    @maccettura: You should downvote if you believe the answer is inaccurate or unhelpful. I note that this is not a "typo" question exactly; a typo is a mistake where you know the right thing to do but your fingers do the wrong thing, and then it is hard to see. In this case the original poster, clearly a novice, is mistaken about the right thing to do. In those cases there is an opportunity for education. Feb 25, 2020 at 19:44
  • 1
    @EricLippert its helpful and accurate, I wrongly assumed the answer that popped up was someone trying to get quick points off what is likely a dupe/closed question. Your expanded answer edit makes it even better
    – maccettura
    Feb 25, 2020 at 19:46
  • 2
    @FrankJ: Prefer if(!b). The reason for the preference is: do you say "if it is snowing, let's go sledding", or "if it is true that it is snowing, let's go sledding"? Do you say "if it is not raining..." or "if it is false that it is raining..." ? Novices often develop the bad intuition that the condition of an if needs to be a comparison. We should emphasize that the condition of an if needs to be true or false, and if the value is already true or false, we're done. We don't need a comparison to true to tell us that true is true. Feb 27, 2020 at 18:47
  • 1
    @EricLippert I never looked at it from this way but I can see that, thank you! In the case of NOT / ! the only argument I may have is that it is easier to overlook while scanning through, but even that argument is not very strong.
    – Frank J
    Feb 27, 2020 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.