Home > Software engineering >  Why does my program output end with "will be 181" instead of "will be 19"?
Why does my program output end with "will be 181" instead of "will be 19"?

Time:11-25

Ask the user to enter their name and current age. Write code to print a personalised greeting and tell them how old they will be on their next birthday.

e.g. If the user enters "Kelly" and "43", your program should output:

"Hello Kelly, on your next birthday you will be 44."

This is my code, it keeps putting 1 at the end of the number instead of doing addition e.g. "Hello earl, on your next birthday you will be 181" instead of "Hello earl, on your next birthday you will be 19."

string name, input;
int age;
Console.WriteLine("What is your name?");
name= Console.ReadLine();
Console.WriteLine("What is your age?");
input = Console.ReadLine();
age = Convert.ToInt32(input);
Console.WriteLine("Hello "   name   ", on your next birthday you will be  "   age   1);
Console.ReadLine();

CodePudding user response:

Look at this snippet of code:

"Hello "   name   ", on your next birthday you will be  " age  1

Notice that you have four operators there. What are the first three doing? They are performing string concatenation. You seem to be under the impression that the last one will be done first for some reason. It won't. It will be done last, after the value of age has already been concatenated into a string, so it will perform string concatenation as well, which is exactly what you're seeing.

If you expect that numeric addition to be done first, before the string concatenation, then you have to indicate that somehow. One way would be to do it separately first, then use the result in the string concatenation, e.g.

age = age   1;

The other way would to be indicate that that operation should be done first within the complex expression. If you've done the most basic of mathematics at school, you know that that is done using parentheses:

"Hello "   name   ", on your next birthday you will be "   (age   1)

EDIT:

By the way, I would recommend that you never use more than one concatenation operator in a single expression. If you need more, use String.Format or string interpolation instead. In that case, there would be no concatenation operators so your addition would be performed as you expect:

$"Hello {name}, on your next birthday you will be {age   1}"

I just noticed that another answer recommends string interpolation but there's at least one flaw in that as it is currently and it doesn't really explain the original problem.

CodePudding user response:

The below code will fix the error

static void Main(string[] args)
        {
            string name, input;
            int age;
            Console.WriteLine("What is your name?");
            name= Console.ReadLine();
            Console.WriteLine("What is your age?");
            input = Console.ReadLine();
            age = Convert.ToInt32(input);

            // Change in below line
            Console.WriteLine("Hello "   name   ", on your next birthday you will be  "   (age  1));
            Console.ReadLine();
        }
    }
}

The change is in this line of code

Console.WriteLine("Hello "   name   ", on your next birthday you will be  " (age  1));

The reason it keeps putting 1 at the end, is because it is doing a string concatenation on the above line of code.

Console.WriteLine() is simply treating all the concatenations as strings. So, age is being passed in as a string, and 1 is also treated as a string. So, the program concatenates 18 1 giving 181

By putting this evaluation in paranteses, we instruct the proram to first evaluate what is in the brackets (i.e. 18 1 = 19), thereby returning the desired output

CodePudding user response:

As pointed out in the comments, snippet bellows WILL NOT do the trick!

Console.WriteLine($"Hello {name}, on your next birthday you will be {age  }");

Don't use if you plan to use age variable later on, but if you don't then this will do the trick for you:

Console.WriteLine($"Hello {name}, on your next birthday you will be {  age}");
  • Related