I'm young and experimenting with coding trying to find what language is "my language" (which I guess means is that it's natural and I'm good at) So I started off with python and some MIT OCW to get introduced to the world of programming. Then I wanted to be able to develop desktop apps. So I tried out java. It didn't click. Then I tried C/C++. That was ok. Then I just said. What platform are you programming on? Well....Windows! So I looked up C# and it's going well and I think that it comes easily to me. Nevertheless I have no idea why I put all of that but by question was....What's wrong with this code? I've done some basic tutorials and I don't see what's wrong with this code. So I hopped on here to try and get help (and yes I tried google first)

using System;

public class Main
{
   public static void Main()
   {
      // code
      string productName = "TV";
      int productYear = 2012;
      float productPrice = 279.99;
      // test code
      Console.WriteLine("productName: " + productName);
      Console.WriteLine("productYear: " + productYear);
      Console.WriteLine("productPrice: " + productPrice);


   }
}

Recommended Answers

All 4 Replies

A good start in determining what's wrong is asking what it's doing versus what you expect it to do. So...what's it doing that's different from what you expected? :)

I see two problems in particular, one of which will be immediately obvious from the compiler's errors and the second will appear after fixing the first.

I'm not being obtuse, by the way. I'm trying to push you to learn how to figure out your own problems, which is a huge skill to have in programming.

In C#, a literal like 279.99 is considered of type double.
C# is very strict about typing(I like that btw.), so a float is not a double. The error message is very clear on how to remedy this.
Instead of using the concatenation operator in your WriteLines I would use this syntax:

Console.WriteLine("productName: {0}", productName);
Console.WriteLine("productYear: {0}", productYear);
Console.WriteLine("productPrice: {0}", productPrice);

Welp, since ddanbe covered the second error, I'll mention the first. You have a name conflict. The class and method are both called Main. Since the method has to be called Main, change the class to something like Program and the compiler should properly report that your floating-point literal needs a type qualifier.

Got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.