i have to make program for a hotel (just a scenario) but i'm having problems.
First i have to ask them to choose a room type i.e A/B/C/D which decides how much their room costs per night (a=$22, b=$55 etc). Then i have to ask how many nights they wish to stay then i ask if they're members and if they answer 'y' then they get a 30% discount. Then I display the price.

I tried using switch statements but it didn't really work so i tried if statements, it ended up looking something like this:
..............................................................................................

char roomType, member;
int howManyNights, cost,costWithDiscount;

if(roomType=='a')
console.write("how many nights would you like to stay");
howManyNights=convert.ToChar(console.ReadLine());
cost= howManyNights *22;
costWithDiscount= cost - (cost%30);
console.writeline("are you a member?");
member=convert.ToChar(Console.ReadLine());

if (member == 'y')
console.WriteLine("cost:${0}", costWithDiscount);
if (member == 'n')
console.WriteLine("cost:${0}", cost);

if(roomType=='b')
console.write("how many nights would you like to stay");
howManyNights=convert.ToChar(console.ReadLine());
cost= howManyNights *55;
costWithDiscount= cost - (cost%30);
console.writeline("are you a member?");
member=convert.ToChar(Console.ReadLine());

if (member == 'y')
console.WriteLine("cost:${0}", costWithDiscount);
if (member == 'n')
console.WriteLine("cost:${0}", cost);

etc...
.......................................................................................

its something like that. 'a' always gives me the right costs but 'b' and the others never give me the discount price when i say i'm a member, they just give me the price WITHOUT the discount.

I'm so confused, i thought i was understanding c#, when i read through the code it makes sense to me but its just not working, very frustrating!

I'd appreciate any help, thanks!

Please use code tags:

[code]
...code here....
[/code]


Your logic flow is a little weird to me. Shouldn't you just ask all the questions up front, then use their input in the if statements, verses checking this and asking that, etc.?

Welcome to daniweb! Please use code tags when sharing code on daniweb.

You need to use brackets in your if statements to restrict the program to running relevant code:

if (roomType == 'a')
      {
        console.write("how many nights would you like to stay");
        howManyNights = convert.ToChar(console.ReadLine());
        cost = howManyNights * 22;
        costWithDiscount = cost - (cost % 30);
        console.writeline("are you a member?");
        member = convert.ToChar(Console.ReadLine());

        if (member == 'y')
          console.WriteLine("cost{0}", costWithDiscount);
        if (member == 'n')
          console.WriteLine("cost{0}", cost);
      }
      else if (roomType == 'b')
      {
        console.write("how many nights would you like to stay");
        howManyNights = convert.ToChar(console.ReadLine());
        cost = howManyNights * 55;
        costWithDiscount = cost - (cost % 30);
        console.writeline("are you a member?");
        member = convert.ToChar(Console.ReadLine());

        if (member == 'y')
          console.WriteLine("cost{0}", costWithDiscount);
        if (member == 'n')
          console.WriteLine("cost{0}", cost);
      }

I made the suggested changes (brackets) but i still dont get the discount on 'b', i just get the discount for 'a' :S

class Program
    {
        static void Main(string[] args)
        {
            char roomType, member;
            int howManyNights, cost, costWithDiscount;

            Console.Write("Choose room type a/b/c/d:");
            roomType = Convert.ToChar(Console.ReadLine());
               
      if (roomType == 'a')
   
      {
   
      Console.Write("how many nights would you like to stay");
   
      howManyNights = Convert.ToInt32(Console.ReadLine());
   
      cost = howManyNights * 22;
   
      costWithDiscount = cost - (cost % 30);
   
      Console.WriteLine("are you a member?");
   
      member = Convert.ToChar(Console.ReadLine());
   
       
  
      if (member == 'y')
  
      Console.WriteLine("cost{0}", costWithDiscount);
  
      if (member == 'n')
  
      Console.WriteLine("cost{0}", cost);
  
      }

      else if (roomType == 'b')
  
      {
  
      Console.Write("how many nights would you like to stay");
  
      howManyNights = Convert.ToInt32(Console.ReadLine());
  
      cost = howManyNights * 55;
  
      costWithDiscount = cost - (cost % 30);
  
      Console.WriteLine("are you a member?");
  
      member = Convert.ToChar(Console.ReadLine());
  if (member == 'y')
  
      Console.WriteLine("cost{0}", costWithDiscount);
  
      if (member == 'n')
  
      Console.WriteLine("cost{0}", cost);
  
      }
        }
    }
}

Your discount calculation is incorrect. Here is the correct discount computation for 30% discount:

decimal discountedCost = cost - (cost * 0.30M);

Your discount calculation is incorrect. Here is the correct discount computation for 30% discount:

decimal discountedCost = cost - (cost * 0.30M);

I get "Error 1 A namespace does not directly contain members such as fields or methods" on char roomType, member; :S

I get "Error 1 A namespace does not directly contain members such as fields or methods" on char roomType, member; :S

You probably have unaligned brackets or something. It's telling you that you have defined a field or method outside of your class (Program). If you still have this trouble, post the code from the entire Program.cs file from top to bottom...

Hi,

You have a lot of code repeated in each 'if' statement, the only thing thats affectedby the initial choice is the base cost. Why not collect all the variables first then do a single calculation at the end:

class Program
    {
        static void Main(string[] args)
        {
            double roomPrice, discount, total;
            int noNights;

            Console.Write("Choose room type a/b/c/d:");

            switch (Console.ReadLine())
            {
                case "a":
                    roomPrice = 22;
                    break;
                case "b":
                    roomPrice = 55;
                    break;
                case "c":
                    roomPrice = 88;
                    break;
                case "d":
                    roomPrice = 122;
                    break;
                default:
                    roomPrice = 0;
                    break;
            }

            Console.Write("how many nights would you like to stay?");
            noNights = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("are you a member? y/n");
            switch (Console.ReadLine())
            {
                case "y":
                    discount = 0.3;
                    break;
                case "n":
                    discount = 0;
                    break;
                default:
                    discount = 0;
                    break;
            }

            total = (roomPrice * noNights) * (1 - discount);

            Console.WriteLine("cost = {0}", total);
            Console.ReadLine();
        }
    }

This way the logic is much easier to follow :)

Remember to markthe thread as solved if your question has been answered

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.