[Help] Simple question c#

Posts 17 of 7 · Page 1 of 1
[Help] Simple question c#
I can't get this simple program to output the total, please tell me what I'm doing wrong. Much appreciated.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static double CalcTotal(double num1, double num2)
        {
            double subtotal = num1 + num2;
            return subtotal;
        }
        static void Main(string[] args)
        {
            double subtotal = CalcTotal(9.3,4.3);
            Console.WriteLine("total:", subtotal);


        }
    }
}
Quote Originally Posted by rmax59 View Post
I can't get this simple program to output the total, please tell me what I'm doing wrong. Much appreciated.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static double CalcTotal(double num1, double num2)
        {
            double subtotal = num1 + num2;
            return subtotal;
        }
        static void Main(string[] args)
        {
            double subtotal = CalcTotal(9.3,4.3);
            Console.WriteLine("total:", subtotal);


        }
    }
}
Aren't you using decimal there?
Console.WriteLine("total:", subtotal); ?

Console.WriteLine("total:" + subtotal);
Quote Originally Posted by 'Bruno View Post
Console.WriteLine("total:", subtotal); ?

Console.WriteLine("total:" + subtotal);
Console.WriteLine has an overload similar to "string.Format". So that's not the issue.

There is nothing wrong with the code, it's probably just that you never actually get to see the output because the Console window will close straight away.

Add something like the following to pause the Console until the user presses a key.

Code:
Console.Write("Press any key to exit...");
Console.Read();
So your code would look like this:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static double CalcTotal(double num1, double num2)
        {
            double subtotal = num1 + num2;
            return subtotal;
        }
        static void Main(string[] args)
        {
            double subtotal = CalcTotal(9.3,4.3);
            Console.WriteLine("total:", subtotal);
            Console.Write("Press any key to exit...");
            Console.Read();
        }
    }
}
Quote Originally Posted by Broderick View Post


Console.WriteLine has an overload similar to "string.Format". So that's not the issue.

There is nothing wrong with the code, it's probably just that you never actually get to see the output because the Console window will close straight away.

Add something like the following to pause the Console until the user presses a key.

Code:
Console.Write("Press any key to exit...");
Console.Read();
So your code would look like this:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static double CalcTotal(double num1, double num2)
        {
            double subtotal = num1 + num2;
            return subtotal;
        }
        static void Main(string[] args)
        {
            double subtotal = CalcTotal(9.3,4.3);
            Console.WriteLine("total:", subtotal);
            Console.Write("Press any key to exit...");
            Console.Read();
        }
    }
}
Jason, if you tested it you would have seen that it is the problem. (It is outputting "Total:" only) And I do know that it has an overload for that.
Also C# always pauses at the end.

So yes the code is wrong

Or...

Console.WriteLine("total: {0}", subtotal);

In this case, yes you can use the format args
Quote Originally Posted by 'Bruno View Post


Jason, if you tested it you would have seen that it is the problem. (It is outputting "Total:" only) And I do know that it has an overload for that.
Also C# always pauses at the end.

So yes the code is wrong

Or...

Console.WriteLine("total: {0}", subtotal);

In this case, yes you can use the format args
Also C# always pauses at the end.
Wrong. Unless you press (Ctrl+F5) instead of the normal (F5 which is equivalent to pressing the start button) the console window will not stop.

Code:
Console.WriteLine("total: {0}", subtotal);
This. I believe Jason missed it.
Quote Originally Posted by Hassan View Post




Wrong. Unless you press (Ctrl+F5) instead of the normal (F5 which is equivalent to pressing the start button) the console window will not stop.



This. I believe Jason missed it.
Sorry, was drunk at the time, forgot about the format string lawls.
Posts 17 of 7 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?