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 

Bit stuck with Coding Academy Project One
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Marrea
LXF regular


Joined: Fri Apr 08, 2005 10:32 pm
Posts: 1872
Location: Chilterns, West Hertfordshire

PostPosted: Tue Feb 02, 2010 9:55 pm    Post subject: Bit stuck with Coding Academy Project One Reply with quote

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
View user's profile Send private message
1slipperyfish
Forum Jester


Joined: Mon May 09, 2005 3:52 pm
Posts: 2429
Location: wigan

PostPosted: Wed Feb 03, 2010 5:55 pm    Post subject: Reply with quote

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 () Very Happy
hth Very Happy
paul
_________________
i am a follower of the culture

Back to top
View user's profile Send private message
Hudzilla
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 265
Location: LXF Towers

PostPosted: Wed Feb 03, 2010 11:17 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Marrea
LXF regular


Joined: Fri Apr 08, 2005 10:32 pm
Posts: 1872
Location: Chilterns, West Hertfordshire

PostPosted: Thu Feb 04, 2010 4:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ram
LXF regular


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

PostPosted: Thu Feb 04, 2010 4:56 pm    Post subject: Reply with quote

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.....
Back to top
View user's profile Send private message MSN Messenger
Marrea
LXF regular


Joined: Fri Apr 08, 2005 10:32 pm
Posts: 1872
Location: Chilterns, West Hertfordshire

PostPosted: Thu Feb 04, 2010 5:31 pm    Post subject: Reply with quote

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


Doh! What an idiot I am! Embarassed

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. Rolling Eyes
Back to top
View user's profile Send private message
1slipperyfish
Forum Jester


Joined: Mon May 09, 2005 3:52 pm
Posts: 2429
Location: wigan

PostPosted: Thu Feb 04, 2010 5:33 pm    Post subject: Reply with quote

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


Doh! What an idiot I am! Embarassed

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. Rolling Eyes

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

Back to top
View user's profile Send private message
Ram
LXF regular


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

PostPosted: Thu Feb 04, 2010 5:42 pm    Post subject: Reply with quote

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. Rolling Eyes


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.....
Back to top
View user's profile Send private message MSN Messenger
Marrea
LXF regular


Joined: Fri Apr 08, 2005 10:32 pm
Posts: 1872
Location: Chilterns, West Hertfordshire

PostPosted: Thu Feb 04, 2010 6:22 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Ram
LXF regular


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

PostPosted: Thu Feb 04, 2010 7:21 pm    Post subject: Reply with quote

How true that is Smile
_________________

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
Dutch_Master
LXF regular


Joined: Tue Mar 27, 2007 2:49 am
Posts: 2430

PostPosted: Thu Feb 04, 2010 7:33 pm    Post subject: Reply with quote

1slipperyfish (troll) wrote:
that's normally what i get the most(believe it or not) syntax errors Very Happy
paul
You are a syntax error: in your chromosomes Evil or Very Mad Evil or Very Mad Rolling Eyes

Mr. Green
Back to top
View user's profile Send private message
1slipperyfish
Forum Jester


Joined: Mon May 09, 2005 3:52 pm
Posts: 2429
Location: wigan

PostPosted: Thu Feb 04, 2010 7:38 pm    Post subject: Reply with quote

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

Back to top
View user's profile Send private message
Marrea
LXF regular


Joined: Fri Apr 08, 2005 10:32 pm
Posts: 1872
Location: Chilterns, West Hertfordshire

PostPosted: Thu Feb 04, 2010 7:46 pm    Post subject: Reply with quote

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


Ah, so it wasn't my mistake after all. It was entirely the fault of the compiler! Very Happy
Back to top
View user's profile Send private message
1slipperyfish
Forum Jester


Joined: Mon May 09, 2005 3:52 pm
Posts: 2429
Location: wigan

PostPosted: Thu Feb 04, 2010 7:47 pm    Post subject: Reply with quote

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


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

exactly i always blame the compiler when my code doesn't work Very Happy
paul
_________________
i am a follower of the culture

Back to top
View user's profile Send private message
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1474
Location: Loughborough

PostPosted: Thu Feb 04, 2010 10:20 pm    Post subject: Reply with quote

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 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
Goto page 1, 2  Next
Page 1 of 2

 
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