Hello , i am making an interchange program that switch the values of two variables . For example :

Input: Output:
nr1 = 2 nr1 = 3
nr2 = 3 nr2 = 2

This is my code:

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

namespace ConsoleApplication1
{
    class Programc : Numbers
    {
       public static void Main(string[] args)
        {
            Numbers numbers = new Numbers();
            numbers.GetNr1();
            numbers.GetNr2();
            numbers.InternGhange();

        }
    }

    class Numbers
    {

        public int nr1;
        public int nr2;
        public int temp;

        public void GetNr1()
        {
            
            Console.WriteLine("Read nr1:");
            nr1 = Console.Read();
            Console.ReadLine();
        }


        public void GetNr2()
        {
           
            Console.WriteLine("Read nr2:");
            nr2 = Console.Read();
            Console.ReadLine();

        }

        public void InternGhange()
        {

            temp = nr1;
            nr1 = nr2;
            nr2 = temp;
            Console.WriteLine("The numbers are now:");
            Console.ReadLine();
            Console.WriteLine("nr1={0}",nr1);
            Console.ReadLine();
            Console.WriteLine("nr2={0}",nr2);
        }

    }
}

But if at input nr1=2 and nr2=3 at the output nr1=50 and nr2=51
Where is the problem.Please help me to find out.I will appreciate your help.

Read() method reads the next (only 1 character) from the standard input so you get ASCII codes of the number...
Try that instead:

nr1 = Convert.ToInt32(Console.ReadLine());

But put it into "try catch"...
btw make the fields private... and move temp declaration to InternGhange() function...
Sample code after little corrections:

class Numbers
    {
        private int nr1 = 0;
        private int nr2 = 0;
     
        public void GetNr1()
        {
            Console.WriteLine("Read nr1:");
            try
            {
                nr1 = Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
            Console.ReadLine();
        }
        public void GetNr2()
        {
            Console.WriteLine("Read nr2:");
            try
            {
                nr2 = Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
            Console.ReadLine();
        }
        public void InternGhange()
        {   
            int temp = nr1;
            nr1 = nr2;  
            nr2 = temp;
            Console.WriteLine("The numbers are now:");
            Console.ReadLine();
            Console.WriteLine("nr1={0}",nr1);
            Console.ReadLine(); 
            Console.WriteLine("nr2={0}",nr2);
        }
    }
commented: Nice. +6

Thanks a lot for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.