Can't update variable in a do while loop [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Console.WriteLine("Would you like to go first or second?");
string firstSecond = Console.ReadLine().ToUpper();
if (firstSecond == "FIRST")
{
Console.WriteLine("1");
}
else if (firstSecond == "SECOND")
{
Console.WriteLine("2");
}
else
{
string input = "x";
bool first = input == "FIRST";
bool second = input == "SECOND";
do
{
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
}
while (!(first | second));
}
The do while loop keeps on going even if I type first or second, I tried changing how I state the boolean but nothing seems to work. I am new so I am might some silly mistakes. How do I fix this?

You must put first and second condition into do while loop.
string input;
bool first;
bool second;
do {
Console.WriteLine("Please say first or second:");
input = Console.ReadLine().ToUpper();
first = input == "FIRST";
second = input == "SECOND";
}
while (!(first || second));

Related

While loop does not get executed when method is called [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
The while loop
public static bool Authentication()
{
bool isAuthenticated = false;
int count = 4;
while (isAuthenticated = false && count > 0)
{
Console.WriteLine("Please insert your 4 digit PIN");
string pin = Console.ReadLine();
if (pin == "1704")
{
isAuthenticated = true;
return isAuthenticated;
}
else
{
count--;
Console.WriteLine($"Wrong PIN: {count} Input remained ");
}
}
return isAuthenticated;
}
I haven't been able to understand, when the debugger goes to the while loop, it does not execute it. It may be the condition of the while that does not fit the logic.
I believe you are using the = as an assigning operator in the condition of the loop. You could try !isAuthenticated instead of isAuthenticated == false and see if that works
while (!isAuthenticate && count > 0)
I believe you were assigning false to isAuthenticate, so the condition could never be true.

cs0103: The name 'input' does not exist in the current context [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was doing the project for methods on Codecademy and was having trouble saving a ReadLine input into a variable and use it in a switch case statement. I tried my best to look up the issue but couldn't figure it out myself. I'm an absolute beginner, so please forgive me if this is trivial or has been answered before.
The relevant section of my code looks as follows:
public static void CalculateTotalCost()
{
retry:
Console.Write("What monument would you like to have calculated? (Teotihuacan, Taj Mahal or Great Mosque): ");
input = Console.ReadLine();
switch(input)
{
case "Teotihuacan":
break;
case "Taj Mahal":
break;
case "Great Mosque":
break;
default:
Console.WriteLine("Wrong entry, try again.");
goto retry;
}
}
My best guess was to declare my variable 'input' as a string but weirdly enough codecademy would crash every time i tried saving my code that way.
Try changing the beginning of your method from:
public static void CalculateTotalCost()
{
retry:
to:
public static void CalculateTotalCost()
{
string input;
retry:
Also, please don't use goto.
There are much better ways.
Try using a loop:
string name = null;
while (name == null)
{
Console.WriteLine("Please enter meaningful information");
string input = Console.ReadLine();
switch (input)
{
case "Teotihuacan":
name = input;
break;
default:
break;
}
}
Console.WriteLine($"You selected: {name}");
You have not declared the input variable.
Do it by
string input;
And then use it.
Or
string input = Console.ReadLine();

Returning an int from a method, and text input in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am simply very confused and could use some help. I dont really know what is going on with the return of num in numCheck method. My teacher said that it should look how it does now so what was returned and how can I use it? I am trying to make a program that asks for the answer to a math problem (e.g. 2 + 2) and if the users input is 4 it end but anything else it asks again. This should have taken 5 mins, I just dont understand what he meant. Thank you!
namespace readNumber
{
class Program
{
static void Main(string[] args)
{
//should be a loop around this method call
//for(......)
numCheck();
Console.ReadLine();
}
static int numCheck()
{
bool itWorked;
int num;
do
{
Console.Write("Enter a Number: ");
string numSt = Console.ReadLine();
Console.WriteLine(numSt);
itWorked = int.TryParse(numSt, out num);
if (itWorked)
{
Console.WriteLine("Integer assigned: " + num);
}
else
{
Console.WriteLine("It Failed");
}
}
while (!itWorked);
return num;
}
}
}
The error I get most is "the name "num" does not exist in the current context" whenever I try to use num in the main program
I will answer the question :
what was returned and how can I use it?
The returned value num of the method numCheck is simply the number entered by the user, actually the method keeps looping until the user enters a valid number, than it breaks the do-while loop and returns num.
Now, you can use checkNum as it is to acquire numbers from the user.
Example:
Console.WriteLine("What is 2+2 ?");
int ans = checkNum();
if(ans == 4)
{
Console.WriteLine("Yes ! that is correct");
}
else
{
Console.WriteLine("No!, false answer");
}
You have not defined any variable named num in your main.
In order to use the value returned from checkNum() you need to assign it to a variable like in the solution by #chouaib.
your main should be like this
static void Main(string[] args)
{
int num;
//should be a loop around this method call
//for(......)
num = numCheck();
Console.ReadLine();
}

How do I turn .Any into a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));

int.TryParse() always returns true [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My program contains a textbox.
I need to check if it gets only numbers, then print.
int num;
if (this.Tree.GetType() == Main.TestInt.GetType())
{
if (int.TryParse(this.label.Text,out num) == true) // i tried without the == before
{
this.Tree.SetInfo(int.Parse(this.TextBox.Text));
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = "";
MessageBox.Show("Only Numbers Allowed", "Error");
}
}
The problem is, for some reason it always returns true, and goes to the
this.Tree.SetInfo(int.Parse(this.TextBox.Text));
Why is it happening?
2 changes:
int num;
if (this.Tree.GetType() == Main.TestInt.GetType())
{
if (int.TryParse(this.TextBox.Text,out num)) //1, you were parsing label.Text
{
this.Tree.SetInfo(num); //2, don't bother parsing it twice!
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = "";
MessageBox.Show("Only Numbers Allowed", "Error");
}
}
Probably you want to check the value of TextBox not the Label. So it would be this.TextBox.Text instead of this.Label.Text
if (int.TryParse(this.TextBox.Text,out num))
{
this.Tree.SetInfo(this.TextBox.Text);
base.label.Text = base.TextBox.Text;
}
else
{
base.TextBox.Text = string.Empty;
MessageBox.Show("Only Numbers Allowed", "Error");
}

Categories

Resources