C
C#10mo ago
Martindb

Compiler doesn't work properly

So i'm following the Microsoft course for beginners with C#. The assignment is letting the console show this message "Hello, Bob! You have 3 messages in your inbox. The temperature is 34.4 celsius." This is my code, when I try to run it the online course keeps processing and doesn't proceed. When I check my code in VS code it shows no errors, any help would be appreciated.
//Declaration of variables
string firstName = ("Bob");
int number = (3);
decimal otherNumber = (34.4M);

//Console commands
Console.Write("Hello, ");
Console.Write(firstName + "!");
Console.Write("You have" + number + "messages in your inbox");
Console.Write("The temperature is " + otherNumber + "celsius.");
//Declaration of variables
string firstName = ("Bob");
int number = (3);
decimal otherNumber = (34.4M);

//Console commands
Console.Write("Hello, ");
Console.Write(firstName + "!");
Console.Write("You have" + number + "messages in your inbox");
Console.Write("The temperature is " + otherNumber + "celsius.");
20 Replies
Pobiega
Pobiega10mo ago
First, that's a very unusual style of variable initialisation. You don't need the parentheses there. Second, those 4 Console.Write could and should be replaced with a single WriteLine with string interpolation
Martindb
Martindb10mo ago
Sorry i’m not sure what you mean with String interpolation…
Pobiega
Pobiega10mo ago
Bring up your favorite search engine and search "C# string interpolation"
Martindb
Martindb10mo ago
Sure will do, sorry for that I could have thought of that myself 😅
Azrael
Azrael10mo ago
Uhm... Pobiega said it all.
DΣX
DΣX10mo ago
There doesnt seem to be much actualy wrong with your code. it should execute. most probable cause is, that the validator expects an new-line in the end so in your last statement, just use WriteLine instead of Write
Angius
Angius10mo ago
There will be no space between the temperature and word celsius
No description
Angius
Angius10mo ago
The message will be Hello, Bob!You have3messages in your inboxThe temperature is 34.4celsius That's why string interpolation is the way
Martindb
Martindb10mo ago
Thanks everyone for the feedback 🙂
//Declaration of variables
string firstName = ("Bob");
int number = (3);
decimal temperature = (34.4M);

//Console command
Console.Write($"Hello, {firstName}! You have {number} messages in your inbox. The temperature is {temperature} celsius. ");
//Declaration of variables
string firstName = ("Bob");
int number = (3);
decimal temperature = (34.4M);

//Console command
Console.Write($"Hello, {firstName}! You have {number} messages in your inbox. The temperature is {temperature} celsius. ");
String interpolation if I understood correctly 🙂
SinFluxx
SinFluxx10mo ago
You still don't need the parentheses when declaring your variables 🙂
Pobiega
Pobiega10mo ago
that, and also, consider using WriteLine instead, so there is a newline added at the end and that blank space at the end triggers me
Martindb
Martindb10mo ago
How is this ?, Removed the blank space at the end as mentioned by @Pobiega and the parentheses with the variable declaration @SinFluxx
//Declaration of variables
string firstName = "Bob";
int number = 3;
decimal temperature = 34.4M;

//Console command
Console.WriteLine($"Hello, {firstName}! You have {number} messages in your inbox. The temperature is {temperature} celsius.");
//Declaration of variables
string firstName = "Bob";
int number = 3;
decimal temperature = 34.4M;

//Console command
Console.WriteLine($"Hello, {firstName}! You have {number} messages in your inbox. The temperature is {temperature} celsius.");
Pobiega
Pobiega10mo ago
perfect
Martindb
Martindb10mo ago
Awesome Thanks In my variable declaration is the added m required since i'm already declaring a decimal type ?
Pobiega
Pobiega10mo ago
yes the right side is not aware of whats happening on the left. and M is the decimal suffix for a literal
Martindb
Martindb10mo ago
Good to know So the declaration of the decimal type happens twice, left for the human readability and on the right for the compiler to process it correctly ?
Pobiega
Pobiega10mo ago
its not different from how your other variables are declared "Bob" is a string literal. 3 is an int literal 34.4M is a decimal literal you could replace the types on the left with var in this code it would change nothing
Martindb
Martindb10mo ago
Because var inherits the data type from which is assigned to ?
Angius
Angius10mo ago
Infers But yes
Martindb
Martindb10mo ago
Infers , all right noted 🙂