C # label and goto

This is my complete program. Obviously I'm a beginner. The problem I'm running into is that if the age is less than zero then it goes back to multiple lines, but it asks the user again for a pin. :( What can I do to fix this?

 using System;

    namespace Examples
    {
        class Program
        {
            static void Main(string[] args)
            {
                string name;
                string city;
                int age;
                int pin;

                // \n is used for line-break
                Console.Write("Enter your name :  ");
                name = Console.ReadLine();

                Console.Write("\nEnter Your City :  ");
                city = Console.ReadLine();

                age:
                Console.Write("\nEnter your age :  ");
                age = Int32.Parse(Console.ReadLine());

                Console.Write("\nEnter your pin :  ");
                pin = Int32.Parse(Console.ReadLine());

                if (age < 0 || age >= 110)
                {
                    goto age;

                }


                // Printing message to console
                //formatting output
                Console.WriteLine("==============");
                Console.WriteLine("Your Complete Address:");
                Console.WriteLine("============\n");

                Console.WriteLine("Name = {0}", name);
                Console.WriteLine("City = {0}", city);
                Console.WriteLine("Age = {0}", age);
                Console.WriteLine("Pin = {0}", pin);
                Console.WriteLine("===============");

                Console.ReadLine();
            }
        }
    }

      

+3


source to share


5 answers


So, if I understand this correctly, you want to indicate the age if it is less than 0 or greater than 110?

First of all, don't go around with labels, they are ugly and you don't want to use them, you can use the do while loop instead, but there are many possibilities:



Also, instead of syntax, \n

you can also useConsole.WriteLine

string name;
string city;
int age;
int pin;

// \n is used for line-break
Console.Write("Enter your name :  ");
name = Console.ReadLine();

Console.Write("\nEnter Your City :  ");
city = Console.ReadLine();
age = -1;

while (age < 0 || age >= 110)
{
    Console.Write("\nEnter your age :  ");
    age = Int32.Parse(Console.ReadLine());

    if (age < 0 || age >= 110)
    {
        Console.WriteLine("The age must be between 0 and 110.");
    }
}


Console.Write("\nEnter your pin :  ");
pin = Int32.Parse(Console.ReadLine());


// Printing message to console
//formatting output
Console.WriteLine("==============");
Console.WriteLine("Your Complete Address:");
Console.WriteLine("============\n");

Console.WriteLine("Name = {0}", name);
Console.WriteLine("City = {0}", city);
Console.WriteLine("Age = {0}", age);
Console.WriteLine("Pin = {0}", pin);
Console.WriteLine("===============");

Console.ReadLine();

      

+3


source


u can use the concept of functions here and u should use functions

            Console.Write("\nEnter your age :  ");
            age = Int32.Parse(Console.ReadLine());
            if (age < 0 || age >= 110)
            {
               //show error msg

            }
            Console.Write("\nEnter your pin :  ");
            pin = Int32.Parse(Console.ReadLine());

           //if every data is corrent - run function
           showinfo();   
////////////
           showinfo()
           { 
            Console.WriteLine("==============");
            Console.WriteLine("Your Complete Address:");
            Console.WriteLine("============\n");

            Console.WriteLine("Name = {0}", name);
            Console.WriteLine("City = {0}", city);
            Console.WriteLine("Age = {0}", age);
            Console.WriteLine("Pin = {0}", pin);
            Console.WriteLine("===============");

            Console.ReadLine();}

      



as @noctis pointed out .. use goto should be avoided .. its creates problems ... also please check - negetivedumber of exceptional concepts ... u can use ur exception too

+1


source


You can set the contact to -1 and check it.

Also, you can get into a religious war with this GOTO expression ... :) usually he frowned, it can certainly be avoided here, but some people will say it's okay. up to you really.

I'll add some code and paste it.

void Main()
{
    string name;
    string city;
    int age;
    int pin;

    // \n is used for line-break
    Console.Write("Enter your name :  ");
    name = Console.ReadLine();

    Console.Write("\nEnter Your City :  ");
    city = Console.ReadLine();

    age = GetAge();

    //... eluded

Console.ReadLine();
}

// Define other methods and classes here
private int GetAge() {
    Console.Write("\nEnter your age :  ");
    int age = -1;
    while (age <0 || age>110) {
        age = Int32.Parse(Console.ReadLine());
    }
    return age;
}

      

using a method would be more appropriate in this case :)

0


source


Goto Label is a pretty old relic from the very beginning of C, you should avoid it. You can achieve the same by implementing a loop

Console.WriteLine("Enter your pin :  ");
pin = Int32.Parse(Console.ReadLine());
while (age < 0 || age >= 110)
{
   Console.WriteLine("Enter your age :  ");
   int age = Int32.Parse(Console.ReadLine());
}

      

0


source


Just move your condition in front of the pin request.

age:
Console.Write("\nEnter your age :  ");
age = Int32.Parse(Console.ReadLine());

if (age < 0 || age >= 110)
{
    goto age;
}

Console.Write("\nEnter your pin :  ");
pin = Int32.Parse(Console.ReadLine());

      

-1


source







All Articles