Results 1 to 7 of 7
  1. #1
    rmax59's Avatar
    Join Date
    Jul 2012
    Gender
    male
    Posts
    7
    Reputation
    10
    Thanks
    0

    [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);
    
    
            }
        }
    }

  2. #2
    Jorndel's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    Norway
    Posts
    8,676
    Reputation
    905
    Thanks
    19,113
    My Mood
    Angelic
    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?

     
    Contributor 01.27.2012 - N/A
    Donator 07-17-2012 - Current
    Editor/Manager 12-16-12 - N/A
    Minion 01-10-2013 - 07.17.13
    Former Staff 09-20-2012 - 01-10-2013 / 07-17-2013 - Current
    Cocksucker 20-04-2013 - N/A

  3. #3
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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.

  4. The Following User Says Thank You to 'Bruno For This Useful Post:

    Jorndel (09-07-2012)

  5. #4
    Broderick's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Location
    Basement.
    Posts
    100
    Reputation
    42
    Thanks
    30
    Quote Originally Posted by Brinuz 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();
            }
        }
    }
    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?

  6. The Following User Says Thank You to Broderick For This Useful Post:

    Hassan (09-08-2012)

  7. #5
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    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
    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.

  8. The Following User Says Thank You to 'Bruno For This Useful Post:

    Hassan (09-08-2012)

  9. #6
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Quote Originally Posted by Brinuz 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.

  10. #7
    Broderick's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Location
    Basement.
    Posts
    100
    Reputation
    42
    Thanks
    30
    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.
    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?

Similar Threads

  1. Replies: 10
    Last Post: 11-30-2010, 02:59 PM
  2. Need help on a simple question
    By xi2fastxi in forum Combat Arms Help
    Replies: 7
    Last Post: 10-31-2010, 01:04 PM
  3. Simple Question (Help)
    By chimpwalk in forum Combat Arms EU Discussions
    Replies: 0
    Last Post: 08-06-2010, 12:40 AM
  4. help if u want, just a simple question.
    By lingfei1994 in forum Combat Arms Mods & Rez Modding
    Replies: 10
    Last Post: 02-27-2010, 12:52 PM
  5. HELP PLZ SIMPLE QUESTION
    By GibsonRocks231 in forum Combat Arms Hacks & Cheats
    Replies: 9
    Last Post: 01-05-2009, 04:24 PM