Curly Brace (C# Monodevelop) Question

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Curly Brace (C# Monodevelop) Question

Postby Buster » Thu Apr 01, 2010 7:36 pm

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: Select all
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: Select all
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!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm

Postby Ram » Thu Apr 01, 2010 7:59 pm

Edit - misread your post

Code: Select all

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.....
User avatar
Ram
LXF regular
 
Posts: 1682
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Buster » Thu Apr 01, 2010 8:37 pm

Hi Ram

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

Code: Select all
---------------------- 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!
Buster
 
Posts: 7
Joined: Sat Jan 02, 2010 5:29 pm

Postby shanita » Thu Apr 08, 2010 7:26 pm

If you take the brace away between
Code: Select all
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: Select all
"Console.WriteLine("in quit case");

directly after
Code: Select all
 
case "!quit":
User avatar
shanita
 
Posts: 9
Joined: Fri Aug 25, 2006 3:20 pm
Location: Belgium


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests