...

View Full Version : A custom app. I require help on please



crank01
12-28-2011, 11:39 PM
Hello all, I am pretty new to C# but every day I learn more and more. Now to test my abilities out after learning some of the basics of C# I decided to try writing a (what I thought was) simple console application. All I wanted the application to do was this: you type in from 1-7, 1 == "Monday", 2 == "Tuesday" ect... up to 7. Now I am getting compiling errors, and I kinda don't fully understand them. I would love it if you all could help me fix what ever problems this code has, because I've been spending hours trying to fix it but with my lack of knowledge I cant :(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DaysOfWeek
{
class Program
{
int dayNum;
string dayName;
static void Main(string[] args)
{
(new Program()).run();

}

public void run()
{
int dayNum = readInt("Day of the Week number: ");
writeDay(calculateDay);
}

private int readInt(string p)
{
throw new NotImplementedException();
}
private void writeDay(string p)
{
Console.WriteLine("The day of the Week is: ", p);
}

private string calculateDay()
{
if (dayNum == 0)
dayName = "Sunday";
else if (dayNum == 1)
dayName = "Monday";
else if (dayNum == 2)
dayName = "Tuesday";
else if (dayNum == 3)
dayName = "Wednesday";
else if (dayNum == 4)
dayName = "Thursday";
else if (dayNum == 5)
dayName = "Friday";
else if (dayNum == 6)
dayName = "Saturday";
else
dayName = "unknown";

return dayName.ToString;
}
private string readInt(int p)
{
Console.Write(p);
string line = Console.ReadLine();
return dayName;
}

}
}

Now this isn't in any tutorials online, I just wanted to see if I could all by myself and I apparently can't, but I'm glad I tried I guess lol. Plz help

Fou-Lu
12-29-2011, 12:09 AM
int dayNum = readInt("Day of the Week number: "); is wrong, or the definition is wrong. I'm leaning towards the definition. That should be private int readInt(string p), and you need to cast the string from console.readline to an integer to use. I think C# simply uses int.Parse() for that. You have a method for this, but it hasn't been implemented and throws an exception.

writeDay(calculateDay); is undoable. There is no calculateDay variable, though I do see a function called that. The signature either needs to be modified to accept a Method and treat it as a delegate, or you need to simply capture the results of the call to calculateDay and send those to the writeDay() (which makes the most sense). Can be done in a single line call, but you need to use calculateDay() and not calculateDay. In CalculateDay, consider using a switch instead.

crank01
12-29-2011, 12:45 AM
int dayNum = readInt("Day of the Week number: "); is wrong, or the definition is wrong. I'm leaning towards the definition. That should be private int readInt(string p), and you need to cast the string from console.readline to an integer to use. I think C# simply uses int.Parse() for that. You have a method for this, but it hasn't been implemented and throws an exception.

writeDay(calculateDay); is undoable. There is no calculateDay variable, though I do see a function called that. The signature either needs to be modified to accept a Method and treat it as a delegate, or you need to simply capture the results of the call to calculateDay and send those to the writeDay() (which makes the most sense). Can be done in a single line call, but you need to use calculateDay() and not calculateDay. In CalculateDay, consider using a switch instead.
Thanks again Fou-Lou(you use to help me in css) Now I am just learning about switches as I type this lol, so I am not all that knowledgeable on the matter, so I hope I don't have to use them for this application without it being too difficult. I am going to try to use your suggestions but I'm having a hard time visualizing the fix since I'm new at this. To make it simpler could you put the changes in a code bracket? It would make understanding simpler :)

I tried this which compiles but doesn't actually work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DaysOfWeek
{
class Program
{
int dayNum = 0;
string dayName;
static void Main(string[] args)
{
(new Program()).run();

}

public void run()
{
int dayNum = readInt("Day of the Week number: ");
calculateDay();
}

private int readInt(string p)
{
throw new NotImplementedException();
}
private void writeDay(string p)
{
Console.WriteLine("The day of the Week is: ", p);
}

private string calculateDay()
{
if (dayNum == 0)
dayName = "Sunday";
else if (dayNum == 1)
dayName = "Monday";
else if (dayNum == 2)
dayName = "Tuesday";
else if (dayNum == 3)
dayName = "Wednesday";
else if (dayNum == 4)
dayName = "Thursday";
else if (dayNum == 5)
dayName = "Friday";
else if (dayNum == 6)
dayName = "Saturday";
else
dayName = "unknown";

return dayName;
}
private string readInt(int p)
{
Console.Write(p);
string line = Console.ReadLine();
return dayName;
}

}
}


