C
C#15mo ago
Ewan

❔ ✅ "Cannot convert from int to string" why cant I take away these values??

the code underlined in red gives me this message, why doesnt the subtraction work?
No description
7 Replies
Thinker
Thinker15mo ago
Console.WriteLine expects a string, you're giving it an int
Ewan
EwanOP15mo ago
its in brackets? then why does the trainnumber on the right work?
Thinker
Thinker15mo ago
long explaination but essentially Console.WriteLine can take any amount of arguments, and it doesn't care about the type of any arguments besides the first If you want to make a string out of that then you'll need to use interpolated strings.
Console.WriteLine($"{trainnumber - notlate} out of {trainnumber} trains were late");
Console.WriteLine($"{trainnumber - notlate} out of {trainnumber} trains were late");
Ewan
EwanOP15mo ago
ah i see thanks!
mg
mg15mo ago
yeah, it's trying to invoke one of these overloads: https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-7.0#system-console-writeline(system-string-system-object-system-object-system-object) https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-7.0#system-console-writeline(system-string-system-object()) which is why it wants your first argument to be a string the subtraction itself is totally fine and does produce an int, but that's not what Console.WriteLine() wants
Ewan
EwanOP15mo ago
ok i gotcha, thank you! yay
Accord
Accord15mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?