Hey I need a bit of help w/ some c# code, very basic as I've just started study but it's really annoying me and I can't see what the fuck is wrong with it.
https://gyazo.com/6d884daffe7a14f70bbcdb6d74d35163
The error says it's cause by the bottom line but there IS a } there, and visual studio doesn't see it or something idk. pls help
Maybe like this?
On your code the IF statement is only being applied to this line:
Code Console.WriteLine("Enter your seconde number: ");
And thus the brackets are not being properly used.
So you should write it inside the brackets:
Code if (answer == "Y")
{
Console.WriteLine("Enter your seconde number: ");
var second = double.Parse(Console.ReadLine());
}
(Mar 23, 2017 06:10 AM)Molino. Wrote: Maybe like this?
On your code the IF statement is only being applied to this line:
Code Console.WriteLine("Enter your seconde number: ");
And thus the brackets are not being properly used.
So you should write it inside the brackets:
Code if (answer == "Y")
{
Console.WriteLine("Enter your seconde number: ");
var second = double.Parse(Console.ReadLine());
}
Thanks that fixed it, but it causes another error saying second does not exist in the current context?
DO NOT DEAL WITH THIS MEMBER
Mar 23, 2017 06:14 AM
(This post was last modified: Mar 23, 2017 06:15 AM by Annabelle Lee.)
(Mar 23, 2017 06:10 AM)Molino. Wrote: Maybe like this?
On your code the IF statement is only being applied to this line:
Code Console.WriteLine("Enter your seconde number: ");
And thus the brackets are not being properly used.
So you should write it inside the brackets:
Code if (answer == "Y")
{
Console.WriteLine("Enter your seconde number: ");
var second = double.Parse(Console.ReadLine());
}
Basically what this user says. You have to put the brackets around what you want to check, all of it is essentially.
(Mar 23, 2017 06:13 AM)Hurdle Wrote: Thanks that fixed it, but it causes another error saying second does not exist in the current context?
You need to declare var outside of the if statement. Right now it is local to those brackets and is destroyed once control leaves those brackets.
Mar 23, 2017 06:15 AM
(This post was last modified: Mar 23, 2017 06:16 AM by Molino..)
(Mar 23, 2017 06:13 AM)Hurdle Wrote: Thanks that fixed it, but it causes another error saying second does not exist in the current context?
Is it an error or a warning?
What code are you running next? Are you calling the variable $second again?
(Mar 23, 2017 06:14 AM)Annabelle Lee Wrote: You need to declare var outside of the if statement. Right now it is local to those brackets and is destroyed once control leaves those brackets.
Yep, this might be it. Didnt noticed that.
(Mar 23, 2017 06:15 AM)Molino. Wrote: Is it an error or a warning?
What code are you running next? Are you calling the variable $second again?
Yep, this might be it. Didnt noticed that.
It's an error, how would I go about declaring it outside of the brackets? I just tried writing var second; earlier in the program and that just caused more errors.
Mar 23, 2017 06:24 AM
(This post was last modified: Mar 23, 2017 06:26 AM by Molino..)
Code static void Main()
{
double second = 0;
string answer = "Y";
if (answer == "Y")
{
Console.WriteLine("Enter your seconde number: ");
second = double.Parse(Console.ReadLine());
}
Console.WriteLine("{0}", second);
}
Just tried this code, and gave me no errors.
I started by declaring the variable outside of the IF statement.
The print in the end its just so i can test if it worked...
The code works, but might not be 100% as in perfect. Some one else might give an opinion on that.
(Mar 23, 2017 06:24 AM)Molino. Wrote: Just tried this code, and gave me no errors.
I started by declaring the variable outside of the IF statement.
The print in the end its just so i can test if it worked...
The code works, but might not be 100% as in perfect. Some one else might give an opinion on that.
This just made it go back to the } expected error, do you mind if I just drop all the code in a pastebin or something? I feel like it'd be easier, thanks for the help btw, Google didn't show me anything relevant to what I was doing.
(Mar 23, 2017 06:35 AM)Hurdle Wrote: This just made it go back to the } expected error, do you mind if I just drop all the code in a pastebin or something? I feel like it'd be easier, thanks for the help btw, Google didn't show me anything relevant to what I was doing.
No problem, just put it on a pastebin and ill try to help :)
(Mar 23, 2017 06:41 AM)Molino. Wrote: No problem, just put it on a pastebin and ill try to help :)
http://pastebin.com/GeCAgAYu
Started learning C# this week, these errors are super annoying when you can't figure out how to fix it.
DO NOT DEAL WITH THIS MEMBER
I don't think C# uses the 'var'. You need to declare stuff as int, string and such?
Mar 23, 2017 07:01 AM
(This post was last modified: Mar 23, 2017 07:02 AM by Molino..)
(Mar 23, 2017 06:57 AM)Annabelle Lee Wrote: I don't think C# uses the 'var'. You need to declare stuff as int, string and such?
You can use var, but i think its much more safe to actually declare the variable type.
To OP.
I'll post the code in a second, but please note that using Labels and GOTO is not efficient... I would advise not to use it...
(Mar 23, 2017 07:01 AM)Molino. Wrote: You can use var, but i think its much more safe to actually declare the variable type.
To OP.
I'll post the code in a second, but please note that using Labels and GOTO is not efficient... I would advise not to use it...
What should I use instead to do the same thing? It was just the first function I found that did what I needed.
Try this code for now:
http://pastebin.com/p3Eag24z
That code works as you want, but I still think the use of Labels and GOTO is just not efficient.
If you want, I can later re-write the code using either functions or a proper Class and methods for the context...
(Mar 23, 2017 07:06 AM)Molino. Wrote: Try this code for now:
http://pastebin.com/p3Eag24z
That code works as you want, but I still think the use of Labels and GOTO is just not efficient.
If you want, I can later re-write the code using either functions or a proper Class and methods for the context...
Damn thanks dude, appreciate it. That looks much better but there's an error I had earlier now.
Program.cs(49,80): error CS0165: Use of unassigned local variable 'result'
|