Do while problems when trying to loop if condition is not attain - c#

I'm trying to have my program work by inputting an integer between 10-50 and if they didn't input within the acceptable range the loop will go back by making them input again. But I can't seem to understand why my program doesn't work. I know the logic behind but I think the codes is the problem. Here is my code
Console.WriteLine("Enter a digit between 10 and 50 ");
xx = Console.ReadLine();
x = int.Parse(xx);
do
{
if (x > 10 && x < 50)
Console.WriteLine("Pleae input again: ");
}
while (x <= 10 && x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();

The if condition is wrong, and the while condition is wronger!
Try this instead:
Console.WriteLine("Enter a digit between 10 and 50 ");
do
{
xx = Console.ReadLine();
x = int.Parse(xx);
if (10 <= x && x <= 50)
break;
Console.WriteLine("Pleae input again: ");
}
while (true);

How about this:
string xx;
int x;
Console.WriteLine("Enter a digit between 10 and 50 ");
bool cont = true;
do
{
xx = Console.ReadLine();
if (int.TryParse(xx, out x) && 10 <= x && x <= 50)
cont = false;
else
Console.WriteLine("Pleae input again: ");
}
while (cont);
Seeing while(true) makes my skin crawl. And, you should always use int.TryParse instead of int.Parse for user input.

Check your IF and WHILE conditions.. Try this:
Console.WriteLine("Enter a digit between 10 and 50 ");
do
{
xx = Console.ReadLine();
x = int.Parse(xx);
if (x <= 10 || x >= 50)
Console.WriteLine("Pleae input again: ");
}
while (x <= 10 || x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();

You need to ask for the input each time or you wont come out of the loop.
do
{
xx = Console.ReadLine();
x = int.Parse(xx);
if (x > 10 && x < 50)
Console.WriteLine("Pleae input again: ");
}
while (x <= 10 && x >= 50);
Console.WriteLine("The number is in between!");
Console.Read();

Related

Two user inputs have to be divided by 5 so the program can finish

Text of the task: A user needs to input numbers until the number that could be divided by 5 has been entered two times. Numbers that could be divided by 3 will reset the counter.
int a;
int b;
int counter = 0;
do
{
Console.WriteLine("Enter number");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter number");
b = int.Parse(Console.ReadLine());
counter++;
// - I am not sure how to set up counter to count a and b together
// when the user enters a and b it counts as 1 not 2?
if (a % 3 == 0 || b % 3 == 0)
{
Console.WriteLine("Counter is reseting");
counter = 0;
}
} while (a % 5 != 0 && b % 5 != 0);
Console.WriteLine(counter);
It takes me out of the loop when I enter once a number divided by 5, and it doesn't want to allow me to enter it twice. How can I fix this?
If I have correctly understood, you want to continue looping unless both values are dividable by 5. So your logic in the final while needs to be:
while( A_is_not_dividable_by_5 OR B_is_not_dividable_by_5 )
or
while( a % 5 != 0 || b % 5 != 0 )
Note the or rather than the and.
Console.WriteLine("Enter number");
a = int.Parse(Console.ReadLine());
counter++;
Console.WriteLine("Enter number");
b = int.Parse(Console.ReadLine());
counter++;

How to do the loop and solve the following

I'm supposed to code a program that writes out a division just like in school.
Example:
13:3=4.333333333333
13
1
10
10
10....
So my approach was:
Solve the division then get the solution in a List.
Then question if the first number (in this case 1) is divisible by 3.
If not put it down and add the second number and so on...
I managed to do this the first time. It's sloppy but works. The problem is that it only works with numbers that when divided get to have a decimal in it.
Exapmle:
123:13
This is the first code:
do
{
for (int number = 1; number <= divNum; number++)
if (number % divisor == 0) countH++;
for (int i = 0; i < count; i++)
Console.Write(" ");
if ((c = divNum % divisor ) < divisor )
{
Console.WriteLine(" " + ((divNum- (countH * divisor ))) * 10);
}
else Console.WriteLine(" " + (divNum- (countH * divisor )));
c = divNum % divisor ;
if (c < divisor )
{
divNum = c * 10;
}
count++; countH = 0;
} while ((divNum >= divisor ) && (count < x));
Any ideas or help? Sorry if this is a bad question.
************ added
Try of a better explanation:
1 cant be divided by 13, so it goes down, we get the 2 down and try 12 divided by 13, still nothing so we get the 3 down and try 123:13, 13 goes 9 times in 123 so we have 123-9*13 = 6 the six goes down we write 9 in the result. We try 6:13 not going so we drop a 0 next to 6. Next we try 60:13, 13 goes 4 times so 60-4*13 = 8, we get the 8 down. And so on..
123:13=9.46153....
123
60
80
20
70
50
....
Something like this should work. Not the fastest solution most likely, but should do the job.
var number = 123;
var b = 12;
int quotient;
double remainder = number;
var x = 10;
do
{
quotient = (int)Math.Floor(remainder / b);
remainder = remainder - (quotient * b);
for (int i = 0; i < count; i++)
Console.Write(" ");
remainder *= 10;
Console.WriteLine(" " + remainder);
count++;
} while ((remainder > 0) && (count < x));

How can I make int command in boolean instead?

I want to make the same as I did here, to do the same operation, but instead of doing the int first_dig and second_dig I want to use it with boolean, something like: bool check = (new code here);
Console.Write("Enter a two digit number: ");
int two_dig_num = int.Parse(Console.ReadLine());
if (two_dig_num >= 10 && two_dig_num <= 99)
{
int first_dig = two_dig_num % 10;
int second_dig = two_dig_num / 10;
if (first_dig == second_dig)
Console.WriteLine("YES!");
else
Console.WriteLine("NO...");
}
else
Console.WriteLine("\nYou haven't entered a Two Digit Number,\nPlease exit the program and try again later");
return;
}
}
}
first_dig == second_dig returns you bool, so if you want to store it in some variable then just:
bool value = first_dig == second_dig;
or with less variables it can be:
bool value = two_dig_num % 10 == two_dig_num / 10;
That is it.

