Reputation: 86
My program is supposed to tally up bottles from 4 rooms. When the user types in quit, the program spits out how many bottles each room has collected.
I have a problem when the program prompts the user, it prompts twice but takes the last entered number as the selected roomNumber
. I also do not know how to set up my if statements so I can continuously add up the bottles entered by the user to each specific room.
Can I use a temporary variable to store the entered bottle count then add it to the room1
, room2
which holds the current bottle count number?
namespace BottleDrive1 {
class Program {
static void Main(string[] args)
{
//Initialize 4 rooms.
int room1 = 0;
int room2 = 0;
int room3 = 0;
int room4 = 0;
int roomNumber;
while (true)
{
Console.WriteLine("Enter the the room number you are in.");
string quit = Console.ReadLine();
if (quit == "quit")
{
//Break statement allows quit to jump out of loop
break;
}
//int roomT = int.Parse(quit);//a temp variable I want to use to add bottles to the count.
roomNumber = int.Parse(Console.ReadLine());
if (roomNumber == 1)
{
Console.WriteLine("How many bottles did room 1 collect?");
room1 = int.Parse(Console.ReadLine());
}
if (roomNumber == 2)
{
Console.WriteLine("How many bottles did room 2 collect?");
room2 = int.Parse(Console.ReadLine());
}
if (roomNumber == 3)
{
Console.WriteLine("How many bottles did room 3 collect?");
room3 = int.Parse(Console.ReadLine());
}
if (roomNumber == 4)
{
Console.WriteLine("How many bottles did room 4 collect?");
room4 = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Bottles each room has collected:");
Console.WriteLine("Room one:" + room1);
Console.WriteLine("Room two:" + room2);
Console.WriteLine("Room three:" + room3);
Console.WriteLine("Room four:" + room4);
int maxRoom = room1;
if (room2 > maxRoom)
{
maxRoom = 2;
}
if (room3 > maxRoom)
{
maxRoom = 3;
}
if (room4 > maxRoom)
{
maxRoom = 4;
}
else
{
maxRoom = 1;
}
Console.WriteLine("The winner is room " + maxRoom + "!");
}
}
}
Upvotes: 1
Views: 464
Reputation: 40345
You were very close! In your while loop, you read the value for quit from console and you "prompt" the user to enter the room number again by reading from console one more time. You can avoid the second "prompt" by simply taking the value of quit and parsing the room number. Note that everything will work fine, because if the user enters quit, then you will exit the loop anyway:
while (true)
{
Console.WriteLine("Enter the the room number you are in.");
string quit = Console.ReadLine();
// another quick thing to fix is to ignore the case (don't trust the user)
if(quit .Equals("quit", StringComparison.InvariantCultureIgnoreCase))
{
//Break statement allows quit to jump out of loop
break;
}
roomNumber = int.Parse(quit); // you've already asked the user for input, so just reuse the variable holding the input
// TODO: consider what happens if you can't parse an integer, i.e. use TryParse etc
// do whatever you need to do after that
}
After you get the room number, then don't forget to add the number of bottles with the '+= operator', e.g.:
room4 += int.Parse(Console.ReadLine());
Upvotes: 4
Reputation: 5132
You need to add the value received from Console.ReadLine() to the existing number of bottles for a room like so:
room1 = room1 + int.Parse(Console.ReadLine());
alternatively you can use the shorthand version:
room1 += int.Parse(Console.ReadLine());
The problem you currently have is that when you enter 5 bottles for room one room1
gets 5 saved in the variable. The next time you enter 10 bottles for room one room1
gets 10 saved in the variable. You need to add those two numbers together in order to keep the total number of bottles for a room.
Upvotes: 0