0

I am just starting out on my coding journey and attempting to learn C#. I simply want this program to ask the user for two integers named myInt and yourInt and output the total. I feel like I am close to completing this but am running into an issue with the string to integer conversion. Please see code below:

static void Main(string[] args)
        {
            string userInput;
            string user_Input;
            int myInt = 0;
            int yourInt = 0;
            int total = myInt + yourInt;

            Console.WriteLine("What is my Integer?");
            userInput = Console.ReadLine();
            myInt = Convert.ToInt32(userInput);

            Console.WriteLine("What is your integer?");
            user_Input = Console.ReadLine();
            yourInt = Convert.ToInt32(user_Input);

            Console.WriteLine("Our integer is " + total);

        }

I continually receive the following error: "Cannot implicitly convert type 'int' to 'string'...

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • 2
    Hello and welcome to SO! Does this answer your question? [Convert int to string?](https://stackoverflow.com/questions/3081916/convert-int-to-string) Have you done any research before asking this question, if so, what didn't make sense and or has failed? – Trevor Jan 13 '20 at 16:40
  • 1
    Cannot reproduce: that code compiles and runs just fine for me (it prints `0` for the total, but that's expected) – canton7 Jan 13 '20 at 16:41
  • 3
    @Spevacus Not true. Try actually running it - it's fine. The operator `string +(string, object)` is defined by the language. – canton7 Jan 13 '20 at 16:45
  • 1
    @Çöđěxěŕ nope, code compile and run fine. But always return 0. – Orace Jan 13 '20 at 16:47
  • 2
    I cannot reproduce the issue. This runs fine. –  Jan 13 '20 at 16:47
  • 1
    Your title says the issue is `int to string conversion`, but your question says `string to int conversion`, then later it says `int to string conversion`. Which is it? –  Jan 13 '20 at 16:49
  • 2
    I think that closure as a duplicate was premature -- we haven't even found out what the problem is yet! – canton7 Jan 13 '20 at 16:50
  • 3
    @Çöđěxěŕ Why did you add a picture with a different exception that *you* made to happen by deliberately providing invalid input? This is not the exception the OP was quoting. – GSerg Jan 13 '20 at 16:53
  • @GSerg my bad... – Trevor Jan 13 '20 at 16:53
  • @canton7 actually I know how the OP maybe threw this error, set a breakpoint on `userInput = Console.ReadLine();` then double click to edit `userInput` and then type a number to edit it... normal flow though wouldn't throw this... See edit above, I will remove it as well, it's temporary. Sorry for the confusion. – Trevor Jan 13 '20 at 16:57
  • 2
    This code does not throw an exception as long as you enter valid integers when requested. – Honeyboy Wilson Jan 13 '20 at 17:01
  • 1
    @agroves227 Please add more detail. Do you mean "int to string conversion" or "string to int conversion"? Which line throws the exception? Is it a compile-time error, or a run-time error? If it's a run-time error, what input do you type into the console? Please answer these questions. – canton7 Jan 13 '20 at 17:12

2 Answers2

2

Try this:

public static void Main(string[] args)
{
    Console.WriteLine("What is my Integer?");
    int myInt = int.Parse(Console.ReadLine());

    Console.WriteLine("What is your integer?");
    int yourInt = int.Parse(Console.ReadLine());

    int total = myInt + yourInt;
    Console.WriteLine("Our integer is " + total);
}

If you want to handle invalid user input, you can try something like this:

public static void Main(string[] args)
{
    Console.WriteLine("What is my Integer?");

    if (int.TryParse(Console.ReadLine(), out int myInt))
    {
        Console.WriteLine("What is your integer?");

        if (int.TryParse(Console.ReadLine(), out int yourInt))
        {
            int total = myInt + yourInt;
            Console.WriteLine("Our integer is " + total);

            return;
        }
    }

    Console.WriteLine("Invalid input.");
}
Nave Sade
  • 441
  • 2
  • 6
  • This worked! Thank you so much!!! – agroves227 Jan 13 '20 at 16:55
  • 3
    @agroves227 Do note that this is identical to your code, except that this actually computes the total. If your code threw an exception, then this code should also – canton7 Jan 13 '20 at 16:56
  • @canton7 I noticed the same thing and thought I was missing something. My eyebrow can't be raised any higher. Yes, this is identical except for the computation of the total. –  Jan 13 '20 at 16:58
  • @canton7 - So what you two are saying is that the code I originally posted and the edit that Nave sent are the same or should behave the same? – agroves227 Jan 13 '20 at 17:05
  • 3
    @agroves227 Exactly, except that your code will always print the total as `0` (because you never calculate the total), but this code does calculate it. Their exception-throwing behaviour should be identical. – canton7 Jan 13 '20 at 17:06
0

Try the following, first output the string the user entered then add some exception handling in case they enter something non numeric

try 
{
    myInt = Convert.ToInt32(userInput);
    Console.WriteLine("myInt: {0} " , myInt);
}
catch (OverflowException) 
{
    Console.WriteLine("{0} is outside the range of the Int32 type.", value);
}   
catch (FormatException) 
{
    Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
                                                userInput.GetType().Name, userInput);
}   
Paul McCarthy
  • 818
  • 9
  • 24