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); } } }
Console.WriteLine("total:", subtotal); ?
Console.WriteLine("total:" + subtotal);
Light travels faster than sound. That's why most people seem bright until you hear them speak.
Jorndel (09-07-2012)
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.
So your code would look like this:Code:Console.Write("Press any key to exit..."); Console.Read();
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(); } } }
The fish trap exists because of the fish.
Once you've gotten the fish you can forget the trap.
The rabbit snare exists because of the rabbit.
Once you've gotten the rabbit, you can forget the snare.
Words exist because of meaning.
Once you've gotten the meaning, you can forget the words.
Where can I find a man who has forgotten words so I can talk with him?
Hassan (09-08-2012)
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
Last edited by 'Bruno; 09-08-2012 at 07:16 AM.
Light travels faster than sound. That's why most people seem bright until you hear them speak.
Hassan (09-08-2012)
The fish trap exists because of the fish.
Once you've gotten the fish you can forget the trap.
The rabbit snare exists because of the rabbit.
Once you've gotten the rabbit, you can forget the snare.
Words exist because of meaning.
Once you've gotten the meaning, you can forget the words.
Where can I find a man who has forgotten words so I can talk with him?