1 Replies - 3979 Views - Last Post: 12 August 2007 - 10:07 PM Rate Topic: -----

#1 muth2355  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-August 07

Using a loop for to find the base and power value

Post icon  Posted 12 August 2007 - 09:30 PM

I'm a beginner and i have run into a problem while writing an application that displays the answer while the user provides the base and power. Below is my attempt ........ Can someone please help!!!!



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

namespace _702A__Unit5
{
class Program
{
static void Main()
{
int result;
string moreData;
double n1;
double n2;


do
{
n1 = InputA( );
n2 = InputB( );
CalculateExponent( );
DisplayExponent( );
moreData() = PromptForMoreCalculations( );
}
while (moreData == "y");


}
public static void CalculateExponent(int n, out int result)
{
result = Math.Pow(n1, n2);
}


}
public static double InputA( )
{
string inValueA;
double base;

Console.WriteLine("Please enter the base: ");
inValueA = Console.ReadLine();
n1 = Convert.ToDouble(inValueA);
return n1;
}
public static int InputB()
{
string inValueB;
double n2;

Console.WriteLine("Please enter the power: ");
inValueB = Console.ReadLine();
n2 = Convert.ToDouble(inValueB);
return n2;



public static string PPromptForMoreCalculations( )
{
string moreData;
Console.WriteLine"(Would you like to enter another set of numbers <y/n>?");
moreData = Console.ReadLine();
return moreData;



}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Using a loop for to find the base and power value

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Using a loop for to find the base and power value

Posted 12 August 2007 - 10:07 PM

This program had several problems associated with it and I would suggest you read up on the chapters dealing with calling functions and returning a result and variable scope. Below is a list of items I found wrong followed by a working copy for you to play with.

1) You need to match up your closing curly braces with your open braces. The closing curly braces before your inputA() function is actually closing your class "Program"

2) You have an extra "P" on your function name for "PromptForMoreCalculations" which causes your call to the function not to work.

3) You don't have DisplayExponent() defined at all, so that is going to fail when you attempt to call a function you haven't defined.

4) I am not too sure why you use "out" for your CalculateExponent function because that is why you, instead of using void, would return the result. I know you are setting the result variable, but it is not needed. Use the return value from the function. To do this, if you are going to use Math.Pow it will return a double, so you need the result to be a double as well or else you will get a warning saying a double to integer conversion will cause data loss. If you are unfamiliar with this, read up on the section for "datatypes"

5) Lastly, it is a bit wiser and safer to compare strings with the "equals" method of a string. So for checking if they entered "y" you can use... moreData.equals("y") instead of a comparison operator like ==.

Like I said before, the working version is listed below. It is up to you to learn what I have done to change yours to mine and why I might have done that. Then you can expand on it.

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

namespace _702A__Unit5 {
	class Program {
		static void Main()
		{
			double result;
			string moreData;
			double n1;
			double n2;


			do {
				n1 = InputA();
				n2 = InputB();
				result = CalculateExponent(n1,n2);
				DisplayExponent(result);
				moreData = PromptForMoreCalculations();
			} while (moreData.Equals("y"));
		}

		public static double CalculateExponent(double baseNumber, double power)
		{
			return Math.Pow(baseNumber, power);
		}

		public static double InputA()
		{
			string inValueA;
			double n1;

			Console.WriteLine("Please enter the base: ");
			inValueA = Console.ReadLine();
			n1 = Convert.ToDouble(inValueA);
			return n1;
		}

		public static double InputB()
		{
			string inValueB;
			double n2;

			Console.WriteLine("Please enter the power: ");
			inValueB = Console.ReadLine();
			n2 = Convert.ToDouble(inValueB);
			return n2;
		}


		public static string PromptForMoreCalculations()
		{
			string moreData;
			Console.WriteLine("Would you like to enter another set of numbers <y/n>?");
			moreData = Console.ReadLine();
			return moreData;
		}

		public static void DisplayExponent(double resultToPrint)
		{
			Console.WriteLine(resultToPrint.ToString());
		}
	}
}



Enjoy!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1