I have no ideas :(

crank01
12-29-2011, 06:30 AM
I have tried this which compiles, and runs but won't return the day of the week (monday, tuesday, etc) after I type in 1-7 :(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DaysOfWeek
{
class Program
{
int dayNum = 0;
string dayName;
static void Main(string[] args)
{
(new Program()).run();

}

public void run()
{
int dayNum = readInt("Enter day of the Week number: ");
writeDay();
}

private int readInt(string dayNum)
{
Console.Write(dayNum);
string line = Console.ReadLine();
return int.Parse(line);
}
private void writeDay()
{
Console.WriteLine("The day of the Week is: ", dayName);
}
private string calculateDay()
{
if (dayNum == 0)
dayName = "Sunday";
else if (dayNum == 1)
dayName = "Monday";
else if (dayNum == 2)
dayName = "Tuesday";
else if (dayNum == 3)
dayName = "Wednesday";
else if (dayNum == 4)
dayName = "Thursday";
else if (dayNum == 5)
dayName = "Friday";
else if (dayNum == 6)
dayName = "Saturday";
else
dayName = "unknown";

return dayName;
}
private double readInt(int dayNum)
{
Console.Write(dayNum);
string line = Console.ReadLine();
return int.Parse(line);
}

}
}


any more ideas guys?

melloorr
12-29-2011, 12:15 PM
Use this to help you out: http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.80).aspx

From their example, you should be able to work out how to get it to work.

alykins
12-29-2011, 12:27 PM
a few things but let's tackle them one at a time... first the only three fxns called are
run, readint, and writeDay
Now writeDay will always write nothing (or it might throw an error) because the string is never being set... also another big oops in there


Console.WriteLine("The day of the Week is: ", dayName);

very close...


Console.WriteLine("The day of the week is: {0}", dayName);

worth noting is that not all strings will be able to "do that"... Console.WriteLine already handles the placement when using "{0} text {1}" , varA, varB ... if you get errors trying to do that then you will need the following (which is what Console.WriteLine does) string.Format("{0} text {1}", varA, varB)... to help you understand consider the following example based on what you have...


string _writeConsole = string.Format("The day of the week is: {0}", dayName);
Console.WriteLine(_writeConsole);

