question about C# in xcode

Discussion in 'Mac Programming' started by twoodcc, Feb 21, 2007.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    for class, i have to write a program that reads a file.

    i'm using 'StreamReader', but it seems xcode doesn't know what StreamReader is, unless i'm doing something wrong.

    can i use StreamReader with xcode and mono and csharpplugin?

    thanks
     
  2. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #2
    I'm 99.999% sure that Xcode and C# aren't friends with each other.
     
  3. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    well they are. i actually got it to work, i forgot to change Using System.IO at the beginning. but now i'm having other problems.

    i'm supposed to ask the user for a name of the file, and then ask the user for a character input, and then search the file and tell how many of that character is in the file.

    here is what i have so far, but it can't compile:

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
    	class WoodT_exercise5
    	{
    		static void Main()
    		{
    			string fileName;
    			char bob;
    			char targetchar;
    			int count = 0;
    			
    			Console.WriteLine("Enter the filename: ");
    			fileName = Console.ReadLine();
    			
    			StreamReader inputFile = new StreamReader(fileName);
    			
    			bob = inputFile.ReadLine();
    			
    			Console.WriteLine("Please enter a character: ");
    			targetchar = Convert.ToChar(Console.ReadLine());
    			
    			while (bob != null)
    			{
    				if (bob = targetchar)
    				{
    					count++;
    					bob = inputFile.Read();
    				}
    			}
    			inputFile.Close();
    		}
    	}
    }
    
     
  4. macrumors 6502

    Joined:
    May 12, 2003
    #4
    What, you're not going to tell us the error messages?

    I don't know any C#. At all. That said, I'll take a stab.

    Code:
    targetchar = Convert.ToChar(Console.ReadLine());
    Now, if I had to guess, Console.ReadLine() would return a string. However, you're attempting to convert it to a character? Seems like that wouldn't work for obvious reasons.

    The other thing that irks me is:

    Code:
    if (bob = targetchar)
    Is that normal for C#? Shouldn't it be "if (bob == targetchar)" ?
     
  5. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #5
    thanks. here are the error messages:

    cannot implicitly convert type string to char
    operator != cannot be applied to operands of type char and null

    you were right about the last error also, and i changed it.

    but this isn't supposed to be a very complex program (shouldn't have to use arrays and stuff, or i don't think)

    this should be easy, just read in the file, look for the characters, count for everyone you find, and bame display and your done.
     
  6. macrumors 6502

    Joined:
    May 12, 2003
    #6
    That would be the string to char thing I talked about....

    Ah. Okay, so chars aren't first class objects, they're primitives. That means, among other things, you can't test them for null this way ('bob != null').

    It looks like you need to start by changing 'bob' from a char to a string. Actually, you need to rename 'bob' to something more descriptive first; that's a really horrible variable name. :p

    It's going to be hard for me to help you seeing as how I have no experience with C# and its various APIs. Maybe someone else can help out here.

    It looks like you're most of the way there. You need to find a way to read in each line of the file and, while the current line is not null, count how many of the desired char is in that line. You will likely need a loop just for that counting task (loop through each char in the string). You could also try looking at the string API and see if there are any easy ways to get the char count.
     
  7. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    thanks.

    if i change both 'bob' and 'targetchar' to stings, then i still get this error:

    cannot implicitly convert type 'int' to 'string'

    and it's talking about this line in the while loop:

    bob = inputFile.Read();

    but i have this same line earlier in the code, and it doesn't get an error?
     
  8. macrumors 6502

    Joined:
    May 12, 2003
    #8
    Okay. I looked it up. Console.Read() gives you back one character (in the form of an int). Console.ReadLine() gives you back a string. Those are different types, so you need to watch that. It seems you're not clear on the difference between a char and a string, so do some research on that.

    As I said, you're most of the way there. You're getting the filename and desired char, opening the filestream, etc. Now it's a matter of doing these steps correctly (you've got some partial code for this, but it needs refinement):
    Code:
    - Get the first line in the file.
    - If the line is not empty:
       - Loop through each char in the line.
          - If the current char is equal to the desired char, then count++
       - Get the next line
    (repeat this as many times as necessary)
    
    So it might look something like this:
    Code:
    string bob = inputFile.ReadLine();
    while (bob != null) {
        // look for the desired chars in bob - leaving this to you
        bob = inputFile.ReadLine();
    }
    and that will cause that while loop to execute for each line in inputFile. See why? Any questions on the logic?
     
  9. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    thank you very much for helping. i'm almost getting there. but here is my main problem :

    i can't get my program to find the file. when i get it to compile, it can't find the file. i'm not sure if it's my compiler (xcode) or what. i might have to try it on a windows machine if i can't get it to work.

    as far as the rest, i'm still not quite there either. i see what you are saying, and i understand the logic. but i need more variables i think. so i added some more, and i changed bob. now i have a theme of names instead (smallville)

    anyway, i'm not done, but i'll go ahead and post what i have right now. thanks again for the help

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
    	class WoodT_exercise5
    	{
    		static void Main()
    		{
    			string fileName;
    			char lana;
    			string clark;
    			string chloe;
    			char lois;
    			int count = 0;
    			
    			Console.WriteLine("Enter the filename: ");
    			fileName = Console.ReadLine();
    			
    			StreamReader inputFile = new StreamReader(fileName);
    			
    			lois = (char)inputFile.Read();
    			chloe = Convert.ToString(lois);
    			
    			Console.WriteLine("Please enter a character: ");
    			clark = Console.ReadLine();
    			
    			lana = Convert.ToChar(clark);
    			
    			while (chloe != null)
    			{
    				if (lois == lana)
    				{
    					count++;
    				}
    				
    				lois = (char)inputFile.Read();
    				chloe = Convert.ToString(lois);
    			}
    			inputFile.Close();
    		}
    	}
    }
     
  10. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #10
    ok, you're not going to believe this, but i think i got it! please look over it and see if it works for you if you can. here is the code:

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
    	class WoodT_exercise5
    	{
    		static void Main()
    		{
    			string fileName;
    			char lana;
    			string clark;
    			string chloe;
    			char lois;
    			int count = 0;
    			
    			Console.WriteLine("Enter the filename: ");
    			fileName = Console.ReadLine();
    			
    			StreamReader inputFile = new StreamReader(fileName);
    			
    			lois = (char)inputFile.Read();
    			chloe = Convert.ToString(lois);
    			
    			Console.WriteLine("Please enter a character: ");
    			clark = Console.ReadLine();
    			
    			lana = Convert.ToChar(clark);
    			chloe = inputFile.ReadLine();
    			
    			while (chloe != null)
    			{
    				CharEnumerator x = chloe.GetEnumerator();
    				while (x.MoveNext() == true)
    				{
    					lois = x.Current;
    					//Console.WriteLine(x.Current);
    					if (lana == lois)
    						{
    							count++;
    						}
    				}   
    				
    				chloe = inputFile.ReadLine();
    			}
    			inputFile.Close();
    			Console.WriteLine(count);
    		}
    	}
    }
     
  11. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    well i thought it worked, but it doesn't. i don't know what it's counting.

    please let me know if you find or think of anything

    thanks
     
  12. macrumors 6502

    Joined:
    May 12, 2003
    #12
    Here's a hint: What happened to clark?





    And please, PLEASE, call your variables what they are and don't give them a cutesy naming scheme. That's fine for computers on a network, but if I were grading your assignment, I'd give you a D-. Then I'd punch you. Seriously, its for your own good. "Is lana equal to lois" will not help you figure this out any quicker.
     
  13. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #13
    well i've changed it since then, but i still don't get it. as for the naming, my teacher actually used "SpongeBob" in class. mainly b/c it really did not matter what you called it. we made the class "SpongeBob".

    but i can still change the names if it makes you happy. here is what i have so far, but even if the file has one letter 'm', it will still count 12 a's somehow.

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
    	class WoodT_exercise5
    	{
    		static void Main()
    		{
    			string fileName;
    			char clark;
    			string chloe;
    			char lois;
    			int count = 0;
    			
    			Console.WriteLine("Enter the filename: ");
    			fileName = Console.ReadLine();
    			
    			StreamReader inputFile = new StreamReader(fileName);
    			
    			Console.WriteLine("Please enter a character: ");
    			clark = Convert.ToChar(Console.ReadLine());
    			
    			
    			chloe = inputFile.ReadLine();
    			
    			
    			while (chloe != null)
    			{
    				
    				
    				CharEnumerator x = chloe.GetEnumerator();
    				while (x.MoveNext() != false)
    				{
    					
    					lois = x.Current;
    	
    					if (clark == lois)
    						{
    							count++;
    						}
    				}   
    				
    				chloe = inputFile.ReadLine();
    			}
    			inputFile.Close();
    			Console.WriteLine(count);
    		}
    	}
    }
     
  14. macrumors 6502

    Joined:
    May 12, 2003
    #14
    Well, look: it doesn't affect me if you name your variables that way. The program is short enough that I can still see what's going on. It's just that it serves no purpose to name them that way, and when you're looking over your code looking for bugs, it gets in the way. Being cavalier with your variable/method/class names is a really bad habit to develop.

    Here's something you can try doing for now:

    Code:
    while (x.MoveNext() != false)
    {				
        lois = x.Current;
    
        Console.WriteLine("Current letter: " + lois);
    
        if (clark == lois)
        {
            count++;
        }
    }  
    
    Maybe the output will give you something to go on.
     
  15. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #15
    i've thought about it, and you are right, and i'll change the names. but after running what you just said, i don't know what this thing is reading. i'm even more confused.

    i don't see where these letters are coming from, do you?
     
  16. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #16
    wait a minute, i think it does work. i created a new text file, and it seems to work. but i'll still change the variale names though. here is what i got:

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
    	class WoodT_exercise5
    	{
    		static void Main()
    		{
    			string fileName;
    			char inputChar;
    			string line;
    			char fileChar;
    			int count = 0;
    			
    			Console.WriteLine("Enter the filename: ");
    			fileName = Console.ReadLine();
    			
    			StreamReader inputFile = new StreamReader(fileName);
    			
    			Console.WriteLine("Please enter a character: ");
    			inputChar = Convert.ToChar(Console.ReadLine());
    			
    			
    			line = inputFile.ReadLine();
    			
    			
    			while (line != null)
    			{
    				
    				
    				CharEnumerator x = line.GetEnumerator();
    				while (x.MoveNext() != false)
    				{
    					
    					fileChar = x.Current;
    					Console.WriteLine(line);
    					Console.WriteLine("Current letter: " + fileChar);
    	
    					if (inputChar == fileChar)
    						{
    							count++;
    						}
    				}   
    				
    				line = inputFile.ReadLine();
    			}
    			inputFile.Close();
    			Console.WriteLine(count);
    		}
    	}
    }
    
     
  17. macrumors member

    Joined:
    Feb 1, 2007
    Location:
    Seattle, WA
    #17
    Yup, looks like it does. I gave it a quick once over and tightened it up. Don't submit this in place of your code, though :)

    Code:
    using System;
    using System.IO;
    
    namespace WoodT_exercise5
    {
        class WoodT_exercise5
        {
            public static void Main()
            {
                int count = 0;
    
                Console.WriteLine("Enter the filename: ");
                string fileName = Console.ReadLine();
    
                string inputText = string.Empty;
                StreamReader inputFile = null;
    
                try
                {
                    inputFile = new StreamReader(fileName);
                    inputText = inputFile.ReadToEnd();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Caught an exception: {0}", ex.ToString());
                    return;
                }
                finally
                {
                    if (null != inputFile)
                        inputFile.Close();
                }
    
                Console.WriteLine("Please enter a character: ");
                char inputChar = Convert.ToChar(Console.ReadLine());
    
                for (int i = 0; i < inputText.Length; i++)
                {
                    if (inputChar == inputText[i])
                        count++;
                }
    
                Console.WriteLine("Count was {0}", count);
            }
        }
    }
    
    Also, if you have an Intel Mac and Parallels or a Windows machine sitting around, you should try out Visual C# Express. It's free and should have everything you need. Feel free to email me if you're curious about learning more: abreth@microsoft.com

    Cheers,
    Aaron
    Program Manager - Non-Professional Tools
    Visual Studio
     
  18. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #18
    hey, thanks a lot! yeah, yours is better, but i won't turn it in.

    i do have an Intel Mac, but i just haven't messed around with installing windows or anything yet.

    thanks, i'll email you if i get into trouble with more complex programs.
     
  19. macrumors 6502

    Joined:
    May 12, 2003
    #19
    I think he was offering help with the Microsoft tools, not with programming questions. :p
     
  20. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #20
    well you're probably right. did you try the last bit of code i posted? just to double check. i'm about to turn this in (and yes i added in-line comments)
     
  21. dcr
    macrumors member

    Joined:
    Jun 10, 2002
  22. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #22
  23. macrumors 6502

    Joined:
    May 12, 2003
    #23
    No, I haven't. As I said before, I'm not a C# developer, so I'm not set up to compile C#, nor do I really want to go to the trouble just for this thread. The code looks pretty much okay to me. And hey, you've got a thumbs up from a Microsoftie, so what do you need me for? :p
     
  24. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #24
    oh, i thought you were able to compile it earlier.

    but you think it looks ok?

    yeah i did, but it never hurts to get a second opinion :)
     
  25. macrumors 6502

    Joined:
    May 12, 2003
    #25
    Nope, unless you count my brain as a compiler.

    Did you take out those Console.WriteLine() statements that I had you add? You don't need them anymore since they were just for debugging. Otherwise, yeah, it looks okay. There is certainly room for improvement -- aaronbrethorst's version is pretty good -- but considering where you are in your education, it's fine. As long as it works. :)
     

Share This Page