I'm 99.999% sure that Xcode and C# aren't friends with each other.
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();
}
}
}
here is what i have so far, but it can't compile:
targetchar = Convert.ToChar(Console.ReadLine());
if (bob = targetchar)
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:
Is that normal for C#? Shouldn't it be "if (bob == targetchar)" ?Code:if (bob = targetchar)
That would be the string to char thing I talked about....thanks. here are the error messages:
cannot implicitly convert type string to char
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').operator != cannot be applied to operands of type char and null
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.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.
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. 😛
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.
- 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)
string bob = inputFile.ReadLine();
while (bob != null) {
// look for the desired chars in bob - leaving this to you
bob = inputFile.ReadLine();
}
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):
So it might look something like this: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)
and that will cause that while loop to execute for each line in inputFile. See why? Any questions on the logic?Code:string bob = inputFile.ReadLine(); while (bob != null) { // look for the desired chars in bob - leaving this to you bob = inputFile.ReadLine(); }
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();
}
}
}
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);
}
}
}
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.
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);
}
}
}
while (x.MoveNext() != false)
{
lois = x.Current;
Console.WriteLine("Current letter: " + lois);
if (clark == lois)
{
count++;
}
}
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.
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);
}
}
}
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.
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);
}
}
}
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
I think he was offering help with the Microsoft tools, not with programming questions. 😛thanks, i'll email you if i get into trouble with more complex programs.
I think he was offering help with the Microsoft tools, not with programming questions. 😛
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)
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? 😛
Nope, unless you count my brain as a compiler.oh, i thought you were able to compile it earlier.
but you think it looks ok?