Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Curly Brace (C# Monodevelop) Question

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Buster



Joined: Sat Jan 02, 2010 5:29 pm
Posts: 7

PostPosted: Thu Apr 01, 2010 8:36 pm    Post subject: Curly Brace (C# Monodevelop) Question Reply with quote

Hi All

I have been stuck on an early part of the WordScramble project for some time now and have tonight realised why it was not working:

Code:

using System;
using System.Collections.Generic;
using System.IO;
   

namespace WordScramble
{
   class MainClass
   {
      static List<string> WordList = new List<string>();
      
      public static void Main(string[] args)
      {
         string[] lines = File.ReadAllLines("wordlist");
         
         foreach(string word in lines) {
            if (word.Length < 3) continue;
            if (word.Contains("'")) continue;
            WordList.Add(word.ToLower());
         }
            bool running = true;
            
            while (running) {
               string input = Console.ReadLine();
                              
               switch (input){
               case "!quit":
                     running = false;
                     break;
               
               default:
                     Console.WriteLine(input);
                     break;
            }
         }
      }
   }
}


Basically I was missing the curly brace between these lines:

Code:

WordList.Add(word.ToLower());
}
bool running = true;


My issue is that without the curly brace the code still compiled without error; it just didn't work as it should do when the code was run.

Can anyone explain why Mondevelop did not throw an error regarding the missing brace in this situation? I don't understand why it didn't and would like to before I move on.

Cheers

Buster
_________________
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1672
Location: Guisborough

PostPosted: Thu Apr 01, 2010 8:59 pm    Post subject: Reply with quote

Edit - misread your post

Code:


using System;
using System.Collections.Generic;
using System.IO;
   

namespace test
{
   class MainClass
   {
      static List<string> WordList = new List<string>();
     
      public static void Main(string[] args)
      {
         string[] lines = File.ReadAllLines("wordlist");
         
         foreach(string word in lines) {
            if (word.Length < 3) continue;
            if (word.Contains("'")) continue;
         
            WordList.Add(word.ToLower());
         
            bool running = true;
           
            while (running) {
               string input = Console.ReadLine();
                             
               switch (input){
               case "!quit":
                     running = false;
                     break;
               
               default:
                     Console.WriteLine(input);
                     break;
            }
         }
      }
   }
}



I've taken the } and press F8 to build. I get the following

Building Solution test

Building: test (Debug)
Performing main compilation...
/usr/bin/gmcs "/out:/test/test/bin/Debug/test.exe" "/r:System.dll" /noconfig /nologo /warn:4 /debug:+ /debug:full /optimize- /codepage:utf8 /define:"DEBUG" /t:exe "/test/test/Main.cs" "test/test/AssemblyInfo.cs"
Compilation failed: 1 error(s), 0 warnings

/test/test/Main.cs(39,3): error CS1513: Expected `}'


Build complete -- 1 error, 0 warnings

---------------------- Done ----------------------

Build: 1 error, 0 warnings
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Buster



Joined: Sat Jan 02, 2010 5:29 pm
Posts: 7

PostPosted: Thu Apr 01, 2010 9:37 pm    Post subject: Reply with quote

Hi Ram

This is very odd. I just ran it again having removed the brace and got this:

Code:

---------------------- Done ----------------------

Build successful.


And the program is no longer behaving like it did before when the brace was missing!

I suspect I must have done something wrong somewhere along the way. I will backtrack and see if I can work it out.

Thanks for the help.

Buster
_________________
My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing!
Back to top
View user's profile Send private message
shanita



Joined: Fri Aug 25, 2006 4:20 pm
Posts: 9
Location: Belgium

PostPosted: Thu Apr 08, 2010 8:26 pm    Post subject: Reply with quote

If you take the brace away between
Code:

WordList.Add(word.ToLower());
         
            bool running = true;
 

Then you get an error while compiling

Build complete -- 1 error, 0 warnings

---------------------- Done ----------------------

Build: 1 error, 0 warnings

But when you put one brace more at the end of your program,
meaning that the while loop is inside the foreach loop.
When you press F8 you will get the message:

---------------------- Done ----------------------

Build successful.

But your program does not exit when you type "!quit" and you have to press twice "Ctrl + c" to exit.
I found it out by placing
Code:

"Console.WriteLine("in quit case");

directly after
Code:
 
case "!quit":
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast