0

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

  • Do you actually pay money for these ELearning Courses..? don't pay money when there are tons of free tutorials online using google as well as YouTube.. do a google search on `int32.TryParse` and you will quickly see what the problem and or issue is.. – MethodMan Jun 24 '16 at 17:09
  • http://stackoverflow.com/questions/4620565/int-tryparse-syntatic-sugar – MethodMan Jun 24 '16 at 17:11
  • Thank you for your reply! It´s a free class in college. The problem is that i can't use int32.TryParse, at least not yet. We can only make do with what we learned, like: int x; var valor_x = Console.ReadLine(); if (int.TryParse(valor_x, out x)) { Console.WriteLine(“Insert next number”); } else { Console.WriteLine(“A number was not inserted”); return; } – lapa_arkeo Jun 24 '16 at 17:13
  • not necessary.. `Int.TryParse` is what I meant to say.. – MethodMan Jun 24 '16 at 17:14
  • Oh ok, i will try to search and correct it then. Thanks! – lapa_arkeo Jun 24 '16 at 17:17

2 Answers2

2

You're trying to parse nr_de_produtos to an int:

int.TryParse(nr_de_produtos, out x)

But it's already an int:

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

There's no need to convert an int to an int.

It seems like you expect that input to be a string which might not represent an int. In which case your attempt to TryParse is the safer approach. If that's the case, remove the Parse (which is less safe) and keep the input as a string:

var nr_de_produtos = Console.ReadLine();

Edit: As @Pikoh points out in a comment below, this block would also need to move:

switch (nr_de_produtos)
{
    //...
}

Since nr_de_produtos is no longer an int, you're going to want to switch on x after the TryParse operation. (The compiler will hint at this when you make your changes.)

David
  • 208,112
  • 36
  • 198
  • 279
  • `var nr_de_produtos = int.Parse(Console.ReadLine());` what if the user entered `X` – MethodMan Jun 24 '16 at 17:12
  • 1
    @MethodMan: Then `int.Parse()` would of course throw an exception. It's probably safer for the OP to just keep the input as a string and use that `TryParse()` approach. Clearly he doesn't need both :) – David Jun 24 '16 at 17:13
  • I know that it would what I am saying is that that line would error out if the op happened to read in `Y` for example – MethodMan Jun 24 '16 at 17:15
  • You miss something in your answer. The `switch` would need to be inside the `if (int.TryParse(nr_de_produtos, out x))`part – Pikoh Jun 24 '16 at 17:19
  • @Pikoh: Ah, good catch. Indeed, some of the logic of this small program would need to be re-arranged a bit. In lieu of completely re-writing it and doing the OP's homework for him though, I'll opt to address merely the error at hand. Hopefully the OP can continue to drive toward a workable solution. – David Jun 24 '16 at 17:21
  • I suspected i was misusing those , but was unable to discern it. Thank you for the quick reply! – lapa_arkeo Jun 24 '16 at 17:22
  • I know David, noone wants to hive him the complete code. But the problem OP is a newbie, and would be nice to warn him that he should first check that the input is an int before trying to process it :) – Pikoh Jun 24 '16 at 17:22
  • @lapa_arkeo: I've updated the answer to include some hints based on helpful suggestions in these comments. Good luck! – David Jun 24 '16 at 17:23
  • Thank you all! As i said i'm a complete newbie in the programming world, yearning to learn, and am glad to find such a helpful community here. Cheers! – lapa_arkeo Jun 24 '16 at 17:26
0

nr_de_produtos is already a integer

int.TryParse(nr_de_produtos, out x)

that's your mistake you should do something like

var nr_de_produtos = Console.ReadLine();

and then try to parse it to integer.

pedrommuller
  • 15,741
  • 10
  • 76
  • 126