How do I accept blanks in C#? - c#

I am trying to create a program which calculates the result of exams by taking in the number of marks obtained in each subject. It is almost done but I'm encountering an error that is, if the user just presses enter instead of entering a value, the application wont proceed. Also is there any way to shortening this code?
Console.WriteLine("Danyal's result calculator for students of class IX. Enter the marks requested, if not applicable leave blank and press enter :)");
Console.Write("Enter your Urdu marks: ");
int urdu = int.Parse(Console.ReadLine());
Console.Write("Enter your Maths marks: ");
int maths = int.Parse(Console.ReadLine());
Console.Write("Enter your English Literature marks: ");
int lit = int.Parse(Console.ReadLine());
Console.Write("Enter your Biology marks: ");
int bio = int.Parse(Console.ReadLine());
Console.Write("Enter your Chemistry marks: ");
int chem = int.Parse(Console.ReadLine());
Console.Write("Enter your Islamiat marks: ");
int isl = int.Parse(Console.ReadLine());
Console.Write("Enter your Physics marks: ");
int physics = int.Parse(Console.ReadLine());
Console.Write("Enter your Computer marks: ");
int comp = int.Parse(Console.ReadLine());
Console.Write("Enter your English Language marks: ");
int lang = int.Parse(Console.ReadLine());
Console.Write("Enter your Pakistan Studies marks: ");
int pst = int.Parse(Console.ReadLine());
int total = urdu + maths + lit + lang + bio + chem + physics + isl + comp + pst;
Console.WriteLine("Your total marks are {0} out of 1000", total);
float percentage = total * 100 / 1000;
Console.WriteLine("Your percentage is: {0}%",percentage);
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
Console.ReadLine();

I'm not sure what you would want to do in case the user leaves it blank, or enters an invalid value, but you could do something like this.
Dictionary<string,int> grades = new Dictionary<string, int>
{
{ "Urdu", 0 },
{ "Maths", 0 },
{ "English", 0 },
{ "Biology", 0 },
{ "Chemistry", 0 },
{ "Islamiat", 0 },
{ "Physics", 0 },
{ "Computer", 0 },
{ "English Language", 0 },
{ "Pakistan Studies", 0 },
};
foreach (string grade in grades.Keys.ToArray())
{
Console.WriteLine(string.Format("Enter your {0} marks: ", grade));
int mark;
if (int.TryParse(Console.ReadLine(), out mark))
grades[grade] = mark;
}
int total = grades.Sum((g) => g.Value);
In this example, if bad input is used the grade will default to 0. If you wanted to you could change the if try parse to a loop and request a good value until one is entered as well.

This is a clear case of not adhering to the DRY principle. You are repeating operations that are essentially the same, don't do that. Refactor the code so that common patterns or behaviors are solved in one single place.
How would you do that here?
Create a method that prompts the user to enter certain information. What does the user need? A descriptive message of what he has to do. What does the user need to do? Enter a valid input. Ok, with that in mind, the following prototype of a method seems like a good starting point:
private static int GetUserInput(string message) { ... }
Hmmm...enter a valid input. This means we need some sort of validation, so again let's think of how we can solve this:
private static int ValidateUserInput(string input) { ... }
Is this good enough? Well...no quite. What happens if the user enters an incorrect number? There is no convenient way of conveying that the input is not valid to the caller. We could return -1, or we could throw an exception, but both seem offish.
The best solution would be to return two values; one telling us if the input is valid and another telling us what the input is. In c# this isn't very striaghtforward (at least until c# 7 comes along). The way to do it is using out arguments:
private static bool ValidateUserInput(string message, out int input) { ... }
Now this method serves our purpose perfectly. The return value tells us if the input is valid and the out argument input gives us the validated value (or we simply ignore it if the validation failed).
Why are you creating an int variable for each mark if you essentially only want sums and averages? Lets create a List<int> where we store all the marks.
Another option, if you want to keep track of what mark corresponds to what subject, would be to use a Dictionary<string, key> where the key would be the subject name and the value its corresponding mark. But lets use List<int> for now.
With all that in mind we can build up our solution:
public static void ComputeMarksSummary()
{
var marks = new List<int>();
marks.Add(GetUserInput("Enter your Urdu marks: "));
marks.Add(GetUserInput("Enter your Maths marks: : "));
marks.Add(GetUserInput("Enter your English Literature marks: "));
marks.Add(GetUserInput("Enter your Biology marks: "));
marks.Add(GetUserInput("Enter your Chemistry marks: "));
marks.Add(GetUserInput("Enter your Islamiat marks: "));
marks.Add(GetUserInput("Enter your Computer marks: "));
marks.Add(GetUserInput("Enter your English language marks: "));
marks.Add(GetUserInput("Enter your Pakistan studies marks: "));
var total = marks.Sum();
Console.WriteLine("Your total marks are {0} out of 1000", total);
Console.WriteLine("Your percentage is: {0}%", total/10.0); //note the 10.0 to force double division, not integer division where 44/10 would be 4 not 4.4
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
//WRONG! Percentage is not rounded off, its truncated: 9/10 is 0 in integer division.
Console.ReadLine();
}
private static int GetUserInput(string message)
{
int mark;
while (true)
{
Console.WriteLine(message);
var input = Console.ReadLine();
if (!ValidateUserInput(input, out mark))
{
Console.WriteLine("Invalid input, please try again.");
}
else
{
return mark;
}
}
}
private static bool ValidateUserInput(string message, out int input)
{
//left as an excerice. Hint: look into int.TryParse(...);
//here you could decide if a blank input should be valid and parsed as zero.
}
Wow, now that seems a lot cleaner....but hey, we can still do a little bit better. Whats up with all those marks.Add(....)? Can't we refactor the code some more? Well yes, we are essentially asking always the same thing, only the subject name changes. How about we do something like this:
public static void ComputeMarksSummary(IEnumerable<string> subjectNames)
{
var marks = new List<int>();
foreach (var subject in sujectNames)
{
marks.Add(GetUserInput(string.Format("Enter your {0} marks: ", subject)));
}
var total = marks.Sum();
Console.WriteLine("Your total marks are {0} out of 1000", total);
Console.WriteLine("Your percentage is: {0}%", total/10.0); //note the 10.0 to force double division, not integer division where 44/10 would be 4 not 4.4
Console.WriteLine("Note: the percentage has been rounded off. Please share this program with other classmates, also I am open to suggestions for creating more helpful programs.");
Console.ReadLine();
}
And you could call it like this:
ComputeMarksSummary(new string[] { "Urdu", "Maths", ... );
Doesn't that seem so much cleaner?

Related

How do I correctly convert strings to ints and input them into a list?

I'm making a program that asks the user for numbers for as long as he inputs positive values.
Here's the code that I currently have written, but I've not come to complete understanding with the function of and .Parse methods.
using System;
using System.Collections.Generic;
namespace Chapter
{
class Program
{
static void Main()
{
int input;
List<int> numbers = new List<int>();
Console.WriteLine("Input number: ");
input = Convert.ToInt32(Console.ReadLine());
numbers.Add(input);
while (input >= 0)
{
Console.WriteLine("Input number: ");
input = Convert.ToInt32(Console.ReadLine());
int val = Int32.Parse(input);
numbers.Add(val);
}
Console.WriteLine("the numbers you have input are");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
}
}
}
The best way to achieve this is to use the Int32.TryParse method. This allows you to validate the input and handle it accordingly as well as converting to an int.
See the below example:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int input;
List<int> numbers = new List<int>();
do
{
Console.WriteLine("Input number: ");
while (!Int32.TryParse(Console.ReadLine(), out input))
{
Console.WriteLine("The value entered was not a number, try again.");
Console.WriteLine("Input number: ");
}
if (input >= 0)
numbers.Add(input);
}
while (input >= 0);
Console.WriteLine("the numbers you have input are");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
}
}
Calling Convert.ToInt32(string) or Int32.Parse(string) do nearly the same thing. The difference is that Convert.ToInt32 will return 0 if the input is null and Int32.Parse will throw an exception.
If you were to look at the implementation you will find that Convert.ToInt32 calls Int32.Parse.
Anothing thing to note is that the Convert class allows you to convert a much wider range of types to intergers. The Int32.Parse method only accepts strings.
The issue with your code is in this block:
input = Convert.ToInt32(Console.ReadLine());
int val = Int32.Parse(input);
The input value has already been converted and is now an integer. This cannot be parsed (and doesn't have to be).
When working with user input, I'd rather use int.TryParse (what if user provides "bla-bla-bla"?).
I aslo suggest using infinite loop: keep asking user until (s)he puts negative value (in which case
we break the loop):
List<int> numbers = new List<int>();
// keep reading user input
while (true) {
Console.WriteLine("Input number: ");
// We try parsing (i.e. converting) user input - Console.ReadLine()
// into integer value - number
if (!int.TryParse(Console.ReadLine(), out int number))
// if parsing fails, say, user put "aha!", we let user know it
Console.WriteLine("Not a valid integer number, please, try again");
else if (number < 0)
// if number is negative stop asking user
break;
else
// non-negative numbers are added into the list
numbers.Add(number);
}
Console.WriteLine("the numbers you have input are");
// You can print the list in one go:
Console.Write(string.Join(" ", numbers));
Parseing detail:
int.TryParse(Console.ReadLine(), out int number)
int.TryParse takes user input Console.ReadLine() as an argument and returns true or false whether parsing succeeds or not. In case parsing succeeds, number contains parsed value.

C# code executes even when variable not assigned

As you can probably tell from my question, I am very new to coding. I am trying to make a calculator that computes some formulas that are used in physics. However, the code runs the formula before the user has time to enter a value for A, in this example at least. Here is the example:
case "f = ma":
Console.WriteLine("Type the value for M in KG:");
var FM = Console.Read();
Console.WriteLine("Type the value for A in M/S:");
var FA = Console.Read();
var FMARes = FM * FA;
Console.WriteLine("Your answer (in Newtowns) is " + FMARes);
break;
How am I able to check whether a value has been assigned to the variable A, and only run the formula after the variable has an assigned value? Thanks.
You need to use ReadLine instead of Read. You also need to do another ReadLine at the bottom so the user can see the result. And...you should validate that the user entered a valid number. This could be refactored a bit to avoid duplicate code - etc. - but see if this works for you! Good luck!!
static void Main(string[] args)
{
double fm;
double fa;
// Use ReadLine instead of Read
Console.WriteLine("Type the value for M in KG:");
var input = Console.ReadLine();
// Now you need to cast it to a double -
// -- but only if the user entered a valid number
if (!double.TryParse(input, out fm))
{
Console.WriteLine("Please enter a valid number for M");
Console.ReadLine();
return;
}
Console.WriteLine("Type the value for A in M/S:");
input = Console.ReadLine();
if (!double.TryParse(input, out fa))
{
Console.WriteLine("Please enter a valid number for A");
Console.ReadLine();
return;
}
// Now we have valid values for fa and fm
// It's a better programming practice to use the string format
// intead of + here...
Console.WriteLine($"Your answer (in Newtowns) is {fm * fa}");
// You need another read here or the program will just exit
Console.WriteLine("Press Enter to end the program");
Console.ReadLine();
}

C# Write a program that computes the average of five exam scores [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am doing my homework. English is not my first language, so I am confused.
This is the question: Write a program that computes the average of five exam scores. Declare and perform a compile-time initialization with the five exam values. Declare integer memory locations for the exam values. Use an integer constant to define the number of scores. Print all scores. The average value should be formatted with two digits to the right of the decimal. Rerun the application with different values. Be sure to desk check your results.
I am not sure what is "a compile-time initialization" mean? What is "Declare integer memory locations for the exam values." want me to do? What is "desk check" mean?
Here is my c# code:
using System;
using static System.Console;
namespace Chap2_1
{
class Chap2_1
{
static void Main()
{
int score1;
int score2;
int score3;
int score4;
int score5;
double average;
Console.Write("Please enter the 1st score: ");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 2nd score: ");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 3rd score: ");
score3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 4th score: ");
score4 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 5th score: ");
score5 = Convert.ToInt32(Console.ReadLine());
average = (score1+score2+score3+score4+score5) /5;
Console.Write("Average score is " + "{0:N2}", average);
Console.ReadKey();
}
}
}
I am not sure what is "a compile-time initialization" mean?
It means that your scores should have a value set in code from the start (hard coded), rather than values set by user input or "figured out" by the program after it's already running.
In other words, replace this:
Console.Write("Please enter the 1st score: ");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 2nd score: ");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 3rd score: ");
score3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 4th score: ");
score4 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 5th score: ");
score5 = Convert.ToInt32(Console.ReadLine());
With something like this:
//Replace the pointed numbers with whatever the scores should be.
// ||
// vv
score1 = 11;
score2 = 22;
score3 = 33;
score4 = 44;
score5 = 55;
What is "Declare integer memory locations for the exam values."
It means declaring the variables responsible for holding the scores so that you can average them. In other words, this part:
int score1;
int score2;
int score3;
int score4;
int score5;
What is "desk check" mean?
It means that you should average the scores with pen & paper, and make sure the result your program outputs is correct.
PS: I don't want to be rude, but this community is made for questions about code. If you don't understand the question, or english in general, you should ask your teacher.
We are here to help you with programming... not with translation or interpretation.
Shouldn't these be questions for your teacher? Your teacher will know your struggles and be able to help you far better than any of us simply because of his/her role in your studies.
That said, a compile-time initialization is something like:
int[] scores = new int[] { 100,80,90,64,72 };
or:
int score1 = 100;
int score2 = 80;
int score3 = 90;
int score4 = 64;
int score5 = 72;
As far as the memory locations go, I'd recommend reading Microsoft's C# programming guide here about it:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/how-to-obtain-the-address-of-a-variable
Oh, and "desk check" means to do the same calculations manually with pen and paper to validate that you get the same results as your code.

How to check for a double type response in an if statement

I'm trying to check if the user's response is a double or an int, but the int is specific, whereas the double is not, as I probably made a right mess of explaining it, here's the code:
Console.WriteLine("\n 2) Q: How old is Sally? \n");
int nSallyAge = Convert.ToInt32(Console.ReadLine());
double dSallyAge = Convert.ToDouble((nSallyAge));
if (nSallyAge == 62 || dSallyAge == 62.0)
{
// Increase Score
sUser1Score++;
Console.WriteLine("\n A: Correct, Sally's age is 62, you have been awarded 1 point. \n");
Console.ReadLine();
}
What I'm trying to do, is instead of dSallyAge HAS to equal 62.0, it just has to equal any double figure.
I would approach this problem by first creating a method that gets a double from the user (that will, of course, also accept an int). This removes error handling from your main code.
NOTE in the code below, Math.Truncate can be replaced by Math.Floor, with the same result:
private static double GetDoubleFromUser(string prompt)
{
double input;
while (true)
{
if (prompt != null) Console.Write(prompt);
if (double.TryParse(Console.ReadLine(), out input)) break;
Console.WriteLine("Sorry, that is not a valid number. Please try again.");
}
return input;
}
Then, in my main code, I would get the number from the user, and use the Math.Truncate method to just read the first part of the double passed in by the user (this is what it sounds like you want to do). This means that if the user enters anything from 62 to 62.0 to 62.999, it will truncate the result to '62':
double nSallyAge = GetDoubleFromUser("2) Q: How old is Sally? ");
if (Math.Truncate(nSallyAge) == 62)
{
// Increase Score
sUser1Score++;
Console.WriteLine("A: Correct, Sally's age is 62, you have been awarded 1 point.");
Console.ReadLine();
}
Other alternative ways to use this are:
int sallyAge = Math.Truncate(GetDoubleFromUser("2) Q: How old is Sally? "));
if (sallyAge == 62)
{
// Increase Score
sUser1Score++;
Console.WriteLine("A: Correct, Sally's age is 62, you have been awarded 1 point.");
Console.ReadLine();
}
Or, you could use an input function that returns an int in the first place:
private static int GetIntFromUser(string prompt)
{
return Math.Truncate(GetDoubleFromUser(prompt));
}
In your code above, you are converting the input to an integer and then converting the int result to a double.
Assuming that you are only allowing numerical values to be entered, why not try something like this. This will identify if the input contained decimals or not:
string input = Console.ReadLine();
int iSallyAge;
double dSallyAge;
if (!Int32.TryParse(input, iSallyAge))
{
dSallyAge = Double.Parse(input);
}
You should be using Double.TryParse to parse the user input to a double and then test that value to see whether it's equal to 62.0.
double age;
if (double.TryParse(Console.ReadLine(), out age) && age == 62.0)
{
// age is 62.
// ...
}

