C
C#16mo ago
shamb

✅ Sanity check: output issues that I do not understand.

Hello! new uni semester new language to learn. im having an issue with my current c# solution where the final output is sometimes wrong by 1.
5 Replies
shamb
shambOP16mo ago
{
// and the code goes here
Console.WriteLine( "===========================" );
// get the input from the user
Console.WriteLine("Enter the number of chickens:");
string chicken_input = Console.ReadLine();
// convert to an integer
int chicken_count = int.Parse(chicken_input);
// create variable to hold egg count
int egg_count = 0;
int i = 0;
// create for loop to get all the eggs
while (i < chicken_count)
{
// user inputs eggs
Console.WriteLine("Eggs:");
// we then read and parse it into an integer
string egg_input = Console.ReadLine();
int egg_inputint = int.Parse(egg_input);
//add our input to our total
egg_count += egg_inputint;
// increment var
i++;

}
//now that we have collected all the eggs we need to sort them correctly
//calculate the dozens and the remainder
int egg_dozens = egg_count / 12;
int egg_remainder = egg_count % 12;
//create to output variables depending on if the final value is plural or not
string plural_output = $"You have {egg_count} eggs which equals {egg_dozens} dozen and {egg_remainder} eggs left over.";
string singular_output = $"You have {egg_count} eggs which equals {egg_dozens} dozen and {egg_remainder} egg left over.";
// and do the final string via an if statement
if (egg_remainder != 1)
{
Console.WriteLine(plural_output);
}
else
{
Console.WriteLine(singular_output);
}
Console.WriteLine( "===========================" );
}
{
// and the code goes here
Console.WriteLine( "===========================" );
// get the input from the user
Console.WriteLine("Enter the number of chickens:");
string chicken_input = Console.ReadLine();
// convert to an integer
int chicken_count = int.Parse(chicken_input);
// create variable to hold egg count
int egg_count = 0;
int i = 0;
// create for loop to get all the eggs
while (i < chicken_count)
{
// user inputs eggs
Console.WriteLine("Eggs:");
// we then read and parse it into an integer
string egg_input = Console.ReadLine();
int egg_inputint = int.Parse(egg_input);
//add our input to our total
egg_count += egg_inputint;
// increment var
i++;

}
//now that we have collected all the eggs we need to sort them correctly
//calculate the dozens and the remainder
int egg_dozens = egg_count / 12;
int egg_remainder = egg_count % 12;
//create to output variables depending on if the final value is plural or not
string plural_output = $"You have {egg_count} eggs which equals {egg_dozens} dozen and {egg_remainder} eggs left over.";
string singular_output = $"You have {egg_count} eggs which equals {egg_dozens} dozen and {egg_remainder} egg left over.";
// and do the final string via an if statement
if (egg_remainder != 1)
{
Console.WriteLine(plural_output);
}
else
{
Console.WriteLine(singular_output);
}
Console.WriteLine( "===========================" );
}
shamb
shambOP16mo ago
ive tried for, while true and now this while i < chicken_count all of which providing the same issue is it just a problem with my maths?
BlackNet
BlackNet16mo ago
@shamb and what's the issue again? when i run it it works as expected what's your input?
shamb
shambOP16mo ago
im unable to see the inputs i think it might be best if i try and find someone in my class to ask
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server