Hello everyone. I'm new here and I am still in the beginning stages of learning C#.
I am trying to write a program for school that calculates exponents but I have to use a loop. Then I have to prompt the user if they want to enter some more numbers. If yes, then it should ask if they want to enter the base and power , etc. all over again.
I think I might be close but I keep getting an error in my CalculateTotal method that not all my code paths return a value.

Can someone take a look at what I have and let me know if I am doing this right and what it means by not all my code paths return a value?
Thanks in advance.

using System;

namespace ExponentCalculator
{
	
	class Power
	{
		
		static void Main(string[] args)
		{
			int number = 0;
			int power = 0;
			int result= 1;
			Console.Write("Please enter the base: ");
			number = int.Parse(Console.ReadLine());
			Console.Write("Please enter the power: ");
			power = int.Parse(Console.ReadLine());
			CalculateTotal(number, power, result);
			CalculationPrompt();
		}

		public static int CalculateTotal(int number, int power, int result)
		{
			
			for(;power != 0; power--)
				result*= number;
			Console.WriteLine("{0}^{1} = {2}", number, power, result);
		}

		public static string CalculationPrompt()
		{
			string moreNumbers;
			Console.Write("Would you like to enter another set of numbers (y/n)?");
			moreNumbers = Console.ReadLine();
			return moreNumbers;
		}
	}
}

When this code is run it generates the code saying that not all the code paths return values - you need to add a return(0); to the function. Here is a fixed version:

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

namespace Exponent_Thing
{
    class Program
    {
        static void Main(string[] args)
        {
			int number = 0;
			int power = 0;
			int result= 1;
			Console.Write("Please enter the base: ");
			number = int.Parse(Console.ReadLine());
			Console.Write("Please enter the power: ");
			power = int.Parse(Console.ReadLine());
			CalculateTotal(number, power, result);
			CalculationPrompt();
		}

		public static int CalculateTotal(int number, int power, int result)
		{
			
			for(;power != 0; power--)
				result*= number;
			Console.WriteLine("{0}^{1} = {2}", number, power, result);
            return (0);
		}

		public static string CalculationPrompt()
		{
			string moreNumbers;
			Console.Write("Would you like to enter another set of numbers (y/n)?");
			moreNumbers = Console.ReadLine();
			return moreNumbers;
		}
    }
}

Do you also want it to loop over when the user enters Y instead of ending?

Now it will loop if you press Y - but you have to enter N twice... I'll see If I can fix that bug too...

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

namespace Exponent_Thing
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 10000; i++)//will loop 10000 times
            {
                int number = 0;
                int power = 0;
                int result = 1;
                Console.Write("Please enter the base: ");
                number = int.Parse(Console.ReadLine());
                Console.Write("Please enter the power: ");
                power = int.Parse(Console.ReadLine());
                CalculateTotal(number, power, result);
                if (!(CalculationPrompt() == "y" || CalculationPrompt() == "Y"))
                {
                    return;
                    break;
                }
            }
            return;
        }
		public static int CalculateTotal(int number, int power, int result)
		{
			
			for(;power != 0; power--)
				result*= number;
			Console.WriteLine("{0}^{1} = {2}", number, power, result);
            return (0);
		}

		public static string CalculationPrompt()
		{
			string moreNumbers;
			Console.Write("Would you like to enter another set of numbers (y/n)?");
			moreNumbers = Console.ReadLine();

            return moreNumbers;

		}
    }
}

Entirely solved:

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

namespace Exponent_Thing
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 10000; i++)//will loop 10000 times
            {
                int number = 0;
                int power = 0;
                int result = 1;
                Console.Write("Please enter the base: ");
                number = int.Parse(Console.ReadLine());
                Console.Write("Please enter the power: ");
                power = int.Parse(Console.ReadLine());
                CalculateTotal(number, power, result);
                if (!(CalculationPrompt() == "y"))
                {
                    Environment.Exit(0);
                    break;
                }
            }
            return;
        }
		public static int CalculateTotal(int number, int power, int result)
		{
			
			for(;power != 0; power--)
				result*= number;
			Console.WriteLine("{0}^{1} = {2}", number, power, result);
            return (0);
		}

		public static string CalculationPrompt()
		{
			string moreNumbers;
			Console.Write("Would you like to enter another set of numbers (y/n)?");
			moreNumbers = Console.ReadLine();
            return moreNumbers;

		}
    }
}

CalculateTotal should only do what it says : Calculate a total.
Your method writes the result of a power calculation to the console. In a small program like this it seems not to matter too much, but if you are working on a large project with many programmers, such things can become fatal.
This is a possible solution:

public static int CalculatePower(int number, int power)
{ 			
    for(;power != 0; power--)	result*= number;	
    return result;		
}

In your main for loop do :

result = CalculatePower(number, power);
Console.WriteLine("{0}^{1} = {2}", number, power, result);

If you want to loop forever(like your for 10000 loop seems to indicate) use while (true) never use while (0) in C#!
Try to figure thatone out for yourself!
Happy computing:)

AHA! I was missing the return (0); in my calculateTotal method.
I'm not sure I understand why it would not work without that though.

When you declare something like
static int MyMethod() you are telling C# that MyMethod returns an integer, you HAVE to use a return statement in MyMethod.
When you don't want that use void.
static void MyMethod()

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.