Console.Read() not work correctly

int y = 0;
Console.WriteLine("insert x");
int x = Console.Read();
Console.WriteLine("insert n");
int n = Console.Read();
Console.WriteLine("insert a");
int a = Console.Read();
int sum = (2 * n - 1) * a;
int sum2 = (2 * n * a);
int sum3 = (2 * n + 1) * a;
if (x <= 0) y = 0;
else if (x > sum && x <= sum2) y = a;
else if (x > sum2 && x <= sum3 || n <= 3 || n >= 1) y = 0;
Console.WriteLine("Y = " + y);
Console.ReadLine();
}
can't insert all values. after i insert x y printed and console close, what is my mistake?
Instead of Read use ReadLine. Only then you can be sure the user actually pressed ENTER and the entire line is returned - Read blocks until the user presses ENTER, but then returns the ASCII code of only one character. If you read the documentation example, this becomes clear.
In your example, if you enter "1" and press ENTER, the next calls to Read will actually return the ASCII codes for 1, \r and \n.
To be clear: Read does not return the entered number, but the ASCII code of the character you entered, so you're using it wrong - what you need to do is convert the string the user enters to a number like so:
int number = Convert.ToInt32(Console.ReadLine());
You could also check for errors easily like this:
int number;
if (!Int32.TryParse(Console.ReadLine(), out number))
{
Console.WriteLine("What you entered is not a number!");
return;
}
The Console.Read reads only the next character. This is not what you want. What happens is this:
you type 7 => you read the character (ascii code) 0x37 for x
you press ENTER => you read 0x0A (\r) for n
etc...
you want to use Console.ReadLine() which terminates when you hit ENTER and returns a string that you can parse as int:
Console.Write("Insert x: ");
string input = Console.ReadLine();
int x = int.Parse(input);
You may want to add error handling if the user types "abc" instead of an int or use
int x;
if (!int.TryParse(input, out x))
Console.WriteLine("This was no number!");
You should use ReadLine and convert to int 32
Thes is the right code:
int y = 0;
Console.WriteLine("insert x");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("insert n");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("insert a");
int a = Convert.ToInt32(Console.ReadLine());
int sum = (2 * n - 1) * a;
int sum2 = (2 * n * a);
int sum3 = (2 * n + 1) * a;
if (x <= 0) y = 0;
else if (x > sum && x <= sum2) y = a;
else if (x > sum2 && x <= sum3 || n <= 3 || n >= 1) y = 0;
Console.WriteLine("Y = " + y);
Console.ReadLine();
Everyone has given a solution but the reason why
Your code doesn't work is this.
The
Console.Read
Returns the ASCII value of the keypressed.
It means saying something like
int i = Console.Read();
And hitting the 4key on your keyboard will store the value 53, which is the ASCII value of the 4key in variable i instead of ur intended integer "4".
To fully understand this check variable values by using breakpoints after Console.Read to see what is really stored in variable a, n and y.

