Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

} expected

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?
(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: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.
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.
I don't think C# uses the 'var'. You need to declare stuff as int, string and such?
(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'


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Robin2Feb 10, 2018 04:35 AM 0 116 Feb 10, 2018 04:35 AM
Last Post: Robin2
[C] by SureDawggMay 22, 2016 02:37 PM 10 335 May 22, 2016 02:37 PM
Last Post: Remix
[Python] by Avoid00May 20, 2016 10:53 AM 1 117 May 20, 2016 10:53 AM
Last Post: stxd