0

I am new to programming and would be very grateful for the help. The main problem is that do not understand how to get and operate the user's input on two arrays.

Current output

my output

Desired output

How the output should look

Current solution

static void Main(string[] args)
{
    string[] name = { "Single", "Double", "Luxury", "Penthouse" };
    double[] price = { 45.50, 99.99, 165.25, 1095.50 };
    Console.WriteLine("Room Types\n");

    for (int i = 0; i < name.Length;  i++)
    {
        Console.WriteLine("1. " + name[i] + " " + "$" + price[i] + " per night");
    }
    Console.Write("\nPlease select a room type 1, 2, 3 or 4: ");
    int room = Int32.Parse(Console.ReadLine());
    Console.Write("Please enter the number of nights: ");
    int nights = Int32.Parse(Console.ReadLine());
    Console.WriteLine("\nThank you, the total cost for staying in the ? room for 5 nights is " + nights * room);
    
    Console.ReadLine();
}
3
  • 1
    price[room] * nights ? or price[room-1] * nights depending Commented Sep 24, 2021 at 5:08
  • 1
    Do you have to code it like this (assignment restriction)? I ask because normally in OO when we want to keep data together like a rooms name and it's price we would make a Room class with a name and a price properties and then have an array of Room.. Commented Sep 24, 2021 at 5:16
  • Yes, the task doesn't require OO) Commented Sep 25, 2021 at 10:11

2 Answers 2

2

You were nearly there, I have made your loop use the index E.g {i+1}

Console.WriteLine($"{i+1}. {name[i]} ${price[i]} per night")

The result to use the token {nights} and the calculation to be {price[room-1] * nights}

Example

string[] name = { "Single", "Double", "Luxury", "Penthouse" };
double[] price = { 45.50, 99.99, 165.25, 1095.50 };

Console.WriteLine("Room Types");

for (int i = 0; i < name.Length;  i++)
   Console.WriteLine($"{i+1}. {name[i]} ${price[i]} per night");

Console.Write("\nPlease select a room type 1, 2, 3 or 4: ");

var room = int.Parse(Console.ReadLine());
Console.Write("Please enter the number of nights: ");
var nights = int.Parse(Console.ReadLine());
Console.WriteLine($"\nThank you, the total cost for staying in the ? room for 5 nights is {price[room-1] * nights}");

Console.ReadLine();

Output

Room Types
1. Single $45.5 per night
2. Double $99.99 per night
3. Luxury $165.25 per night
4. Penthouse $1095.5 per night

Please select a room type 1, 2, 3 or 4: 2
Please enter the number of nights: 2

Thank you, the total cost for staying in the ? room for {nights} nights is 199.98

Also note, never use parse type methods for user input, use TryParse type methods to protect against pesky users who can't read instructions.

A more modern approach

internal class Program
{
   public record Room(string Name, double Price);

   public static Room[] _rooms =
   {
      new("Single", 45.50),
      new("Double", 99.99),
      new("Luxury", 165.25),
      new("Penthouse", 1095.50 ),
   };

   private static void Main(string[] args)
   {
      do
      {
         Console.WriteLine("Room Types");

         for (int i = 0; i < _rooms.Length; i++)
            Console.WriteLine($"{i + 1}. {_rooms[i].Name} ${_rooms[i].Price} per night");

         Console.Write($"\nPlease select a room type between 1 and {_rooms.Length} : ");

         int room;
         while (!int.TryParse(Console.ReadLine(), out room) || room <= 0 || room > _rooms.Length)
            Console.Write("Omg, you had one job, follow the instructions : ");

         Console.Write("Please enter the number of nights : ");

         int nights;
         while (!int.TryParse(Console.ReadLine(), out nights))
            Console.Write("Omg, you had one job, follow the instructions : ");

         Console.WriteLine($"\nThank you, the total cost for staying in the ? room for {nights} nights is {_rooms[room - 1].Price * nights}");
         Console.WriteLine("Game over... press any key to play again, or enter to exit");


      } while (Console.ReadLine() == null);

      Console.WriteLine("be gone...");
   }
}

Output

Room Types
1. Single $45.5 per night
2. Double $99.99 per night
3. Luxury $165.25 per night
4. Penthouse $1095.5 per night

Please select a room type between 1 and 4 : fgdsfgdsfg
Omg, you had one job, follow the instructions : 2
Please enter the number of nights : 2

Thank you, the total cost for staying in the ? room for 2 nights is 199.98
Game over... press any key to play again, or enter to exit

be gone...
Sign up to request clarification or add additional context in comments.

1 Comment

Good use of record but still didn't fix the in the ? room bug.
1

The main problem here is the console output and the composition of strings. It is better to use string interpolation to compose strings with characters and values.

For example Console.WriteLine( $"The value is {x}" ); where the program will insert the value of x in the string when it is displayed.

Specifically for your code, note that arrays are 0-indexed which means the price for room type #1 is stored in price[0], and for room type #2 in price[1]. It is a bit confusing as the index represents an offset in memory from the first value (or it used to in C days, and the convention is retained).

As you can see in the code below the cost for the rooms is evaluated with

decimal cost = nights * price[room-1]; 

which uses the array index room-1 for that reason. Also, I switched to decimal types which are better for currency and display with the currency symbol $ when rendered using the code c. For example the line

Console.WriteLine($"{i + 1}. {name[i]} {price[i]:c2} per night");

displays the price[i] value using currency c with 2 decimal places.

static class Program
{
    static void Main(string[] args)
    {
        string[] name = { "Single", "Double", "Luxury", "Penthouse" };
        decimal[] price = { 45.50m, 99.99m, 165.25m, 1095.50m };
        Console.WriteLine("Room Types");
        Console.WriteLine();
        for (int i = 0; i < name.Length; i++)
        {
            Console.WriteLine($"{i + 1}. {name[i]} {price[i]:c2} per night");
        }
        Console.WriteLine();
        Console.Write("Please select a room type 1, 2, 3 or 4: ");
        int room = Int32.Parse(Console.ReadLine());
        Console.Write("Please enter the number of nights: ");
        int nights = Int32.Parse(Console.ReadLine());
        decimal cost = nights * price[room-1];
        Console.WriteLine($"Thank you, the total cost for staying in the {name[room-1]} room for {nights} nights is {cost:c2}");

        Console.ReadLine();
    }
}

PS. Avoid printing \n for new line, as the correct way, is to either use WriteLine() or to use the string stored in Environment.NewLine.

3 Comments

Thanks for the down vote, you got me back to multiples of 5.. I have major OCD!
@TheGeneral - I did not downvote.
All good either way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.