user2224223
user2224223

Reputation: 45

How to fix the best overloaded method match has some invalid arguments?

I am getting the error in the title, what is wrong with the code? I think its a syntax error, but I'm not sure as I don't have much information on what the error actually means.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

class Program
{

    static void Main(string[] args)
    {

        Console.WriteLine("Please Input Number of Rows you want to make in your pyrimid: ");

        int num = int.Parse(Console.Read()); // error here

        Console.WriteLine(num);// Just to check if it is getting the right number
        Console.Read();//This is Here just so the console window doesn't close when the program runs

    }
}
}

Edit:

Just to Clarify, I want the code just to get a number from the user and then print the number the user inputted.

Upvotes: 3

Views: 46426

Answers (4)

Ilya Ivanov
Ilya Ivanov

Reputation: 23636

int.Parse accepts a string as parameter. Use Console.ReadLine() to get a string from user and then pass it into int.Parse

int num = int.Parse(Console.ReadLine());

Note that this will throw FormatException if the user will enter something not recognisable as int. If you are not sure, that the user will input a good number (I always don't), use TryParse. Example is given below

int value;

if (int.TryParse(Console.ReadLine(), out value))
    Console.WriteLine("parsed number as: {0}", value);
else
    Console.WriteLine("incorrect number format");

Upvotes: 9

Daniel Imms
Daniel Imms

Reputation: 50229

Console.Read() and ASCII

This is occurring because Console.Read() actually returns and int, not a string. It returns the ASCII code of the key that was pressed, you will need to convert it to a char then to a string and then parse it.

var val = int.Parse(((char)Console.Read()).ToString());

Note that Console.Read() will not return the integer in the format you think, values 0 to 9 actually come out at 60 to 70 as they're the key codes not the characters you pressed.

See the ASCII table here

Console.ReadLine()

An alternative and probably better solution would be to use Console.ReadLine() which returns a string

var val = int.Parse(Console.ReadLine());

Warning

You should always be careful when using int.Parse() as it will throw an exception if the string provided is not numeric. A better option is to use int.TryParse() which you give an out argument and it returns whether parsing was successful.

string text = Console.ReadLine();
int val;
if (int.TryParse(text, out val))
{
    // It is a number
}
{
    // It is not a number
}

Upvotes: 1

Haedrian
Haedrian

Reputation: 4328

The reason you're getting that, is because Console.Read returns an int

http://msdn.microsoft.com/en-us/library/system.console.read.aspx

And it can't parse an int, it can only parse strings.

You probabily want Console.ReadLine - which returns a string.

Upvotes: 0

VladL
VladL

Reputation: 13043

The problem is that Console.Read() returns an int, but int.Parse is expecting a string. Simply change it to

int num =Console.Read();

Upvotes: 1

Related Questions