Lesson 13 +25 XP

Project 1: Personal Greeter

Project 1: Personal Greeter

Your first C# console app: ask for a name and age, then greet the user.

The goal

Read a name and an age, then print a friendly message that tells them how old they'll be next year.

What you practice

  • Console.WriteLine and Console.ReadLine
  • Convert.ToInt32 casting
  • String interpolation with
    quot;{x}"
  • A little arithmetic

Starter code

using System;

Console.WriteLine("What is your name?");
string name = Console.ReadLine();

Console.WriteLine("How old are you?");
int age = Convert.ToInt32(Console.ReadLine());

int nextYear = age + 1;
Console.WriteLine(
quot;Hello {name}, next year you will be {nextYear}!");

Step-by-step

  1. Run the starter and answer the questions.
  2. Wrap the age conversion in a try...catch that handles a bad number.
  3. Add a goodbye message at the end.
  4. Re-run and test with both valid and invalid ages.

Checklist

  • [ ] The name is read with ReadLine()
  • [ ] The age is converted with Convert.ToInt32
  • [ ] The message uses interpolation
  • [ ] Next year's age is correct
  • [ ] A try/catch handles bad input
  • [ ] The program runs without errors

TL;DR

  • ReadLine() always returns a string.
  • Convert it before doing math.
  • Interpolation makes messages clean.