Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts

    Exclamation A custom app. I require help on please

    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
    Code:
    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    Last edited by Fou-Lu; 12-29-2011 at 12:12 AM.

  • #3
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    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:
    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("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
    Last edited by crank01; 12-29-2011 at 01:03 AM.

  • #4
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    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
    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: ", 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?

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Use this to help you out: http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

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

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,725
    Thanks
    41
    Thanked 191 Times in 190 Posts
    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
    Code:
    Console.WriteLine("The day of the Week is: ", dayName);
    very close...
    Code:
    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...
    Code:
    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
    Code:
    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
    Code:
    ... in some function ...
    int myInt = 0;
    string myString = "10";
    myInt = int.Parse(myString);
    return myInt;
    hope that helps move you forward

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Thanks everyone for the help 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 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:
    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
    Last edited by crank01; 12-29-2011 at 06:50 PM.

  • #8
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    This will work (with much much less code):
    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?
    Last edited by melloorr; 12-29-2011 at 07:35 PM.

  • #9
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    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*

  • #10
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    Quote Originally Posted by crank01 View Post
    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.

  • #11
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by melloorr View Post
    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 I just don't know too much about C# yet so any criticism is good criticism

  • #12
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    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

  • #13
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by melloorr View Post
    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 I'm probably wrong tho so please correct me so I don't look like an idiot next time

  • #14
    Regular Coder
    Join Date
    Dec 2011
    Location
    NW England
    Posts
    194
    Thanks
    8
    Thanked 15 Times in 15 Posts
    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

  • #15
    New Coder crank01's Avatar
    Join Date
    Jan 2011
    Posts
    96
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Thanks again =) I'll get crackin on it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •