none
How to use user input in an equation?

    Question

  • Hello!

    I'm a beginner software developer and i'm struggling with some "simple" codeing.My exercise was that i asked user to type the distance from 1 to 1200.Then the answer would be substracted from 1200 and i would get the answer.For example if i typed in 500,then the answer would be 700 and the number that was substracted would give me another answer of how many times the journey of Peter is longer or shorter than Mikes.Here's a part of my code,the part that i dont know how to solve.

                

    Console.Write("Type the length of Peters journey (1-1200)");

    num01 = Convert.ToInt32(Console.ReadLine());  <--- I dont know why i wrote that and if it helps or not.

    int num01 = I dont know how to make num01 equal to user input and if i dont know that i wont be able to finish the code.

    int num02 = 1200;

    int num03 = num02 - num01; (For example if the user answers 500 then 700 = 1200 - 500 and so Peters journey is 500 and Mikes journey is 700.Then i need the program to say how many times is Peters journy shorter or longer than Mikes , depending on the answer of the user).

    PS : Sorry for bad english.     

    Thursday, January 05, 2017 2:33 PM

All replies

  • Console.ReadLine will return a string.

    if you entered 700 then Console.ReadLine will return the string "700".

    you should use int.TryParse to convert it to an integer.  Because it will handle the case where the user enters something silly like "asdfasdf" by returning false.

    string userInputText = Console.ReadLine();
    int num01;
    if( int.TryParse( userInputText, out num01 ) {
        // num01 now has a numeric value
        // corresponding to the text that the user entered.
    } else {
        // User entered something that's not a number.
        // Display an error message.
        // and probably exit early so we don't continue.
    }


    Thursday, January 05, 2017 8:29 PM
  • Thanks Wyck.

    Hi my friend,

    Welcome to the MSDN forum.

    Like Wyck said, the method Console.ReadLine() outputs the string type then you need to transfer it. Refer to your description, I made a small test code as below to get the expected results, please have a look at the following:

    static void Main(string[] args)

            {

                Console.WriteLine("Type the length of Peter's journey(1-1200)");

                string typeinputtext = Console.ReadLine();

                int PeterJourney;

                if (int.TryParse(typeinputtext, out PeterJourney))

                {

                    int Total = 1200;

                    int MikeJourney = Total - PeterJourney;

                    int num01 = PeterJourney - MikeJourney;

                    if (num01<0)

                    {

                        Console.WriteLine("Peter's journey is shorter than Mike's journey about "+(-num01));

                        Console.ReadLine();

                    }

                    else if (num01>0) {

                        Console.WriteLine("Peter's journey is longer than Mike's journey about " + num01);

                        Console.ReadLine();

                    }

                    else

                    {

                        Console.Write("Peter's journey is the same with Mike's journey");

                        Console.ReadLine();

                    }

                }

                else {

                    Console.WriteLine("You should input an valid number with integer number");

                    Console.ReadLine();

                }

               

            }

    Executed the above code, I can get the result like the following screenshot:

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 06, 2017 5:58 AM
    Moderator
  • Hi my friend,

    Sorry to trouble you again, does your issue is solved or not? If not, please feel free to let me know.

    If it is solved, could you please share your solution in here, that will help other community members who meet the same or similar issue, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 16, 2017 9:05 AM
    Moderator
  • Hi my friend,

    Sorry to trouble you again, does your issue is solved or not? If not, please feel free to let me know.


    Sara,

    Sorry but I had to smile. That is not helping, you made the program for him/her 

    You could also have sent the exe.

    If it is a student, then you made his/her homework, not as forum contributors do by convention. 
    With a student you do in fact a bad action because now you give them no chance to learn.



    Success
    Cor



    Monday, January 16, 2017 11:00 AM
  • Hi Cor,

    Thank you for your reminder and I will take care of this next time:)

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 17, 2017 7:26 AM
    Moderator