I im trying to make a simple program that would create book objects and assign them a price title author and number of pages and the price cant be more than 10 cents per page. the program has to throw an exception if the price is more than that and then ask for a new price. I wrote some code but when i compile i get a cs0201 and i cant figure out why (im new to this ).

here is what i have so far

using System;
public class BookExceptions
{
    public static void Main()
    {
        string InputPages ;
        string InputPrice ;
        bool GoodPrice = false ;
        int zero = 0 ;
        double answer;

        Book book1 = new Book();
        Console.Write("Please enter the book's title : ");
        book1.Title = Console.ReadLine();

        Console.Write("Please enter the book's author : ");
        book1.Author = Console.ReadLine();

        Console.Write("Please enter the number of pages in the book : ");
        InputPages = Console.ReadLine();
        book1.Pages = Convert.ToInt32(InputPages);
        book1.MaxPrice = book1.Pages * 0.10;
       
        while (GoodPrice == false)

            Console.Write("Please enter the book's price :  ");
            InputPrice = Console.ReadLine();
            book1.Price = Convert.ToDouble(InputPrice);

            if (book1.Price <= book1.MaxPrice)
            {
                GoodPrice == true;
            }
            else
                try
                {
                    answer = book1.Price / zero;
                }
                catch (BookException exceptionbook1)
                {
                    Console.Write(exceptionbook1.Message);
                    Console.WriteLine(" For {0} the maximum price allowed is {1}.", book1.Title, book1.MaxPrice);
                }

        
        Console.WriteLine("The title of Book 1 is {0} by {1} it contains {2} pages and is priced at {3}$.", book1.Title, book1.Author, book1.Pages, book1.Price);
        Console.ReadLine();

        
    }






    public class Book
    {
        public string Title { get; set; }
        public string Author { get; set; }
        public int Pages { get; set; }
        public double Price { get; set; }
        public double MaxPrice { get; set; }

    }

    public class BookException : Exception
    {

         private static string msg = ("The ratio is invalid.");
         public BookException() : base(msg) { }
        
        
    }
    
}

any help would be appreciated

Recommended Answers

All 4 Replies

it would be more helpfull if you pointed the line where compiler finds the error.

But this statement(or logic) is not quite correct.

try                
{                    
answer = book1.Price / zero;                
}

You will get at runtime "Division by zero" and not a BookException. Use "throw" instead.

try
{ 
     throw new BookException()
}

i tried what you said but im still getting the same error line (32, 17)

GoodPrice == true; //use "=", "==" it's used for checking the equality, not for assignments

Cool thx that did thx a bunch

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.