C#, integer entered not between two numbers [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 6 years ago.
Improve this question
I have to make it to when an integer entered that is not between 0 and 10 then it will display a message, whatever the number entered is not between 0 and 10. Also when -99 is entered it will exit the program. I have tried the while statement and nothing seems to be working.
int total = 0;
double avg;
string inValue;
int[] score = new int[8];
// prompt user for initial values
for (int i = 0; i < score.Length; i++)
{
Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
inValue = ReadLine();
if (int.TryParse(inValue, < 1 && > 10) == false)
WriteLine("Integer entered, {0}, is not between 0 and 10.");
if (int.TryParse(inValue, out score[i])
== false)
WriteLine("\n\tInvalid data - re-enter homework score: ");
}
You can't put the "greater than 10, less than 1" condition inside the TryParse() method, it does not support that. So check the condition separately. Also no need to check if (something == false) because that's identical to if (!something). I changed your ReadLine/Write/WriteLine's to have Console. prepended so it works on my system. You will need a while loop for the "please re-enter homework score" to work as you intend, but the code here does fix your original problem..
int total = 0;
double avg;
string inValue;
int[] score = new int[8];
// prompt user for initial values
for (int i = 0; i < score.Length; i++)
{
Console.Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
inValue = Console.ReadLine();
if (int.TryParse(inValue, out score[i]))
{
if (score[i] == 99)
{ Environment.Exit(0); }
bool between0and10 = score[i] <= 10 && score[i] >= 0;
if (!between0and10)
{ Console.WriteLine("Integer entered, {0}, is not between 0 and 10."); }
}
else
{ Console.WriteLine("\n\tInvalid data - re - enter homework score: "); }
}
Try this:
int total = 0;
double avg;
string inValue;
int[] score = new int[8];
// prompt user for initial values
for (int i = 0; i < score.Length; i++)
{
Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
inValue = ReadLine();
if (!int.TryParse(inValue, out score[i]))
{
WriteLine("\n\tInvalid data, StackOverflow did your homework! - re-enter homework score: ");
}
else if (score[i] < 1 || score[i] > 10)
{
WriteLine("Integer entered, {0}, is not between 0 and 10.");
}
}
int[] score = new int[8];
string sVal;
int val;
int i = 0;
while (i < score.Length)
{
Console.WriteLine("Please enter homework score [0 to 10] (-99 to exit):");
sVal = Console.ReadLine();
if (int.TryParse(sVal, out val))
{
//if quit
if (val == -99)
break;
//if valid range
if (val >= 0 && val <= 10)
{
score[i] = val;
i++;
}
else //invalid input range
Console.WriteLine("Invalid data - re-enter homework score:");
}
else //not a number
Console.WriteLine("Invalid data - re-enter homework score:");
}
int total = 0;
double avg;
int parsedScore;
//this bool will tell us if the data entered is valid
bool isValid;
int[] score = new int[8];
string inValue;
// prompt user for initial values
for (int i = 0; i < score.Length; i++)
{
Console.Write("Please enter homework score [0 to 10] (-99 to exit): \n", i + 0);
inValue = Console.ReadLine();
//here we check that the data entered is valid and set our bool to the result
isValid = int.TryParse(inValue, out parsedScore);
if (isValid && parsedScore == -99) //check first if it is -99 and then exit if need be.
{
System.Environment.Exit(0);
}
//if it is not valid we are going to prompt them to re-enter their number
if(!isValid || (parsedScore < 0 && parsedScore > 10))
{
Console.WriteLine("Integer not entered or {0}, is not between 0 and 10.", inValue);
i--; //we decrement i in order to let them re-enter at this index
}
else
{
//valid number, do logic here.
}

Categories

Resources