How to start the program from the beginning

Danish Zubair

New member
Joined
Nov 10, 2022
Messages
2
Programming Experience
Beginner
Hi, I am having a big issue, in trying to understand how to go back to the beginning of the program, once it reaches the end. I have done several things such as goto Start, Restart() and Application.Restart(), but none of them are working. I have tried to add the code in a for loop or a while loop, but I don't understand how to add the loop from the beginning of the program. The program is about a ticket booking system, where users get asked about destination they want to travel, any hotel they want to include, number of adults and it also, applies 50% discount if you have 1 or more child, then it should go to the start and allow the user to book another ticket, without pressing the run button. Here is the file of the code. Thank you.
 

Attachments

  • Unit 4 C# code.txt
    9.3 KB · Views: 18
Solution
C#:
static void Main(){
  while(true){
    //do your code here
  }
}

When you want to exit the loop and end the program, do a break
goto should be a keyword of last resort. It should only be used in the most extreme of circumstances, and even then with a lot of forethought and deliberation. Code with gotos very quickly grow to have more gotos making it spaghetti code.

If you are just learning to program, always try to try restructure your code to use the common looping mechanisms.
 
I have tried, bit in this case, how you suggest should I write the for loop. I knwo the syntax, but since I wat it to start from beginning, how should I use the for loop?
 
C#:
static void Main(){
  while(true){
    //do your code here
  }
}

When you want to exit the loop and end the program, do a break
 
Solution
For reference, I'm putting the code from the attachment here:
Unit 4:
using System;

public class HelloWorld
{
    static public void Main()
    {
        Console.WriteLine("Welcome to the ticket booking system. \n\nPlease select one of the following destinations: ");

        string a = "UK";
        string b = "France";
        string c = "Spain";
        //    int duration;//
        string hotelchoice;
        int adults;
        int children;
        int Ukcost = 0;
        int Francecost = 120;
        int Spaincost = 200;
        int priceUKhotel1 = 1584;
        int priceUKhotel2 = 519;
        int priceUKhotel3 = 1357;
        int priceParishotel1 = 299;
        int priceParishotel2 = 1027;
        int priceParishotel3 = 1104;
        int priceSpainhotel1 = 183;
        int priceSpainhotel2 = 562;
        int priceSpainhotel3 = 455;

        Console.WriteLine("Destinations available:\n\n{0} \n{1} \n{2}", a, b, c);

        string d = Console.ReadLine();


        if (d == "UK")
        {
            Console.WriteLine("You have chosen {0}", d);
        }
        else if (d == "France")
        {
            Console.WriteLine("You have chosen {0}", d);
        }
        else if (d == "Spain")
        {
            Console.WriteLine("You have chosen {0}", d);
        }
        else
        {
            Console.WriteLine("Error");
        }

        if (d == "UK")
        {
            Console.WriteLine("Return travel cost: £0");
        }
        else if (d == "France")
        {
            Console.WriteLine("Return travel cost: £120");
        }
        else if (d == "Spain")
        {
            Console.WriteLine("Return travel cost: £200");
        }
        else
        {
            Console.WriteLine("Error");
        }


        Console.WriteLine("Hotels available: ");

        if (d == "UK")
        {
            Console.WriteLine("The tower hotel London:            Week price: £1584");
            Console.WriteLine("Britannia international hotel:     Week price: £519");
            Console.WriteLine("The midland:                       Week price: £1357");
        }
        else if (d == "France")
        {
            Console.WriteLine("The people Paris marais:             Week price: £299");
            Console.WriteLine("Novotel Paris centre tour eiffel:    Week price: £1027");
            Console.WriteLine("Raddison blu hotel, Nice:            Week price: £1104");
        }
        else if (d == "Spain")
        {
            Console.WriteLine("The hat Madrid:                       Week price: £183");
            Console.WriteLine("Hotel nuevo Madrid:                   Week price: £562");
            Console.WriteLine("Doubletree by hilton hotel Girona:    Week price: £455");
        }
        else
        {
            Console.WriteLine("Error");
        }

        Console.WriteLine("Please enter the chosen hotel: ");
        hotelchoice = Console.ReadLine();


        Console.WriteLine("Please enter the number of weeks you want to stay in the hotel: ");

        int duration = Convert.ToInt32(Console.ReadLine());
        if (hotelchoice == "The tower hotel London")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceUKhotel1 * duration);
        }
        else if (hotelchoice == "Britannia international hotel")
        {
            Console.WriteLine("The price of your stay: ");
            Console.WriteLine("£" + priceUKhotel2 * duration);
        }
        else if (hotelchoice == "The midland")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceUKhotel3 * duration);
        }
        else if (hotelchoice == "The people Paris marais")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceParishotel1 * duration);
        }
        else if (hotelchoice == "Novotel Paris centre tour eiffel")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceParishotel2 * duration);
        }
        else if (hotelchoice == "Raddison blu hotel, Nice")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceParishotel3 * duration);
        }
        else if (hotelchoice == "The hat Madrid")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceSpainhotel1 * duration);
        }
        else if (hotelchoice == "Hotel nuevo Madrid")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceSpainhotel2 * duration);
        }
        else if (hotelchoice == "Doubletree by hilton hotel Girona")
        {
            Console.WriteLine("The price of your hotel stay: ");
            Console.WriteLine("£" + priceSpainhotel3 * duration);
        }
        else
        {
            Console.WriteLine("Error");
        }

        Console.WriteLine("Please enter the total number of adults: ");
        adults = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Please enter the total number of children travelling(under 12): ");
        children = Convert.ToInt32(Console.ReadLine());


        if (d == "UK" & hotelchoice == "The tower hotel London")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Ukcost + priceUKhotel1 * duration * adults);
        }

        else if (d == "UK" & hotelchoice == "Britannia international hotel")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Ukcost + priceUKhotel2 * duration * adults);
        }

        else if (d == "UK" & hotelchoice == "The midland")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Ukcost + priceUKhotel3 * duration * adults);
        }

        else if (d == "France" & hotelchoice == "The people Paris marais")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Francecost + priceParishotel1 * duration * adults);
        }

        else if (d == "France" & hotelchoice == "Novotel Paris centre tour eiffel")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Francecost + priceParishotel2 * duration * adults);
        }

        else if (d == "France" & hotelchoice == "Raddison blu hotel, Nice")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Francecost + priceParishotel3 * duration * adults);
        }

        else if (d == "Spain" & hotelchoice == "The hat Madrid")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Spaincost + priceSpainhotel1 * duration * adults);
        }

        else if (d == "Spain" & hotelchoice == "Hotel nuevo Madrid")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Spaincost + priceSpainhotel2 * duration * adults);
        }

        else if (d == "Spain" & hotelchoice == "Doubletree by hilton hotel Girona")
        {
            Console.WriteLine("Total price: ");
            Console.Write("£");
            Console.WriteLine(Spaincost + priceSpainhotel3 * duration * adults);
        }

        else
        {
            Console.WriteLine("Error");
        }

        if (children >= 1 & hotelchoice == "The tower hotel London")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Ukcost + priceUKhotel1 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "Britannia international hotel")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Ukcost + priceUKhotel2 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "The midland")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Ukcost + priceUKhotel3 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "The people Paris marais")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Francecost + priceParishotel1 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "Novotel Paris centre tour eiffel")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Francecost + priceParishotel2 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "Raddison blu hotel, Nice")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Francecost + priceParishotel3 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "The hat Madrid")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Spaincost + priceSpainhotel1 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "Hotel nuevo Madrid")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Spaincost + priceSpainhotel2 * duration * adults) / 2);
        }
        else if (children >= 1 & hotelchoice == "Doubletree by hilton hotel Girona")
        {
            Console.WriteLine("Total price, including child discount: ");
            Console.Write("£");
            Console.WriteLine((Spaincost + priceSpainhotel3 * duration * adults) / 2);
        }




    }
}

