hallkids1
hallkids1

Reputation: 3

How can the user input the word "stop" and the code shuts off?

I just need help because I couldn't find the answer I was looking for. I want this code to stop as soon as the user inputs the word stop. If their is anymore details you want me to add just let me know.

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

namespace LibraryWork
{

    class Program
    {
        static void Main(string[] args)
        {
            var bookList = new List<string>();
            string ansSearch = String.Empty;
            string search = String.Empty;
            int i = 1;
            for (int zero = 0; i > zero; i++)
            {
                Console.Write("Type ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("'New'");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(" if you would you like to enter a new book. Type ");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("'List' ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("to see a list of books entered. Type ");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("'Search' ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("to look up a specific book.");
                Console.WriteLine();
                string answer = Console.ReadLine();

                if (answer == "New")
                {
                    Console.Write("Please format the Entry of your book as follows: ");
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("'Name of the Book',");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("'Author (first, last)',");
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.Write("'Category',");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.Write("'Dewey Decimal Number'.");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine();
                    bookList.Add("Entry " + i + ": " + Console.ReadLine());
                    continue;
                }
                if (answer == "List")
                {
                    bookList.ForEach(Console.WriteLine);
                    Console.WriteLine("Press enter to continue");
                    Console.ReadLine();
                    i--;
                    continue;
                }
                if (answer == "Search")
                {
                    Console.WriteLine("What would you like to search for (Title: Full Title; Author: first, last): ");
                    search = Console.ReadLine();
                    var results = bookList.Where(x => x.Contains(search)).ToList();
                    bool isEmpty = !results.Any();
                    if (isEmpty)
                    {
                        i--;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Sorry, we could not find that.");
                        Console.ForegroundColor = ConsoleColor.White;
                        continue;
                    }
                    foreach (var result in results)
                    {
                        Console.WriteLine(result);
                    }
                    Console.WriteLine("Press Enter to continue");
                    Console.ReadLine();
                    results.Clear();
                    i--;
                    continue;
                }
                i--;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Incorrect Response, please try again");
                Console.ForegroundColor = ConsoleColor.White;
            }

        }
    }

}

Upvotes: 0

Views: 939

Answers (1)

ThePerplexedOne
ThePerplexedOne

Reputation: 2950

if(answer == "Stop")
{
    Environment.Exit(0);
}

Will exit the program. (Using return; will have the same affect)

Or, you can just use break; to escape the for loop:

if(answer == "Stop")
{
    break;
}

I'd recommend going with using break;, so that you can execute any other bits of code before the program closes.

EDIT

Another user has mentioned this, but your for loop is badly constructed. You don't need to decrement the value of i to make the for loop run endlessly.

Start by getting rid of every i--; in your code.

You have two options for an infinite loop:

1. while(true) - This is most commonly used, it's more readable and conventional to use this method.

2. for(;;) - Does the same thing, essentially, but it is far less common.

Upvotes: 4

Related Questions