Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to read input as integer in console.readline my data type is int .. or how to take string and int in console.readline..
seee my postal code it show error when i input

What I have tried:

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

namespace ConsoleApplication1
{
    class Program
    {
      

        static void Main(string[] args)
        {
            string firstname;
            string lastname;
            string birthdate;
            string addressline1;
            string adressline2;
            string city;
            string stateorprovince;
            int    ziporpostalcode;
            string country;

            Console.WriteLine("enter your first name");
            firstname = Console.ReadLine();
            Console.WriteLine("enter last name");
           lastname = Console.ReadLine();
            Console.WriteLine("brithdate");
            birthdate =Console.ReadLine();
            Console.WriteLine("address line 1");

           addressline1 = Console.ReadLine();
            Console.WriteLine("addres line 2");
            adressline2= Console.ReadLine();
            Console.WriteLine("city");
            city = Console.ReadLine();
            Console.WriteLine("state/province");
            stateorprovince = Console.ReadLine();
            Console.WriteLine("zip or postal code");
            ziporpostalcode =int.Parse(Console.ReadLine());
            Console.WriteLine("country");
            country = Console.ReadLine();
        }
    }
}
Posted
Updated 12-Apr-16 19:34pm
v2
Comments
PIEBALDconsult 13-Apr-16 0:10am    
I would store the string from the Readline and then use int.TryParse afterward. Don't try to do both at once.
https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

try this

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

namespace ConsoleApplication1
{
    class Program
    {


        static void Main(string[] args)
        { 
            int ziporpostalcode;            
            Console.WriteLine("zip or postal code");
            ziporpostalcode = GetZipCode();
          
        }

        private static int GetZipCode()
        {
            int ziporpostalcode;
            here:
            string zip = Console.ReadLine();
            if (!int.TryParse(zip, out ziporpostalcode))
            {
                Console.WriteLine("Please enter a valid zip code");
                goto here;
            }
            return ziporpostalcode;
        }
    }
}
 
Share this answer
 
Hi,


Try below code.

C#
ziporpostalcode =Convert.ToInt32(Console.ReadLine());


Thanks,
Raghuveer
 
Share this answer
 
Comments
PIEBALDconsult 13-Apr-16 0:07am    
All that does is call int.Parse which he already says he tried.
In general, don't use Convert, when there's a more direct option.
Ahmed Shah 14-Apr-16 10:52am    
direct like parse
use TryParse, if you will enter int then it will convert as it is, but if you will enter string in zip code it will give you 0.

C#
static void Main(string[] args)
       {

           string firstname;
           string lastname;
           string birthdate;
           string addressline1;
           string adressline2;
           string city;
           string stateorprovince;
           int ziporpostalcode;
           string country;

           Console.WriteLine("enter your first name");
           firstname = Console.ReadLine();
           Console.WriteLine("enter last name");
           lastname = Console.ReadLine();
           Console.WriteLine("brithdate");
           birthdate = Console.ReadLine();
           Console.WriteLine("address line 1");

           addressline1 = Console.ReadLine();
           Console.WriteLine("addres line 2");
           adressline2 = Console.ReadLine();
           Console.WriteLine("city");
           city = Console.ReadLine();
           Console.WriteLine("state/province");
           stateorprovince = Console.ReadLine();
           Console.WriteLine("zip or postal code");
            Int32.TryParse(Console.ReadLine(), out ziporpostalcode);
           Console.WriteLine("country");
           country = Console.ReadLine();

       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900