0

I am having trouble with this project. The project is to use user input to take scores. Using a loop that allows the user to input as many scores that they want. I am having a difficult time passing a variable to my ConvertToInt method. I am wanting to pass the variable, with the value, score to the ConvertToInt method. In the ConvertToInt method I have to convert the string to an int. What I have coded now will run but after entering a score the System.FormatException is thrown. I am new to C# and do not quite understand what is wrong.

using System;

namespace MethodsAndUnitTestsMethodTestsCampbell
{
    class Program
    {
        private static string GetUserInput(string scores)
        {
            Console.WriteLine("Enter score: ");
            string score = Console.ReadLine();
            score = scores;
            return score;
        }

        private static int ConvertToInt(string scores)
        {
            return Convert.ToInt32(scores);
        }

        static void Main(string[] args)
        {
            string scores = "{C} ";
            GetUserInput(scores);
            ConvertToInt(scores);
            

        }
    }
}
1
  • Where is your GetUserInput method? Right now, you are passing the string '{C} ' to your ConvertToInt method, and that definitely can't be converted to an int. Commented Oct 2, 2021 at 3:30

3 Answers 3

2

You can use Int32.TryParse(String, Int32) to convert the string. Your code will be like this:

class Program
{
    private static void ConvertToInt32(string input, ref int output)
    {
        if (int.TryParse(input, out int number))
        {
            output = number;
        }
        else
        {
            System.Console.WriteLine($"Cannot convert {input}.");
        }
    }

    private static string GetUserInput(string prompt)
    {
        System.Console.Write(prompt);
        return System.Console.ReadLine();
    }

    static void Main(string[] args)
    {
        string input = GetUserInput("Enter scores: ");
        int scores = 0;
        ConvertToInt32(input, ref scores);
        System.Console.WriteLine($"Scores: {scores}");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are not assigning the return value of GetUserInput method to scores variable. Below is updated program -

class Program
{
   private static int ConvertToInt(string scores)
   {
       return Convert.ToInt32(scores);
   }
   private static string GetUserInput(string scores) 
   { 
       Console.WriteLine("Enter score: "); 
       scores = Console.ReadLine();            
       return scores; 
   }

   static void Main(string[] args)
   {
       string scores = "{C} ";
       scores = GetUserInput(scores);
       ConvertToInt(scores);
   }
}

Comments

0

The problem is that you didn't assigned the output value of your GetUserInput method to your string and you are passing the "{C} " value that you assigned to the variable to your ConvertToInt32 method and C# can't convert that value to int ! Repleace your Main method :

string scores = "{C} ";
GetUserInput(scores);
ConvertToInt(scores);

With this lines of code :

string scores = GetUserInput(scores);
int numericResult = ConvertToInt(scores);

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.