| View previous topic :: View next topic |
| Author |
Message |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Thu Apr 01, 2010 8:36 pm Post subject: Curly Brace (C# Monodevelop) Question |
|
|
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 |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1552 Location: Guisborough
|
Posted: Thu Apr 01, 2010 8:59 pm Post subject: |
|
|
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 _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Thu Apr 01, 2010 9:37 pm Post subject: |
|
|
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 |
|
 |
shanita

Joined: Fri Aug 25, 2006 4:20 pm Posts: 9 Location: Belgium
|
Posted: Thu Apr 08, 2010 8:26 pm Post subject: |
|
|
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
|
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|