0

Currently working on an exercise to create a shopping app, currently working on the check out method of the program where the user selects their payment type through a simple console based program that uses numerical selections. Getting error CS0029 "cannot implicitly convert type int to string"

In the context of the code "input" Is the console.readline(); that the user inputs when asked for their selection.

    public void UserInformation()
    {
        string input = "";


        Console.WriteLine("Please enter your Name");
        string userName = Console.ReadLine();
        Console.WriteLine("Please enter your address");
        Console.Write(">");
        string userAddress = Console.ReadLine();
        Console.WriteLine("Please Select payment type");
        Console.WriteLine("1. Debit");
        Console.WriteLine("2. Credit");
        Console.WriteLine("3. Cash on delivery");
        input = Console.ReadLine();
        Console.Write(">");

         if (input = 1)// gives error CS0029 "cannot implicitly convert 
                                                     type int to string"
         {
            //Debit
            Console.WriteLine($"Your total is {total}");
            Console.WriteLine("Please enter your Debit card Number");
            string userDebit = Console.ReadLine();
            Console.Write(">");
         }
        else
        if (input = 2)
        {
            //Credit
            Console.WriteLine($"Your total is {total}");
            Console.WriteLine("Please select card type");
            Console.WriteLine("1. Visa");
            Console.WriteLine("2. Mastercard");
            Console.WriteLine("3. American Express");
            string userCredit = Console.ReadLine();
            Console.Write(">");
        }
lurker
  • 56,987
  • 9
  • 69
  • 103
Stanley
  • 15
  • 7
  • 1
    First: comparing objects should be done with `==`, not `=`, as the latter is an **assignment**, not a **comparison**. Second: Your inout surely is something like `"1"`, not just `1`. You have to explicetly convert that input to a number, e.g. by using `Convert.ToInt32` or `int.Parse`. – MakePeaceGreatAgain Oct 28 '19 at 15:52
  • You need to convert the string to an int and you must use `==` instead of `=` in `if` condition. So try: `int.TryParse(input, out var value); if (value == 1)` –  Oct 28 '19 at 15:55

1 Answers1

1

The problem seems to be that your IF statements are wrong. If you are comparing a string "Input" to 2. this will need to be done like this:

 if (input == "2")

That was you are Explicitly comparing it to a string, not an Int

jasttim
  • 723
  • 8
  • 19