Bit stuck with Coding Academy Project One

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Bit stuck with Coding Academy Project One

Postby Marrea » Tue Feb 02, 2010 9:55 pm

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: Select all
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: Select all
if (args[0] == “add”) {
//do something here
}


This is where I am stuck. Where do I insert the
Code: Select all
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: Select all
 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.
User avatar
Marrea
LXF regular
 
Posts: 1877
Joined: Fri Apr 08, 2005 9:32 pm
Location: Chilterns, West Hertfordshire

Postby 1slipperyfish » Wed Feb 03, 2010 5:55 pm

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 operatorto 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 () :D
hth :D
paul
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby Hudzilla » Wed Feb 03, 2010 11:17 pm

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
User avatar
Hudzilla
Site admin
 
Posts: 265
Joined: Mon Apr 04, 2005 11:52 am
Location: LXF Towers

Postby Marrea » Thu Feb 04, 2010 4:48 pm

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: Select all
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: Select all
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.
User avatar
Marrea
LXF regular
 
Posts: 1877
Joined: Fri Apr 08, 2005 9:32 pm
Location: Chilterns, West Hertfordshire

Postby Ram » Thu Feb 04, 2010 4:56 pm

I'd say your missing a ; at the end of line 13

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1679
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Marrea » Thu Feb 04, 2010 5:31 pm

Ram wrote:I'd say your missing a ; at the end of line 13


Doh! What an idiot I am! :oops:

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. :roll:
User avatar
Marrea
LXF regular
 
Posts: 1877
Joined: Fri Apr 08, 2005 9:32 pm
Location: Chilterns, West Hertfordshire

Postby 1slipperyfish » Thu Feb 04, 2010 5:33 pm

Marrea wrote:
Ram wrote:I'd say your missing a ; at the end of line 13


Doh! What an idiot I am! :oops:

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. :roll:

that's normally what i get the most(believe it or not) syntax errors :D
paul
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby Ram » Thu Feb 04, 2010 5:42 pm

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. :roll:


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.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1679
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Marrea » Thu Feb 04, 2010 6:22 pm

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. :wink:
User avatar
Marrea
LXF regular
 
Posts: 1877
Joined: Fri Apr 08, 2005 9:32 pm
Location: Chilterns, West Hertfordshire

Postby Ram » Thu Feb 04, 2010 7:21 pm

How true that is :)

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1679
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Dutch_Master » Thu Feb 04, 2010 7:33 pm

1slipperyfish (troll) wrote:that's normally what i get the most(believe it or not) syntax errors :D
paul
You are a syntax error: in your chromosomes :evil: :evil: :roll:

:mrgreen:
Dutch_Master
LXF regular
 
Posts: 2452
Joined: Tue Mar 27, 2007 1:49 am

Postby 1slipperyfish » Thu Feb 04, 2010 7:38 pm

anyway marrea keep with it, and it will soon be second nature finding compiler errors :D
paul
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby Marrea » Thu Feb 04, 2010 7:46 pm

1slipperyfish (troll) wrote:.. it will soon be second nature finding compiler errors :D


Ah, so it wasn't my mistake after all. It was entirely the fault of the compiler! :D
User avatar
Marrea
LXF regular
 
Posts: 1877
Joined: Fri Apr 08, 2005 9:32 pm
Location: Chilterns, West Hertfordshire

Postby 1slipperyfish » Thu Feb 04, 2010 7:47 pm

Marrea wrote:
1slipperyfish (troll) wrote:.. it will soon be second nature finding compiler errors :D


Ah, so it wasn't my mistake after all. It was entirely the fault of the compiler! :D

exactly i always blame the compiler when my code doesn't work :D
paul
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2432
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby Bazza » Thu Feb 04, 2010 10:20 pm

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...
User avatar
Bazza
LXF regular
 
Posts: 1476
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest