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!

Dani AI

Generated

The behavior described usually comes from two separate issues: how the discount is calculated, and how the inputs/flow are handled. was right to point out scope/braces — missing or mis-scoped blocks can let code run in the wrong place. caught the math problem — using the wrong operation for “30% off” will produce wrong numbers. ’s suggestion to gather inputs first and calculate once is the cleanest fix.

Concrete checklist to make the program reliable

  • Do not compute money with integer math; use a floating type (preferably decimal) so percentages produce fractional results.
  • Calculate the discount by multiplying by the discount percentage (or by 1 minus that percentage). The modulus operator is for remainders and will not give a percent.
  • Read and validate input as strings first, normalize case, then take the first character. This avoids Convert.ToChar surprises when the user hits Enter or types extra text.
  • Keep the prompt/validation separate from the pricing logic: map the room type to a base rate, read nights and membership, then do one final calculation and print.
  • If you see "A namespace does not directly contain members..." check for mismatched braces or code declared outside the Main method/class.

Safe input pattern (illustrative)

string raw = Console.ReadLine();
if (string.IsNullOrWhiteSpace(raw)) { /* reprompt */ }
char choice = char.ToLower(raw.Trim()[0]);

Quick debug tips: add temporary Console.WriteLine statements that show the parsed roomType, howManyNights, and member before applying the discount. If those values are correct and the arithmetic uses a decimal/floating type and multiplication for the percentage, the discount will apply consistently across all room types.

Recommended Answers

All 7 Replies

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 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.