My name is Carlos and i recently began to learn programming (completely from scratch) through an E-learning class. My tutor is using C# as an introductory language. I searched in the forum and although i found somewhat similar problems, they don't address my current doubts as most questions are of a more advanced content. I'm having a bit of trouble in a exercise regarding Data and Exceptions handling, specifically in the following exercise:
using System;
public class Program
{
public static void Main()
{
int x;
var nr_de_produtos = int.Parse(Console.ReadLine());
int Máximo_por_caixa = 20;
var total_em_falta = Máximo_por_caixa - nr_de_produtos;
string aviso;
switch (nr_de_produtos)
{
case 0:
aviso = "Caixa vazia";
break;
case 20:
aviso = "Caixa cheia";
break;
default:
aviso = "número de unidades em falta:" + total_em_falta;
break;
}
if (int.TryParse(nr_de_produtos, out x))
{
Console.WriteLine("Insira o próximo número");
}
else
{
Console.WriteLine("Não foi inserido um número. Fim da aplicação");
return;
}
Console.WriteLine(aviso);
}
}
It results in the following errors:
Compilation error (line 25, col 7): The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments
Compilation error (line 25, col 20): Argument 1: cannot convert from 'int' to 'string'
The objective is to address the input of the user and turn a string into an int. I believe i´m using int.Parse and int.TryParse wrongly, but not sure how to correct that. Any help would be more than welcome!
Thank you very much! Cheers