//Add the children discount of 50%//


/*        Console.WriteLine("Welcome to ticket booking system.\n\nPlease select one of the following destinations: ");
        string a;
        string b;
        string c;
        
       Console.WriteLine("Destinations available: ");
      
       Console.WriteLine("Islamabad");
      
       Console.WriteLine("Abu dhabi");
    
       Console.WriteLine("Milan");
      
       a = Console.ReadLine();
       if(a == "Islamabad" || "Abu dhabi" || "Milan")
       {
           Console.WriteLine("You have choosen {0}", a);
       }
       else
       {
           Console.WriteLine("Error");
       }*/


Anyway when you find yourself naming variables with a number suffix like:
C#:
int priceUKhotel1;
int priceUKhotel2;
int priceUKhotel3;
That is usually a hint that you should be using an array:
C#:
int [] priceUKHotel;

And so you could use it like:
C#:
int [] priceUKHotel = new int[] { 1584, 519, 1357 };

When you find yourself naming variables with a name to associate them with each other like:
C#:
int UkCost = 0;
int [] priceUKhotel = new int[] { 1584, 519, 1357 };

int FranceCost = 120;
int [] priceParisHotel = new int[] { 299, 1027, 1104 };

int SpainCost = 200;
int [] priceSpainHotel = new int[] { 183, 562, 455 };
it is usually hint that you should be keeping them together using a class, struct, or record like:
C#:
class Package
{
    public string Destination { get; set; }
    public int Cost { get; set; }
    public int[] HotelPrices { get; set; }
}
:
Package[] packages = new Package []
{
    new Package() { Destination = "UK", Cost = 0, HotelPrices = new int [] { 1584, 519, 1357 } },
    new Package() { Destination = "France", Cost = 120, HotelPrices = new int [] { 299, 1027, 1104 } },
    new Package() { Destination = "Spain", Cost = 200, HotelPrices = new int [] { 183, 562, 455 } },
};

