Method not calculating properly

Really? I've never seen used much, I'll be doing the Microsoft exam for c# in two weeks and it was mentioned in that once but our tutor never mentioned it when he was teaching us variable types!
I use decimal far more often than I do double.

Ignoring other issues the SQL Server Money datatype gets mapped to decimal.
 
I'm beginning too think I'm not cut out too be a programmer if I can't get a simple program like below too work the way I want it too, I just want the program too exit the loop and quit is typed but instead it continues on asking the second question, any help appreciated.


Code:
 static void Main(string[] args)
        {
            Choice();
        }


        public static void Choice()
        {


            string quitter = "quit";
           

            do{

                Console.WriteLine("Please enter your  first name: ");
                string name = Console.ReadLine();

                Console.WriteLine("Please enter your second name: ");
                string secondName = Console.ReadLine();


                if (quitter.ToLower() == "quit")
                {
                    Console.WriteLine("Goodbye");
                    break;
                }

             
           
           
           
            } while (quitter != "quit") ;
        }
 
It wont quit at all until it gets passed the second prompt. It always executes both those prompts.

You dont really need a loop of any sort just to capture two variables.

There is also not much point having a "goodbye" message and the app just quits/closes anyway and you wont see it, maybe.
 
Could do this

Code:
string input = string.Empty;
            do
            {
                Console.WriteLine("Please enter your  first name: ");
                input = Console.ReadLine();

                if (input.ToLower() == "quit") break;

                Console.WriteLine("Please enter your second name: ");
                input = Console.ReadLine();
            }
            while (input.ToLower() != "quit");

This line

if (input.ToLower() == "quit") break;

exits the loop early if quit is entered first and the end of the loop handles the second input check.
 
I'm curious, did you actually attach a debugger at any point and walk through the code? It seems like if you get stuck after coding you don't know how to debug.

Also, please change your default 'to' to 'to' instead of 'too'.
 
I'm curious, did you actually attach a debugger at any point and walk through the code? It seems like if you get stuck after coding you don't know how to debug.

Also, please change your default 'to' to 'to' instead of 'too'.

What do you mean "attach a debugger"?
 
Last edited:
Back again with another problem, I have two methods that add numbers, I want the first to add two numbers and the second to also add two numbers but I want too pass the answer from the first method too 2nd method thus adding three numbers, any ideas?


Code:
  static void Main(string[] args)
        {
          int number = addtwonumbers(10,20);
          Console.WriteLine("The sum of the two numbers is"+number);
         
         int numbertwo = addthreenumbers(30,40);
         Console.WriteLine("The sum of the three number is now "+number,+numbertwo);
         
        }
        public static int addtwonumbers(int numone, int numtwo)
        {
            int number = numone + numtwo;

            return number;

        }

        public static int addthreenumbers(int numthree, int numfour)
        {

            int number = numthree + numfour;

            return number;
       
       
        }
 
What’s it doing? What’s it not doing?

What do you think might be wrong with it?

As a hint... You say that you want the second method to add three numbers, but you only pass two to it...
 
Stop typing 'too' when you mean 'to'.

Code:
static void Main(string[] args)
{
    var result = addtwonumbers(10, 20);
    //output result here
   
    result = addthreenumbers(result, 30, 40);
    //output result here
}

public static int addtwonumbers(int val1, int val2)
{
    return va1 + val2
}

public static int addthreenumbers(int val1, int val2, int val3)
{
    return val1 + val2 + val3;
}
 
I haven't done any serious coding for decades but two things leap out at me about your code: first, there's no inline documentation; and second, there's no error-checking. Has your tutor mentioned things like preconditions and post-conditions? Yes the code is trivial but you should start good practice right away.
 
I haven't done any serious coding for decades but two things leap out at me about your code: first, there's no inline documentation; and second, there's no error-checking. Has your tutor mentioned things like preconditions and post-conditions? Yes the code is trivial but you should start good practice right away.

Inline documentation meaning commenting code yes? that's down too me being lazy more than anything, I know I should do it,as for preconditions and postconditions, I've never heard them being mentioned.
 
Stop typing 'too' when you mean 'to'.

Code:
static void Main(string[] args)
{
    var result = addtwonumbers(10, 20);
    //output result here
  
    result = addthreenumbers(result, 30, 40);
    //output result here
}

public static int addtwonumbers(int val1, int val2)
{
    return va1 + val2
}

public static int addthreenumbers(int val1, int val2, int val3)
{
    return val1 + val2 + val3;
}

Was messing around with the code you posted and think I finally have the hang of it, cheers.

Code:
    int result = addtwonumbers(10, 20);
            //output result here

            result = addthreenumbers(result, 30, 40);
            //output result here

            Console.WriteLine("The total amount is: "+result);

            int resultTwo = addfournumbers(600,600,result);
            
            resultTwo = addfournumbers(result,600,600);

            Console.WriteLine("The new total amount is: "+resultTwo);

            int resultThree = addfivenumbers(1000, 1000, result, resultTwo);

            resultThree = addfivenumbers(1000, 1000, result, resultTwo);

            Console.WriteLine("The newest total is now: "+resultThree);


        
        }


        public static int addtwonumbers(int val1, int val2)
        {
         return val1 + val2;
        }

        
        public static int addthreenumbers(int val1, int val2, int result)
        {
            return val1 + val2 + result;
        }

        
        public static int addfournumbers(int val1, int val2, int result)
        {
            return val1 + val2 + result;
        }     
        
        
        public static int addfivenumbers(int val1,int val2,int result,int resultTwo)

        {
            return val1 + val2 + result + resultTwo;
 
public static int addthreenumbers(int val1, int val2, int result)
{
return val1 + val2 + result;
}


public static int addfournumbers(int val1, int val2, int result)
{
return val1 + val2 + result;
}

These 2 methods do exactly the same thing.
The addfournumbers method doesnt know where the inputs come from. It doesnt know that the variable 'result' you gave it is the result of an addition of 2 other numbers, all it knows is 'result' is a single number.

I would write those methods in the following way to avoid confusion:
Code:
public static int addtwonumbers(int val1, int val2)
       {
         return val1 + val2;
        }

     
        public static int addthreenumbers(int val1, int val2, int val3)
        {
            return val1 + val2 + val3;
        }

     
        public static int addfournumbers(int val1, int val2, int val3, int val4)
        {
            return val1 + val2 + val3 + val4;
        }   
     
     
        public static int addfivenumbers(int val1,int val2,int val3, int val4, int val5)

        {
            return val1 + val2 + val3 + val4 + val5;
}

you dont need to match the variable names you've used when creating the methods to the variable names you pass into the methods when writing the main body of code (in fact, it makes things more confusing for yourself).

If you use the code I wrote above where the input names to the method are val1,val2,val3,val4,val5 the line you wrote which says "addfivenumbers(1000, 1000, result, resultTwo);" is still correct.
 
Inline documentation meaning commenting code yes? that's down too me being lazy more than anything,

Get into the habit of doing it right.

as for preconditions and postconditions, I've never heard them being mentioned.

Get your tutor to explain them.

As for the techniques used above in adding multiple numbers, can I just mention limited recursion? As in AddTwoNumbers(A,(AddTwoNumbers(B, AddTwoNumbers (C,D))))?
 
What do you mean "attach a debugger"?
I mean did you actually do any proper debugging or are you perpetually guessing without observing. I think your response is pretty damning of whatever course you're doing if you don't know what 'attach a debugger' means. Debugging is essential to programming - learn to do it asap!
 
I would write those methods in the following way to avoid confusion:
Code:
public static int addtwonumbers(int val1, int val2)
       {
         return val1 + val2;
        }

  
        public static int addthreenumbers(int val1, int val2, int val3)
        {
            return val1 + val2 + val3;
        }

  
        public static int addfournumbers(int val1, int val2, int val3, int val4)
        {
            return val1 + val2 + val3 + val4;
        }
  
  
        public static int addfivenumbers(int val1,int val2,int val3, int val4, int val5)

        {
            return val1 + val2 + val3 + val4 + val5;
}

Really?


Code:
    public static int addNumbers(params int[] vals)
    {
        int sum = 0;
        foreach (int i in vals)
            sum += i;

        return sum;
    }

or using linq just,

'return vals.Aggregate((x,y) => x + y);'
 
Back
Top Bottom