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();
Related
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));
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 2 years ago.
Improve this question
I need to make a program that grades student tests. I'm trying to find a way to use loops and not write out a lot of variables. I thought I could use a switch statement then put an if statement inside of it.
For example, in the loop, if the student's answer for question one equals c then add 1 to the counter, but for some reason my code doesn't work.
int Counter = 0;
string StudAns;
for (int i = 1; i <= 10; i++)
{
Console.Write("Students answer for question {0}: ");
StudAns = Console.ReadLine();
switch (i)
{
case '1':
if (StudAns == "c")
{
Counter++;
}
default:
break;
}
}
There are many problems with your code, but answering the question itself, i is a number, not a character. Change your switch case to the following instead:
case 1: // not '1'
That will highlight the next problem, you're missing a break; before your default case.
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 3 years ago.
Improve this question
PLEASE HELP !!
I don't know what is going on, this piece of my stupid code does not work.
Please help me because I am a newbie to c# and I am just trying to do some code to practice.
I wrote an "If statement" and normally it should work with: {}, but it doesn't.
And there are other several problems, here is the output above the code :
main.cs(14,30): error CS1525: Unexpected symbol `{'
main.cs(17,6): error CS1525: Unexpected symbol `else'
main.cs(23,246): error CS1525: Unexpected symbol `end-of-file'
Compilation failed: 3 error(s), 0 warnings
compiler exit status 1
// this is the code
using System;
public class LOOOOOL {
public static void Main(String [] args)
{
int enterInfo;
int question1;
{
Console.WriteLine("LOOOOOL");
enterInfo = Console.ReadLine();
Console.WriteLine(enterInfo);
question1 = Console.WriteLine("Is that what you wrote ???");
if(String.Contains("y") {
Console.WriteLine("So that's cool, right ???");
}
else
{
Console.WriteLine("Can you re-write it please ???");
question1 = Console.ReadLine();
Console.WriteLine(question1);
}
}
you're missing a closing parathesis:
if(String.Contains("y")) //<---missing extra )
you have an extra bracket:
int question1;
{ //<----what's that doing there?
Console.WriteLine("LOOOOOL");
Reformatted code should look as such:
using System;
public class LOOOOOL
{
public static void Main(String [] args)
{
int enterInfo;
int question1;
Console.WriteLine("LOOOOOL");
enterInfo = Console.ReadLine();
Console.WriteLine(enterInfo);
question1 = Console.WriteLine("Is that what you wrote ???");
if(String.Contains("y"))
{
Console.WriteLine("So that's cool, right ???");
}
else
{
Console.WriteLine("Can you re-write it please ???");
question1 = Console.ReadLine();
Console.WriteLine(question1);
}
}
}
Also, your variables are int... it will never contain "y".... might want to change those two to strings
Also... String.Contains()... what string are you checking? Something for you to look into :)
Perhaps, enterInfo.Contains("y") might be userful for you.
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 5 years ago.
Improve this question
I have a piece of code I'm working with, that involves checking a condition and moving backwards to re-enter information if an invalid input is made. Please find the code, below:
Gender:
Console.WriteLine("Select Gender"
+"\n (M)ale/(F)emale");
input = Console.ReadLine();
if (input=="M" || input==("m"))
commonobj.gender = 1;
else if (input != ("F")||input!="f")
{
Console.WriteLine("Invalid input, please enter again");
goto Gender;
}
Intended behaviour:
If the user inputs 'M' or 'm', the value of the object variable is changed.
If the user inputs 'F' or 'f', the value of the object variable is unchanged.
If the user inputs any other value, an error statement should be displayed and the user asked to re-enter the information.
Variables used:
input - local variable, type String
| commonobj.gender - object variable
Current behaviour:
Entering 'F' or 'f' on the console displays the code under the 'else if' loop,
What am I doing wrong?
Note: The code executes as intended with 'm' being selected on the input.
You need an and in your last conditional statement
Gender:
Console.WriteLine("Select Gender"
+"\n (M)ale/(F)emale");
input = Console.ReadLine();
if (input=="M" || input==("m")) {
commonobj.gender = 1;
} else if (input != ("F") && input!="f")
{
Console.WriteLine("Invalid input, please enter again");
goto Gender;
}
Better to use
if (input.Equals("M", StringComparison.CurrentCultureIgnoreCase)) {
commonobj.gender = 1;
} else if (!input.Equals("F", StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("Invalid input, please enter again");
goto Gender;
}
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();
}