And there is also other information that is associate with hotels: it's name. So you should eventually end up with something like:
C#:
class Hotel
{
    public string Name { get; set; }
    public int Price { get; set; }
}

class Package
{
    public string Destination { get; set; }
    public int Cost { get; set; }
    public Hotel[] Hotels { get; set; }
}
:
Package[] packages = new Package []
{
    new Package()
    {
        Destination = "UK",
        Cost = 0,
        Hotels = new Hotel []
        {
            new Hotel() { Name = "The Tower Hotel London", Cost = 1584 },
            new Hotel() { Name = "Britannia International Hotel", Cost = 519 },
            new Hotel() { Name = "The Midland", Cost = 1357 },
        }
    },
    :
};

And eventually, you should not be using arrays, and take advantage of List<T>.
 
I hope that the following code demonstrates why you want to take advantage of those Package and Hotel classes. Although the code below uses the newer records, just think of them as glorified classes or structs. The SelectDestination() lets the user pick their destination from a list of destinations. Similarly SelectHotel lets the user pick hotel from a list of hotels. The destination object holds that list of hotels.

The method GetInteger() just prompts the user to enter an integer value within the min-max range of values. By just getting an integer for the users choice, it simplifies determining which list item was selected.

C#:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

List<Destination> destinations = new()
{
    new("UK", 0, new()
    {
        new("The Tower Hotel London", 1584),
        new("Britannia International Hotel", 519),
        new("The Midland", 1357),
    }),
    new("France", 120, new()
    {
        new("The People Paris Marais", 299),
        new("Novotel Paris Centre Tour Eiffel", 1027),
        new("Raddison Blu Hotel, Nice", 1104),
    }),
    new("Spain", 200, new()
    {
        new("The Hat Madrid", 183),
        new("Hotel Nuevo Madrid", 562),
        new("Doubletree by Hilton Hotel Girona", 455)
    })
};

var britishCulture = new CultureInfo("en-GB");
CultureInfo.CurrentCulture= britishCulture;
CultureInfo.CurrentUICulture = britishCulture;

while (true)
{
    Console.WriteLine("Welcome to the Ticket Booking System");
    Console.WriteLine();

    var destination = SelectDestination(destinations);
    Console.WriteLine();

    var hotel = SelectHotel(destination.Hotels);
    Console.WriteLine();

    var weeks = GetInteger($"How many weeks will you be staying at the {hotel.Name}? ");
    var adults = GetInteger($"How many adults will be going to {destination.Name}? ");
    var children = GetInteger($"How many children will be going to {destination.Name}? ");

    var cost = destination.BaseCost + hotel.WeeklyRate * weeks * adults;
    if (children >= 1)
        cost = cost / 2;

    Console.WriteLine();
    Console.WriteLine($"The total price of your trip to {destination.Name} staying at the {hotel.Name} is {cost:C}.");
    Console.WriteLine();
}

int GetInteger(string prompt, int max = Int32.MaxValue, int min = 0)
{
    while (true)
    {
        Console.Write(prompt);
        var input = Console.ReadLine();
        if (int.TryParse(input, out int value))
        {
            if (min <= value && value <= max)
                return value;

            Console.WriteLine($"Please enter an integer in the range {min}-{max}.");
            continue;
        }
        Console.WriteLine("Invalid integer.");
    }
}

Destination SelectDestination(List<Destination> destinations)
{
    Console.WriteLine("Please select one of the following destinations:");
    for (int i = 0; i < destinations.Count; i++)
        Console.WriteLine($"{i + 1}: {destinations[i].Name,-10}  Base Cost: {destinations[i].BaseCost,8:C}");

    int option = GetInteger("Where would you like to go? ", max: destinations.Count, min: 1); ;
    return destinations[option - 1];
}

Hotel SelectHotel(List<Hotel> hotels)
{
    Console.WriteLine("Please select one of the following hotels:");
    for (int i = 0; i < hotels.Count; i++)
        Console.WriteLine($"{i + 1}: {hotels[i].Name,-40}  Weekly Rate: {hotels[i].WeeklyRate,10:C}");

    int option = GetInteger("Where would you like to stay? ", max: hotels.Count, min: 1);
    return hotels[option - 1];
}

record Hotel(string Name, decimal WeeklyRate);

record Destination(string Name, decimal BaseCost, List<Hotel> Hotels);
 
But don't just take SD's code and hand it in; it will be blindingly obvious that someone else has done it for you
 
Back
Top Bottom