Reputation: 69
Good Morning/Afternoon,
I'm would love some clarification on passing by reference and value. The program being posted works if I set the variable value to "0" but does not override after user input. So I believe the issue is passing, however my professor said everything should be static in this program.
Short: How to pass an input variable without the use of void?
I am very new. This is only chapter 3 so any assistance would be wonderful.
namespace Lesson3
{
class Program
{
public static void Main(string[] args)
{
// declarations
// 12 inches to a foot
// 36 inches to a yard
// 63360 inches per 1 mile
double userInput= 0; // if "0" is removed the program will not function CS0165 error
double feet = 12;
double yards = 36;
double miles = 63360;
double ansFeet = userDivFeet(ref feet, ref userInput);
double ansYards = userDivYards(ref yards, ref userInput);
double ansMiles = userDivMiles(ref miles, ref userInput);
Console.WriteLine("Please enter the number of inches to convert: ");
userInput = Convert.ToInt32(Console.ReadLine());
// output each calculation and display with console to hold the results
WriteLine(userInput + " inches converts into " + ansFeet + " feet.");
WriteLine(userInput + " inches converts into " + ansYards + " yards.");
WriteLine(userInput + " inches converts into " + ansMiles + " miles.");
Console.ReadKey();
}
public static double userDivFeet(ref double userInput, ref double feet)
{
return userInput / feet;
}
public static double userDivYards(ref double userInput, ref double yards)
{
return userInput / yards;
}
public static double userDivMiles(ref double userInput, ref double miles)
{
return userInput / miles;
}
}
}
Upvotes: 0
Views: 1574
Reputation: 2923
The reason you don't see any changes is because you're calling your methods before getting the input of the user so userInput
will always be equal to 0 you should move them after the
userInput = Convert.ToInt32(Console.ReadLine());
So it would look like this
public static void Main(string[] args)
{
// declarations
// 12 inches to a foot
// 36 inches to a yard
// 63360 inches per 1 mile
double userInput = 0; // if "0" is removed the program will not function CS0165 error
double feet = 12;
double yards = 36;
double miles = 63360;
Console.WriteLine("Please enter the number of inches to convert: ");
userInput = Convert.ToInt32(Console.ReadLine());
double ansFeet = userDivFeet(ref feet, ref userInput);
double ansYards = userDivYards(ref yards, ref userInput);
double ansMiles = userDivMiles(ref miles, ref userInput);
// output each calculation and display with console to hold the results
WriteLine(userInput + " inches converts into " + ansFeet + " feet.");
WriteLine(userInput + " inches converts into " + ansYards + " yards.");
WriteLine(userInput + " inches converts into " + ansMiles + " miles.");
Console.ReadKey();
}
And as Polyfun stated you don't need to use ref, Also the reason your teacher said it must all be static is because you're calling those methods directly from the main.
If you want the user to be able to enter number with decimals like 1.25 you should change the Convert.ToInt32
to a Convert.ToDouble
You were getting CS0165 error
when removing the = 0
because you were trying to use an unassigned value in a method, now that the methods are moved after you assigned a value to userInput
you shouldn't get this error anymore.
Upvotes: 1
Reputation: 43
You're using the variable userInput to call your method before assigning the user input. Accept the input first and then call the call the methods.
Also, you're declaring userInput as double but parsing the actual user input as int. Change it to parse it as double.
using System;
namespace Lesson3 {
internal class Program
{
public static void Main(string[] args)
{
// declarations
// 12 inches to a foot
// 36 inches to a yard
// 63360 inches per 1 mile
double userInput=0; // if "0" is removed the program will not function CS0165 error
double feet = 12;
double yards = 36;
double miles = 63360;
Console.WriteLine("Please enter the number of inches to convert: ");
userInput = Convert.ToInt32(Console.ReadLine());
double ansFeet = userDivFeet(ref feet, ref userInput);
double ansYards = userDivYards(ref yards, ref userInput);
double ansMiles = userDivMiles(ref miles, ref userInput);
// output each calculation and display with console to hold the results
Console.WriteLine(userInput + " inches converts into " + ansFeet + " feet.");
Console.WriteLine(userInput + " inches converts into " + ansYards + " yards.");
Console.WriteLine(userInput + " inches converts into " + ansMiles + " miles.");
Console.ReadKey();
}
public static double userDivFeet(ref double userInput, ref double feet)
{
return userInput/feet;
}
public static double userDivYards(ref double userInput, ref double yards)
{
return userInput/yards;
}
public static double userDivMiles(ref double userInput, ref double miles)
{
return userInput/miles;
}
}
}
Upvotes: 0