now with only the three fxns in use (didn't look at the others since not being used) there is another spot that is problematic in readInt


return int.Parse(line);

return is never declared, and you cannot declare it since int is a reserved word. you need to do that to an int... so


... in some function ...
int myInt = 0;
string myString = "10";
myInt = int.Parse(myString);
return myInt;

hope that helps move you forward

crank01
12-29-2011, 06:24 PM
Thanks everyone for the help :D I'll get crackin on the fixes. Sorry about the late response, I was up til 4 in the morning reading on C#, trying to learn more & more day by day :thumbsup: I'll get started on these fixes and respond with any questions I may have. Thx all
Edit: Ok I tried my best to figure out how to solve this based on your suggestions but still without any luck. The day (saturday, sunday etc..) won't write... This is frustrating lol. I would like to do this without using switches, so I can learn this way before I move to that, even tho Im sure I'll have just as much difficulty on those as I will with methods. Heres my updated code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DaysOfWeek
{
class Program
{
int dayNum = 0;
string dayName;
static void Main(string[] args)
{
(new Program()).run();

}

public void run()
{
int dayNum = readInt("Enter day of the Week number: ");
writeDay();
}

private int readInt(string dayNum)
{
Console.Write(dayNum);
string line = Console.ReadLine();
return int.Parse(line);
}
private void writeDay()
{
Console.WriteLine("The day of the week is: {0}", dayName);
}
private string calculateDay()
{
if (dayNum == 0)
dayName = "Sunday";
else if (dayNum == 1)
dayName = "Monday";
else if (dayNum == 2)
dayName = "Tuesday";
else if (dayNum == 3)
dayName = "Wednesday";
else if (dayNum == 4)
dayName = "Thursday";
else if (dayNum == 5)
dayName = "Friday";
else if (dayNum == 6)
dayName = "Saturday";
else
dayName = "unknown";

return dayName;
}
private double readInt(int dayNum)
{
Console.Write(dayNum);
string line = Console.ReadLine();
int myInt = 0;
string myString = "10";
myInt = int.Parse(myString);
return myInt;
}

}
}


I'm sooo confused

melloorr
12-29-2011, 07:19 PM
This will work (with much much less code):


using System;

class Program
{
static void Main()
{
int dayNum;
string dayName;

Console.WriteLine("Please enter 1 to 7: ");
dayNum = int.Parse(Console.ReadLine());

if (dayNum == 1)
dayName = "Sunday";
else if (dayNum == 2)
dayName = "Monday";
else if (dayNum == 3)
dayName = "Tuesday";
else if (dayNum == 4)
dayName = "Wednesday";
else if (dayNum == 5)
dayName = "Thursday";
else if (dayNum == 6)
dayName = "Friday";
else if (dayNum == 7)
dayName = "Saturday";
else
dayName = "unknown";

Console.WriteLine("The day of the week is: " + dayName);
Console.ReadLine();


}
}


Or do you want it so they have to type in the day?

crank01
12-29-2011, 09:20 PM
Thank you man =) Let me try it out and see how it is. Have any ideas why my code wouldn't write the day out? It seems at tho it would *scratching head*

melloorr
12-29-2011, 09:34 PM
Thank you man =) Let me try it out and see how it is. Have any ideas why my code wouldn't write the day out? It seems at tho it would *scratching head*

Could be loads of things, if you don't mind me saying, your code is a bit hectic.

With a small program, you only really need one Method. Its when your program has multiple jobs that you need more.

crank01
12-29-2011, 11:18 PM
Could be loads of things, if you don't mind me saying, your code is a bit hectic.

With a small program, you only really need one Method. Its when your program has multiple jobs that you need more.
Lol no problem, I know its hectic :D I just don't know too much about C# yet so any criticism is good criticism :thumbsup:

melloorr
12-29-2011, 11:26 PM
If you want some thing to do, here's a list:
Ask for their name and
1. Display it in upper case.
2. Enter each character seperatly when Enter is pressed.
3. Display it backwards.

Then ask for their name and number, then save that to a text file.

Then display all the prime numbers between 1 and 1000.

Then display a random int number between 1 and 1000.

Then you can put them all in the same code file and use a switch to choose which one to run.


Not much but it should help you learn. If you need any help if you do it, then just ask here and I/we will help

crank01
12-29-2011, 11:53 PM
If you want some thing to do, here's a list:
Ask for their name and
1. Display it in upper case.
2. Enter each character seperatly when Enter is pressed.
3. Display it backwards.

Then ask for their name and number, then save that to a text file.

Then display all the prime numbers between 1 and 1000.

Then display a random int number between 1 and 1000.

Then you can put them all in the same code file and use a switch to choose which one to run.


Not much but it should help you learn. If you need any help if you do it, then just ask here and I/we will help
That sounds like a really interesting challenge that I would love to tackle :) I haven't yet learned how to write and save input from a user to an external document file tho. Any idea where I can find the methods to call these functions? <= I think they are called methods to call functions :confused: I'm probably wrong tho so please correct me so I don't look like an idiot next time :D

melloorr
12-29-2011, 11:58 PM
Methods are a block of code. I.E. Main() is a method. And you just call a Method using Main() inside a different method.

And here is a link to save text to a file: http://www.dotnetperls.com/streamwriter

crank01
12-30-2011, 12:16 AM
Thanks again =) I'll get crackin on it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum