What i want to do is to make it that inside a loop, the computer checks whether the number entered is firstly not a decimal, and at the same time i want to make sure that the number is within the range 1 - 100. My code now works in regards to having the first function about the number not being a decimal, so when i enter a decimal, an error message is displayed that tells the user to keep adding another number until an integer is added, and it then runs to the next part. However it doesn't seem to work when i put in a number outside of the range, the error message doesn't pop up and the conditions just don't seem to work. What i want to know is how do i get these two parts, the decimal and the range checking to work simultaneously. I'm really new to coding so could any explanations be simple so that i could understand. Thank you in advance!
string inputcost;
string inputmoney;
int validcost;
int validmoney;
int changereq;
Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: ");
inputcost = Console.ReadLine();
bool result = int.TryParse(inputcost, out validcost);
while (!int.TryParse(inputcost, out validcost))
{
if (result == true )
{
Console.Write("Valid Value");
}
if (result == false)
{
Console.Write("You Cannot Enter Decimals. Please Enter A Valid Integer Value.");
Console.WriteLine();
inputcost = Console.ReadLine();
}
if (validcost < 100 && validcost > 1)
{
Console.Write("valid value");
}
else
{
Console.Write("invalid value.please enter a number between 1 and 100 ");
Console.ReadLine();
}
}
The line
while (!int.TryParse(inputcost, out validcost))
means that you enter the while loop only when the user types something that cannot be converted to an integer. If it is a valid integer the code inside the while loop is never reached and thus, your test on the valid range is never executed
Instead put everything inside the an infinite loop and provide a way to break the program (type x to quit)
while (true)
{
Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: (type x to quit)");
inputcost = Console.ReadLine();
// Check if the user wants to stop executing the program
if(inputcost == "x")
break;
// Check if it is a valid integer
bool result = int.TryParse(inputcost, out validcost);
if (!result)
{
Console.WriteLine("You Cannot Enter Decimals (or strings). Please Enter A Valid Integer Value.");
}
else if (validcost > 100 || validcost < 1)
{
Console.WriteLine("invalid value.please enter a number between 1 and 100 ");
}
else
{
Console.WriteLine("Valid value");
// other code block that works with the input number....
}
}
Alternativly use another condition for your loop, something that first checks for an integer and afterwards if it is in range:
while (!int.TryParse(inputcost, out validcost)) || validCost < 1 || validCost > 100)
{
Console.WriteLine("Please enter an integer between 1 and 100");
inputCost = Console.ReadLine();
}
All the code that should be executed when your inout is valid should now gow beyond the loop, not inside it.
Related
I'm new to C# and i would really need your help with a project.
The idea is to ask the user for 10 numbers between 1-20. The numbers that the user enters are stored in an array. In the next phase a random number i generated and then the program compare all the numbers that the user previously entered with the random number. If one of numbers matches the program write something like "You win!".
My current solution is okay and working but I want a better exception handling than the current one. Here is my problem:
As you can see in my code below I rely on a loop and try/catch to ensure that the user enters a valid number, but after testing several times I discovered that if you enter a valid input, let say the first time but not the second, the unvalid input is still sent to the for-loop and to the next index.
I want to ensure that the user enters a valid number and if not the for-loop would temporarily "pause" until the next VALID number is entered.
bool start = true; //Create a loop.
{
while (start == true)
{
try
{
for (int x = 0; x < vektor.Length; x++) //To fill my array.
{
Console.WriteLine("Enter a number between 1 and 20:");
vektor[x] = int.Parse(Console.ReadLine());
start = false;
}
}
catch
{
Console.WriteLine("Error, you need to enter a number!");
}
}
}
Make a method that asks the user a question and doesn't give an answer until it's valid:
public int Ask(string question, int lower, int upper){
while(true)
{
Console.WriteLine(question);
string input = Console.ReadLine();
bool valid = int.TryParse(input, out int inputInt); //valid is true if it was a number
valid = valid && inputInt >= lower && inputInt <= upper; //but also test was it in range?
if(valid)
return inputInt; //if not valid, repeat the question because the loop is infinite
}
}
valid will be true if the user enters a number, but if they entered 40 for a 1 to 20 range, then the second validity assessment is:
valid = true /*it was a number*/ && true /*40 is >= 1*/ && false /*40 is not <= 20*/
So valid becomes false. We can only escape the Ask() method, returning the valid number if valid is true, otherwise the loop goes round again
--
Now you can have a list of numbers, say you want 10, we can loop and add numbers to a list until we get 10:
List<int> numbers = new List<int>();
while(numbers.Count < 10)
{
int validNumber = Ask("Enter a nubmer between 1 and 20: ", 1, 20);
numbers.Add(validNumber);
}
You can do this as an array if you like:
int[] numbers = new int[10];
for(int x = 0; x<numbers.length; x++)
{
int validNumber = Ask("Enter a nubmer between 1 and 20: ", 1, 20);
numbers[x] = validNumber;
}
Because you know that the Ask method will never return unless the input is valid, so the loop "pauses"
My Instructions:
Implement a C# console application/program that makes use of a sentinel controlled while-loop structure to continuously prompt, read and store the test results entered by the user. The user will stop his/her input sequence by entering a ‘x’ character. You also need to make use of a switch selection structure (nested inside of the while loop).
e.g code to be used:
char stop = 'x';
Console.WriteLine("Enter result (x to stop): ");
results = Convert.ToChar(Console.ReadLine());
while(results != stop)
{
Console.WriteLine("Enter result (x to stop): ");
results = Convert.ToChar(Console.ReadLine());
}
When I try to enter the 'x' as is, it does not work
you are using Console.WriteLine, but you should use Console.ReadKey and omit Convert.ToChar instead get the KeyChar property of the returned value.
so
while(results != stop)
{
Console.WriteLine("Enter result (x to stop): ");
results = Console.ReadKey().KeyChar;
}
should work, I didn't test it
You convert into wrong type: not every string can be converted into a single char (say, "xenogg").
However, every char can be represented as a string with a help of ToString():
char stop = 'x';
// Keep looping ...
while (true) {
// ...and asking user for input...
Console.WriteLine("Enter result (x to stop): ");
string results = Console.ReadLine();
// ... until (s)he puts "x"
// put StringComparison.OrdinalIgnoreCase if 'X' is exit as well as 'x'
if (string.Equals(results.Trim(), x.ToString(), StringComparison.Ordinal))
break;
// results is not "x"
//TODO: add relevant code here
}
I am writing a method to take user input for a menu with three options: 1,2,3. My do while loop runs, however my while logic is still accepting unwanted input. Why is my code not looping for inputs outside of my set range of values?
I have tried different logical operators, removing logical operators and setting while Option < Value.
private static MenuOption ReadUserOption()
{
int Option;
Console.WriteLine("Please make a selection");
Console.WriteLine("1, will run TestName, 2 will run Guess that number, 3 Wil quit the program.");
do
{
Option = Convert.ToInt32(Console.ReadLine());
return (MenuOption)(Option - 1);
} while (Option < 1 || Option > 3);
}
My goal is for any user input outside of <1 and >3 for the loop to continue until a value within that range is entered. In its current state if I enter 0 the loop will accept that value and output -1.
You just need to move the return statement outside the loop
private static MenuOption ReadUserOption()
{
int Option;
Console.WriteLine("Please make a selection");
Console.WriteLine("1, will run TestName, 2 will run Guess that number, 3 Wil quit the program.");
do
{
Option = Convert.ToInt32(Console.ReadLine());
} while (Option < 1 || Option > 3);
return (MenuOption)(Option - 1);
}
Hi my If statement wont work for some reason I want it to say invalid if user goes below 0 or over 100 but every number i put in goes directly to my else statement doesn't even notice my if statement.
What am I doing wrong?
g:
Console.WriteLine("Enter Grade: ");
int grade = Convert.ToInt32(Console.Read());
if (grade < 0 || grade > 100)
{
Console.WriteLine("Input Valid");
Console.ReadLine();
}
else
{
Console.WriteLine("Input invalid");
Console.ReadLine();
}
goto g;
if/else is not a problem here. The way you're getting input from user is.
Use Console.ReadLine() instead of Console.Read():
int grade = Convert.ToInt32(Console.ReadLine());
Console.Read returns numeric representation (ACII code) of the first character from console, not the number you've typed itself.
Some changes you might need to do:
int grade = Convert.ToInt32(Console.ReadLine());
and change your condition to &&:
if (grade >= 0 && grade <= 100)
Change your read statement as follows:
int grade = Convert.ToInt32(Console.ReadLine());
Console.Read() returns the ASCII code that you typed, while Console.ReadLine() returns a string.
There is something wrong with my code. I am teaching myself c# and one of the challenges in this chapter was to prompt the user for 10 numbers, store them in an array, than ask for 1 additional number. Then the program would say whether the additional number matched one of the numbers in the array. Now what I have below does work, but only if I enter in a comparison number that is less than 10 which is the size of the array.
I'm not sure how to fix it. I am not sure how to do the comparison. I tried a FOR loop first which kind of worked, but ran through the loop and displayed the comparison against all 10 numbers so you would get 9 lines of No! and 1 line of Yes!. I put in a break; which stopped it counting all 10 but if I entered the number 5 for comparison, then I would get 4 lines of No! and 1 of Yes!. The below has been the only way I could get it to work reliably but only as long as the number isn't out of the bounds of the array.
I can see why I get the error when the number is above 10, I just don't know what to use to compare it but still allow the user to enter in any valid integer. Any assistance would be great!
int[] myNum = new int[10];
Console.WriteLine("Starting program ...");
Console.WriteLine("Please enter 10 numbers.");
for (int i = 0; i <= 9; ++i)
{
Console.Write("Number {0}: ", i + 1);
myNum[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine("Thank you. You entered the numbers ");
foreach (int i in myNum)
{
Console.Write("{0} ", i);
}
Console.WriteLine("");
Console.Write("Please enter 1 additional number: ");
int myChoice = Int32.Parse(Console.ReadLine());
Console.WriteLine("Thank you. You entered the number {0}.", myChoice);
int compareArray = myNum[myChoice - 1];
if (compareArray == myChoice)
{
Console.WriteLine("Yes! The number {0} is equal to one of the numbers you previously entered.", myChoice);
}
else
{
Console.WriteLine("No! The number {0} is not equal to any of the entered numbers.", myChoice);
}
Console.WriteLine("End program ...");
Console.ReadLine();
You were on the right track- you want to loop through the array in myNum and compare each element to the variable myChoice. If you don't want to print whether each element of the array is a match, create a new variable and use it to keep track of whether you've found a match or not. Then after the loop you can check that variable and print your finding. You'd usually use a bool variable for that- set it false to start, then true when you find a match.
bool foundMatch = false;
for (int i = 0; i < 10; i++) {
if (myNum[i] == myChoice) {
foundMatch = true;
}
}
if (foundMatch) {
Console.WriteLine("Yes! The number {0} is equal to one of the numbers you previously entered.", myChoice);
}
If you include the System.Linq namespace (or if you change the type of myNum to be something that implements ICollection<T>, like List<T>), you can use myNum.Contains(myChoice) to see if the value myChoice matches one of the values in myNum. array.Contains returns a boolean that is true if the specified value is found in the array and false if it is not.
You can update your code to use this like so:
//int compareArray = myNum[myChoice - 1]; // This line is no longer needed
if (myNum.Contains(myChoice))
{
Console.WriteLine("Yes! The number {0} is equal to one of the numbers you previously entered.", myChoice);
}
else
{
Console.WriteLine("No! The number {0} is not equal to any of the entered numbers.", myChoice);
}
If you're looking for numbers that are definitely between 1 and 10, then before you use
int compareArray = myNum[myChoice - 1];
check if it's over the value of 10. For example:
while(myChoice > 10)
{
Console.Write("Please choose a number less than or equal to 10: ");
myChoice = Int32.Parse(Console.ReadLine());
}
The benefit of putting it inside a while loop instead of an if tag means that, when the user enters another number, the value of myChoice will be rewritten, and compared against. If they enter a number over 10, it'll keep responding Please choose a number less than or equal to 10. until the number they input is below or equal to 10:` Then, your program will continue.
However, if you want to compare it against the array, rather than put in a fixed number comparison, consider the following while loop:
while(myChoice > myNum.Length)
{
Console.Write("Please choose a number less than or equal to {0}: ", myNum.Length);
myChoice = Int32.Parse(Console.ReadLine());
}
This will work for any sized array then, without you having to change the while loops content. By using this system, you can then ensure that you won't get an IndexOutOfBounds exception, so long as you subtract 1 when using it as an index.
You are looking to compare a final, 11th value and trying to determine if its in an array of 10 previous entries?
Try:
for(int i = 0; i < array.length - 1; i++;)
{
If(array[i] == input)
return true;
}
return false;
You should be able to figure out how to implement this completely yourself, as you did want to do it as an exercise.
Edit: If someone wants to check this or complete it in correct syntax, go ahead. I posted this rough outline from a phone.