1

I'm trying to create a program that will ask for your name and age and then say something like "Hi "Name" you have "years left" years left until your retierment. My problem is that i cant get the math correct, im quite new at this and cant find a solution on my own, heres my code so far:

using System;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace PensionAluHermondsUppgift1
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("What is your first name?");
            string a = Console.ReadLine();
            Console.WriteLine("What is your last name?");
            string b = Console.ReadLine();
            Console.WriteLine("How old are you?");
            string c = Console.ReadLine();
            string d = 65 - c;
            Console.WriteLine("HI " + a +" " + b + " you have " + d + " years left until your retierment!");
        }
    }
}

2 Answers2

1

The problem is that you can't subtract from a string and currently the variable you have for a year is a string.

So you need to change the variable c to an int. (you should give your variables meaningful names FYI)

string c = Console.ReadLine();
int d = 65 - int.Parse(c);

You can see a working example here dotnet fiddle

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
ejwill
  • 141
  • 8
0

Your mistake is here

string c = Console.ReadLine();

c = "65" which is not a number, its a string and cannot be substructed from 65,

you need to parse it into number

try :

int c = int.Parse(Console.ReadLine());
AnGG
  • 679
  • 3
  • 9