0

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;
    }




    }

}

2
  • Clue: you don't need to use ref, and where you are reading the userInput is too late. Commented Jul 6, 2017 at 14:22
  • @ Poly thank you so much, I greatly appreciate the clue's ! Commented Jul 6, 2017 at 14:45

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And don't need to use ref.
Thank everyone so much... I was parsing an Int when I wanted a double... while I was creating it.... I thought a person may not just throw in whole numbers. If anyone else is referencing this for future work - sorry there is a logical error. The feet, userInput need to be swapped to userInput / feet. The results concluded 6000 inches was .0045 feet which is way way wrong. :-) Thanks everyone so much!
@Elements Don't forget that you can accept an answer if it solved your question.
Gah, I up-voted instead of accepting! Thank you for the reminder!
0

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;
    }


}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.