Use of try - catch for taking care of letters

I have written a program that will emulate how a cash register works.
I would need some help with how to make the program take care of, if for example the user enters letters instead of numbers.
Then i would like the letters that the user entered were lost and the user receives a new opportunity to start from scratch.
I have written some code for it using try and catch, but not sure how it should be written.
class Program
{
static void Main(string[] args)
{
int cash = 0;
double totalAmount = 0;
uint subTotal;
int exchange;
double roundingOffAmount;
Console.Write("Please enter a total amount for the cash register : ");
totalAmount = double.Parse(Console.ReadLine());
if (totalAmount < 1)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("\nTotalamount needs to be more\n");
Console.ResetColor();
Environment.Exit(0);
}
try
{
Console.Write("Please enter cash for the cash register: ");
cash = int.Parse(Console.ReadLine());
if (cash < totalAmount)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("\nCash needs to be more than totalAmount\n");
Console.ResetColor();
Environment.Exit(0);
Console.WriteLine();
}
else
{
// Do nothing
}
}
catch (FormatException)
{
Console.Write("\nSorry you typed in a letter you need to type in a number");
Console.WriteLine();
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("\nSomething went wrong, please try again");
Console.ResetColor();
Console.WriteLine();
Main(args);
}
subTotal = (uint)Math.Round(totalAmount);
roundingOffAmount = subTotal - totalAmount;
exchange = cash - (int)totalAmount;
Console.WriteLine("\n Receipt"
+ "\n ------------------------------------"
+ "\n Totalt \t: \t {0:c}", totalAmount);
Console.WriteLine(" RoundingOffAmount\t: \t {0:f2}", roundingOffAmount);
Console.WriteLine(" To pay \t: \t {0:c}", subTotal);
Console.WriteLine(" Cash \t: \t {0:c}", cash);
Console.WriteLine(" Exchange \t:\t {0:c}", exchange
+ "\n ------------------------------------");
Console.WriteLine();
}
}
Any help is warmly received.
Firstly - and more importantly - for currency values, you should be using decimal rather than double. Decimal floating point numbers are more appropriate for monetary values, which are exactly represented in decimal - whereas binary floating point types (double and float) are more appropriate for "natural" values such as height and weight, which will never have an absolutely precise measured value anyway. See my articles on binary floating point and decimal floating point for more details.
Next, rather than using exception handling for this validation, I suggest you use decimal.TryParse - which returns whether or not it was successful. That way you don't have to use try/catch just to catch a pretty predictable exception which can easily be avoided. For example:
decimal value;
while (!decimal.TryParse(Console.ReadLine(), out value))
{
Console.WriteLine("Sorry, that wasn't a valid number");
}
You should use int.TryParse. If the input is not a valid integer, this will return false.
If it returns false, you can use this as a way of prompting the user to enter another value in.
EDIT
As Jon Skeet pointed out, you really should use decimal type when dealing with currency.
Use try parse:
decimal totalAmount;
bool ok = decimal.TryParse(outConsole.ReadLine(), out totalAmount);
if(!ok){
//Bad input. Do something
}else{
//input ok, continue
}
Use the same approach to parse the integer.
instead of
totalAmount = double.Parse(Console.ReadLine());
do
boolean isDouble = double.TryParse(Console.ReadLine(), out totalAmount);
then check
if(isDouble)...

Categories