| View previous topic :: View next topic |
| Author |
Message |
Marrea LXF regular

Joined: Fri Apr 08, 2005 10:32 pm Posts: 1846 Location: Chilterns, West Hertfordshire
|
Posted: Tue Feb 02, 2010 9:55 pm Post subject: Bit stuck with Coding Academy Project One |
|
|
I've had a number of tries at getting through Project One but so far have always come unstuck at the “Adding things to do” section, which follows on from the section which explains how to print out in upper case. At that point I have the following in Mono:
| Code: | using System;
using System.IO;
namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
var contents = File.ReadAllText("todo.txt");
foreach (var arg in args) {
Console.WriteLine(arg);
Console.WriteLine(arg.ToUpper());
}
}
}
}
|
Then under the “Adding things to do" heading, Paul explains (1) about C# counting all its arrays from 0, not 1; and (2) if you want to check whether a variable is equal to something, you use == (two equals signs). He then says:
With those two important points in mind, here's how to check whether the first argument is “add” or not:
| Code: | if (args[0] == “add”) {
//do something here
} |
This is where I am stuck. Where do I insert the | Code: | | if (args[0] == “add”) { | line into the code I already have? Or does some of the existing code need to be removed first?
Incidentally, a bit further on (middle of right hand column, page 11, and a third of the way down the left hand column, page 12), Paul has further lines of code without the bracket before args, ie
| Code: | | if args[0] == “add”) { |
Is that a mistake? Should there be a bracket in all cases where that particular line appears?
If someone could steer me in the right direction I should be most grateful. |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Wed Feb 03, 2010 5:55 pm Post subject: |
|
|
yo marrea i don't know a lot of c# i'm more c++, as far as i know all arrays start at 0, and == is a boolean operator to test true or not(equal to (sorry i didn't say that very well) rather than equals)
yes there should either be a pair of () or none, all brackets have to have pairs whether it's {} [] or ()
hth
paul _________________ i am a follower of the culture
 |
|
| Back to top |
|
 |
Hudzilla Site admin

Joined: Mon Apr 04, 2005 12:52 pm Posts: 265 Location: LXF Towers
|
Posted: Wed Feb 03, 2010 11:17 pm Post subject: |
|
|
Hey!
For your first question: put it at the start of Main(). You'll add a little more code to it later in the project, but at the end Main() really isn't that big and contains the entire program.
For your second question: sorry, that's a mistake in the magazine! "if" statements always need to have their conditions inside brackets, ie "if (foobarbaz) {".
If you are unsure where some code should go, you can look at the examples on the DVD.
Paul |
|
| Back to top |
|
 |
Marrea LXF regular

Joined: Fri Apr 08, 2005 10:32 pm Posts: 1846 Location: Chilterns, West Hertfordshire
|
Posted: Thu Feb 04, 2010 4:48 pm Post subject: |
|
|
Many thanks 1sf (troll) and Hudzilla for your replies. I had somehow overlooked the fact that the code was on the DVD, so have now printed off Main.cs so I can have it beside me to refer to. However, I am still struggling. I can't get Mono to print out the item numbers as shown in Paul's screenshot at the bottom of page 12.
My code looks like this:
| Code: | 1. using System;
2. using System.IO;
3.
4. namespace ToDoList
5. {
6. class MainClass
7. { public static void Main(string[] args)
8. {
9. if (args[0] == "add") {
10. File.AppendAllText("todo.txt", args[1] + "\n");
11. } else {
12. var contents = File.ReadAllLines("todo.txt");
13. var counter = 1
14. foreach(var item in contents) {
15. Console.WriteLine(counter + ": " + item);
16. counter = counter + 1;
17. }
18. }
19. }
20. } |
This reports an error at Line 14 "Unexpected symbol `foreach'(CS1525)"
When I run the command ./ToDoList.exe list it prints out the items, but without numbers, as follows:
| Code: | marrea@marrea-desktop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe list
Meh
Wash socks
Feed cats
Take over the Earth
|
I assume there is a problem with the "foreach" line in my code but cannot figure out what it is. |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1547 Location: Guisborough
|
Posted: Thu Feb 04, 2010 4:56 pm Post subject: |
|
|
I'd say your missing a ; at the end of line 13 _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
Marrea LXF regular

Joined: Fri Apr 08, 2005 10:32 pm Posts: 1846 Location: Chilterns, West Hertfordshire
|
Posted: Thu Feb 04, 2010 5:31 pm Post subject: |
|
|
| Ram wrote: | | I'd say your missing a ; at the end of line 13 |
Doh! What an idiot I am!
Thanks, Ram, that indeed was it.
I can see I am going to have to pay much more attention to details like this if I am going to get anywhere with programming.  |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Thu Feb 04, 2010 5:33 pm Post subject: |
|
|
| Marrea wrote: | | Ram wrote: | | I'd say your missing a ; at the end of line 13 |
Doh! What an idiot I am!
Thanks, Ram, that indeed was it.
I can see I am going to have to pay much more attention to details like this if I am going to get anywhere with programming.  |
that's normally what i get the most(believe it or not) syntax errors
paul _________________ i am a follower of the culture
 |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1547 Location: Guisborough
|
Posted: Thu Feb 04, 2010 5:42 pm Post subject: |
|
|
| Marrea wrote: |
I can see I am going to have to pay much more attention to details like this if I am going to get anywhere with programming.  |
You are and you'll still make the same mistake again, I'm sure.
Generally if I get an error. I check the code before where the complier complains as it usually a typo of some discription. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
Marrea LXF regular

Joined: Fri Apr 08, 2005 10:32 pm Posts: 1846 Location: Chilterns, West Hertfordshire
|
Posted: Thu Feb 04, 2010 6:22 pm Post subject: |
|
|
It's amazing how a fresh eye is often best at spotting errors like this. You can read through your own work over and over again and completely miss what is wrong, but someone else can look at it and see the mistake immediately.  |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1547 Location: Guisborough
|
Posted: Thu Feb 04, 2010 7:21 pm Post subject: |
|
|
How true that is  _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
Dutch_Master LXF regular
Joined: Tue Mar 27, 2007 2:49 am Posts: 2353
|
Posted: Thu Feb 04, 2010 7:33 pm Post subject: |
|
|
| 1slipperyfish (troll) wrote: | that's normally what i get the most(believe it or not) syntax errors
paul | You are a syntax error: in your chromosomes
 |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Thu Feb 04, 2010 7:38 pm Post subject: |
|
|
anyway marrea keep with it, and it will soon be second nature finding compiler errors
paul _________________ i am a follower of the culture
 |
|
| Back to top |
|
 |
Marrea LXF regular

Joined: Fri Apr 08, 2005 10:32 pm Posts: 1846 Location: Chilterns, West Hertfordshire
|
Posted: Thu Feb 04, 2010 7:46 pm Post subject: |
|
|
| 1slipperyfish (troll) wrote: | .. it will soon be second nature finding compiler errors |
Ah, so it wasn't my mistake after all. It was entirely the fault of the compiler!  |
|
| Back to top |
|
 |
1slipperyfish Forum Jester

Joined: Mon May 09, 2005 3:52 pm Posts: 2366 Location: wigan
|
Posted: Thu Feb 04, 2010 7:47 pm Post subject: |
|
|
| Marrea wrote: | | 1slipperyfish (troll) wrote: | .. it will soon be second nature finding compiler errors |
Ah, so it wasn't my mistake after all. It was entirely the fault of the compiler!  |
exactly i always blame the compiler when my code doesn't work
paul _________________ i am a follower of the culture
 |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Thu Feb 04, 2010 10:20 pm Post subject: |
|
|
Oooo I wish I could code like the big guns...
1sf (troll)...
Put some of your own C(++) code on here so that we
can see it.
You`ve told us of your exploits but we never ever see
any of it...
Over to you... ;oD _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|