I'm a bit of a novice when it comes to programming, wanting to learn.

I'm trying to write a program that has a while loop in it, or preferrably a do..while loop but I can't seem to call methods within a nested if statement in the while loop.

How is this done? Have I been clear enough?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int userChange, twoPoundCoin, onePoundCoin, fiftyPCoin, twentyPCoin,
                tenPCoin, fivePCoin, twoPCoin, onePCoin;

            Console.WriteLine("Please enter an amount in pence: ");
            userChange = Convert.ToInt32(Console.ReadLine());

            do
            {
                if (userChange >= 199)
                {
                    LargeCoins();
                }

                else if (userChange >= 9)
                {
                    MediumCoins();
                }

                else if (userChange >= 1)
                {
                    SmallCoins();
                }
            } while (userChange != 0);
        }
    }
}

Any help would be, well, helpful,
Cheers.

You haven't defined the LargeCoins(), MediumCoins(), and SmallCoins() methods. Calling them within the loop and within the if blocks will work.

One issue you presently have is that there is nothing within the loop to eventually bring about an exit condition. As such, and once you define the aforementioned methods, you'll have an infinite loop.

Also, drop Convert.ToInt32. If, as a user, I enter "hey I'm not an integer," your program will throw an exception. Look into int.TryParse and using it to ensure that the user inputs something useful.

Thanks for the help, I've managed to get the methods to not throw up errors but now I can't access the variables from within the main method? Here is my "new" code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int userChange, twoPoundCoin, onePoundCoin, fiftyPCoin, twentyPCoin,
                tenPCoin, fivePCoin, twoPCoin, onePCoin;

            Console.WriteLine("Please enter an amount in pence: ");
            userChange = Convert.ToInt32(Console.ReadLine());

            do
            {
                if (userChange >= 199)
                {
                    LargeCoins();
                }

                else if (userChange >= 9)
                {
                    MediumCoins();
                }

                else if (userChange >= 1)
                {
                    SmallCoins();
                }
            } while (userChange != 0);
        }

        public static void LargeCoins()
        {
            if (userChange >= 200)
            {

            }
        }

        public static void MediumCoins()
        {

        }

        public static void SmallCoins()
        {

        }

    }
}

O and I appreciate the fact that the methods and other parts may seem incomplete, I'm just trying to get over the initial small hurdles because I'm very tired and then once I understand my simple mistakes I should be able to complete the rest. =]

userChange is declared within the Main method. That makes it local to the Main method, meaning Main is the only method that can access it. To have a variable that is accessible from more than one method, it would need to be declared outside of any method in your class.

However, in this situation, let's talk about what the method is doing and then go onto how to work with your data. It is called LargeCoins, but is that was it does? Make your method names descriptive. Is your method removing large coins? Counting large coins? Adding large coins?

Once you've settled the naming issue, then modify the rest of the signature. Instead of making the userChange variable accessible within the entire class, add a parameter to the methods that you will be calling instead.

static void DoSomething(int input)
{
    // do something with the input
}

If you need data back from the method, give the method a type to match your intended output.

static int DoSomethingAndReturnAValue(int input)
{
   // do something with the input and return a value
   return 0;
}

Thank you very much for your help, I have it all sorted now after severely changing my code.
But I have come across a new problem.
I require an integer input by users, but one of the requirements is that it cannot be decimal nor string etc. just an int type. What kind of check can I do to ensure that only an int is entered?
I need it in an if statement really because of the nature of my code:
if (change < 0)
Return error message.

I need to add the conditions to stop an incorrect type input.

Thanks for any help,
Gl0mby

use int.tryparse:

int userInput;
if(int.TryParse(Console.ReadLine(), out userInput)
{
    //input was valid
}
else
{
    //error...non int value
}

TryParse accepts two parameters: string to parse, variable to store result in (reference by the 'out' keyword).
It returns a boolean value to say wether it succeeded.

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.