Needing Help Again

Posts 1–5 of 5 · Page 1 of 1
Needing Help Again
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.Title = "Info Whore";
            
            Console.WriteLine("Whats Your Name?");

            string Name = Console.ReadLine();

            Console.WriteLine("Hello {0} how old are you?", Name);

            double age = Console.Read();

            if (age < 18) Console.WriteLine("Hey now you've gotta be 18 to use this!");

            Console.ReadLine();

            if (age > 18) Console.WriteLine("Your Name Is {0} and you are {1} years old", Name, age);

            Console.ReadLine();
        }
    }
}
Here's another one I need help with .-. Whats wrong with it
Its supposed to take your name, output it back and ask you for your age,
if the age is over 18 then it'll say your name is blank and your blank years old
if the age is under 18 then its supposed to say hey now you've gotta be 18
to use this. Any help? Also how do I set age so that if someone inputs letters or charcters
as the age it'll say incorrect input and ask for their age again, and same for the name but with
letters only thanks.
I got this working:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.Title = "Info Whore";
            
            Console.WriteLine("What's Your Name?");

            string Name = Console.ReadLine();
            if(Name.All(char.IsLetter))
            {
                Console.WriteLine("Hello {0}, how old are you?", Name);
                string age = Console.ReadLine();
                int value;
                if (int.TryParse(age, out value))
                {
                    if (value < 18)
                    {
                        Console.WriteLine("Hey now, you've gotta be 18 to use this!");
                        Console.ReadLine();

                    }
                    else
                    {
                        Console.WriteLine("Your name is {0} and you are {1} years old.", Name, age);
                        Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Please write your age in numbers!");
                    Console.ReadLine();
                }
            }
            else
            {
                Console.WriteLine("Please write your name with letters!");
                Console.ReadLine();
            }
        }
    }
}
If you know basic C#, the code is pretty understandable
Now First off I cant stress enough how grateful I am that you've been willing to be so helpful to a noobie like me, Its super frustrating, I know I code in HTML Fluently so when I get noobie questions on other fourms I'm Like, You srs? We gotta go through this again? Lol but anyways, I have revamped my code using corrections/Ideas from your code, Essentially the same just formatted differently xD
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            // Delcare Variables
            string strUserName = "";
            int intUserAge = 0;

            // Program UI Bar
            Console.Title = "Useless Program";

            // Asks User For Input
            // Sets The Name Variable To Read The Input From Above
            Console.WriteLine("Whats Your Name?");
            strUserName = Console.ReadLine();

            // Displays Back Their Name And Asks For Their Age
            // Sets Age Variable To Read The Input
            Console.WriteLine("Hello {0} how old are you?", strUserName);
            intUserAge = Console.Read();

            if (intUserAge <= 18)
            {
                Console.WriteLine("Hey now you've gotta be 18 to use this!");
            }

            else
            {
                Console.WriteLine("Your Name Is {0} and you are {1} years old", strUserName, intUserAge);
            }

            Console.ReadLine();
        }
    }
}
Now that those simple corrections are done, at the very end it has a console.readline but it closes the fucking window and I'ts been an emotional roller-coaster to try to get that to not happen, xD but I just cant get it to keep open so I can read the output because its closing too damn fast I tried to sneak another console.readline inbetween the if and else statements but my compiler told me it was unexpected with 13+ arguments/overloads, whatever the actual FUCK that means.....

- - - Updated - - -

Hey thanks for the source code! I'm really grateful for that, but if you could would you mind going in and putting footnotes on how the different sections/blocks work as well as the parameters of using them? I'm not familiar with half of the commands you used .-. Lol thanks anyways though!
Quote Originally Posted by NastyAttraction View Post
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.Title = "Info Whore";

            Console.WriteLine("Whats Your Name?");

            string Name = Console.ReadLine();

            Console.WriteLine("Hello {0} how old are you?", Name);

            double age = Console.Read();

            if (age < 18) Console.WriteLine("Hey now you've gotta be 18 to use this!");

            Console.ReadLine();

            if (age > 18) Console.WriteLine("Your Name Is {0} and you are {1} years old", Name, age);

            Console.ReadLine();
        }
    }
}



A few things to start off with:

1. You need to use some naming conventions that have more detail. What you have might not be confusing for such a small program, but once you start getting into 400 to 500 lines things can get a little confusing. After about a thousand it can become a little frustrating when you have 20+ integers, four arrays, 10 strings, etc. and you have to try to remember which Name variable is which. Is this one a double or a string? Intellisense helps with this but not every language you ever use will have those features, so you should break this bad habit now.

For example, take a 5000 line program and make a variable called "Name". The name of what? It is a string, right? You might assume it is the name of X when it is actually the name of Y, but you won't get a compiler error because it is still a valid string. This will create very hard to trace bugs in some languages that often aren't apparent until you start testing a specific piece of a program. This will waste tons of time as your projects get bigger as you advance.

Instead, name the variable something like "strUsersNameInput". Break it down: "str" = string, so we know the variable's type at a glance when we see it in code. "UsersName" lets us know that it is the user's name, and "Input" lets us know that this is direct input and not from a file or database (This last one isn't usually necessary, but merely an example). Is it a little long? Sure, but the extra half second it takes for a programmer to type that is nothing compared to the time spent debugging stupid mistakes, especially on complicated projects. It may seem bureaucratic, but when you get to large projects you will realize how much easier it makes things.

Take your double for example: "age". It isn't consistent with your other variable, where the first letter was capitalized. What is this the age of? Age should be a whole number, so this is an integer! Wrong, now you've got stupid errors. Instead, use something like "dblUsersAge".

TL;DR for #1: Naming conventions are an extremely important habit to build, and they should be uniform throughout a program.



2. Declare your variables at one point in the area of its scope, usually at the top. That way, if you ever need to make a change to variables they are all in one spot. This makes changes and maintenance much easier.



3. Using a double for someone's age isn't really necessary. When you ask someone how old they are they give you a whole number, not 21.65. It won't stress this a whole lot because these days pretty much every machine has more than enough memory so this isn't really an issue for 95% of programs, but be aware that it is usually best to pair variable with the smallest/simplest variable type - with future updates of the program in mind. This is a much bigger issue when you start designing databases.



4. Organize your code based on relation (known as Localization), don't just skip one line between everything and call it good. Something small like this is essential in large programs to ensure modifiability and understandability when you have to fix or change something in 6 months later.



5. Always use brackets with conditional statements. Does it work without them when there's only one line to execute? Yes, however that doesn't mean it is good practice. Just because a compiler will let you do it doesn't mean you should and that it is okay to do. This goes against uniformity, which is essential for maintainability and readability. Really easy to add an extra line to an if statement without brackets if you aren't paying attention and create a bug that shouldn't exist.



6. If there's only two possible outcomes and only one can exist, use if statement and then an else statement.


If there's anything I said that you didn't understand/recognize, let me know and I will attempt to clarify it.

This is how I would modify your code (Note: I didn't add any input validation because I don't think you need to be worrying about that just yet. Work on basic skills some more first.):

Code:
using System;
Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variable Declarations
            string strUsersName = "";
            int intUsersAge = 0;

            //Setting Up the UI
            Console.Title = "Info Whore";

            //Gathering User Info/Input
            Console.WriteLine("Whats Your Name?");
            strUsersName = Console.ReadLine();

            Console.WriteLine("Hello {0} how old are you?", strUsersName);
            intUsersAge = Console.Read();

            //Responding to the user's age.
            if (intUsersAge <= 18) 
            {
                     Console.WriteLine("Hey now you've gotta be 18 to use this!");
            }
            else
            {
                     Console.WriteLine("Your Name Is {0} and you are {1} years old", strUsersName, intUsersAge);
            }

            Console.ReadLine();
        }
    }
}
Quote Originally Posted by pcgamers123 View Post
I got this working:
Quote Originally Posted by pcgamers123 View Post
Code:
using System;
Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Info Whore";

            Console.WriteLine("What's Your Name?");

            string Name = Console.ReadLine();
            if(Name.All(char.IsLetter))
            {
                Console.WriteLine("Hello {0}, how old are you?", Name);
                string age = Console.ReadLine();
                int value;
                if (int.TryParse(age, out value))
                {
                    if (value < 18)
                    {
                        Console.WriteLine("Hey now, you've gotta be 18 to use this!");
                        Console.ReadLine();

                    }
                    else
                    {
                        Console.WriteLine("Your name is {0} and you are {1} years old.", Name, age);
                        Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Please write your age in numbers!");
                    Console.ReadLine();
                }
            }
            else
            {
                Console.WriteLine("Please write your name with letters!");
                Console.ReadLine();
            }
        }
    }
}
If you know basic C#, the code is pretty understandable

You could benefit from much of the advice above as well. Also, your input validation is rather clunky and lacks readability. Additionally, it doesn't specify to the user what was wrong when they get an error. Alway assume your users need everything spelled out for them, because many do. You also didn't explain to somebody who is clearly learning the language exactly what you're doing. Take a spanish translation forum for example, to someone who is new to spanish. You wouldn't just spout out a paragraph like a native and then say in english "If you know spanish this is simple". Well no shit it is simple to you, you already know the language they are trying to learn! You need to tell people what you are doing, and why. This is how you should comment your code as well.

Input validation isn't even a language specific concept, but you're telling him that it is "basic C#". No beginners course starts straight off including input validation. You should stop trying to help people until you learn how to actually explain what was wrong, what you changed, and why you changed it. Also, the way you wrote that... it actually lacking understandability/readability.
Quote Originally Posted by Disturbed View Post


A few things to start off with:

1. You need to use some naming conventions that have more detail. What you have might not be confusing for such a small program, but once you start getting into 400 to 500 lines things can get a little confusing. After about a thousand it can become a little frustrating when you have 20+ integers, four arrays, 10 strings, etc. and you have to try to remember which Name variable is which. Is this one a double or a string? Intellisense helps with this but not every language you ever use will have those features, so you should break this bad habit now.

For example, take a 5000 line program and make a variable called "Name". The name of what? It is a string, right? You might assume it is the name of X when it is actually the name of Y, but you won't get a compiler error because it is still a valid string. This will create very hard to trace bugs in some languages that often aren't apparent until you start testing a specific piece of a program. This will waste tons of time as your projects get bigger as you advance.

Instead, name the variable something like "strUsersNameInput". Break it down: "str" = string, so we know the variable's type at a glance when we see it in code. "UsersName" lets us know that it is the user's name, and "Input" lets us know that this is direct input and not from a file or database (This last one isn't usually necessary, but merely an example). Is it a little long? Sure, but the extra half second it takes for a programmer to type that is nothing compared to the time spent debugging stupid mistakes, especially on complicated projects. It may seem bureaucratic, but when you get to large projects you will realize how much easier it makes things.

Take your double for example: "age". It isn't consistent with your other variable, where the first letter was capitalized. What is this the age of? Age should be a whole number, so this is an integer! Wrong, now you've got stupid errors. Instead, use something like "dblUsersAge".

TL;DR for #1: Naming conventions are an extremely important habit to build, and they should be uniform throughout a program.



2. Declare your variables at one point in the area of its scope, usually at the top. That way, if you ever need to make a change to variables they are all in one spot. This makes changes and maintenance much easier.



3. Using a double for someone's age isn't really necessary. When you ask someone how old they are they give you a whole number, not 21.65. It won't stress this a whole lot because these days pretty much every machine has more than enough memory so this isn't really an issue for 95% of programs, but be aware that it is usually best to pair variable with the smallest/simplest variable type - with future updates of the program in mind. This is a much bigger issue when you start designing databases.



4. Organize your code based on relation (known as Localization), don't just skip one line between everything and call it good. Something small like this is essential in large programs to ensure modifiability and understandability when you have to fix or change something in 6 months later.



5. Always use brackets with conditional statements. Does it work without them when there's only one line to execute? Yes, however that doesn't mean it is good practice. Just because a compiler will let you do it doesn't mean you should and that it is okay to do. This goes against uniformity, which is essential for maintainability and readability. Really easy to add an extra line to an if statement without brackets if you aren't paying attention and create a bug that shouldn't exist.



6. If there's only two possible outcomes and only one can exist, use if statement and then an else statement.


If there's anything I said that you didn't understand/recognize, let me know and I will attempt to clarify it.

This is how I would modify your code (Note: I didn't add any input validation because I don't think you need to be worrying about that just yet. Work on basic skills some more first.):

Code:
using System;
Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gathering_Data
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variable Declarations
            string strUsersName = "";
            int intUsersAge = 0;

            //Setting Up the UI
            Console.Title = "Info Whore";

            //Gathering User Info/Input
            Console.WriteLine("Whats Your Name?");
            strUsersName = Console.ReadLine();

            Console.WriteLine("Hello {0} how old are you?", strUsersName);
            intUsersAge = Console.Read();

            //Responding to the user's age.
            if (intUsersAge <= 18) 
            {
                     Console.WriteLine("Hey now you've gotta be 18 to use this!");
            }
            else
            {
                     Console.WriteLine("Your Name Is {0} and you are {1} years old", strUsersName, intUsersAge);
            }

            Console.ReadLine();
        }
    }
}



You could benefit from much of the advice above as well. Also, your input validation is rather clunky and lacks readability. Additionally, it doesn't specify to the user what was wrong when they get an error. Alway assume your users need everything spelled out for them, because many do. You also didn't explain to somebody who is clearly learning the language exactly what you're doing. Take a spanish translation forum for example, to someone who is new to spanish. You wouldn't just spout out a paragraph like a native and then say in english "If you know spanish this is simple". Well no shit it is simple to you, you already know the language they are trying to learn! You need to tell people what you are doing, and why. This is how you should comment your code as well.

Input validation isn't even a language specific concept, but you're telling him that it is "basic C#". No beginners course starts straight off including input validation. You should stop trying to help people until you learn how to actually explain what was wrong, what you changed, and why you changed it. Also, the way you wrote that... it actually lacking understandability/readability.
Ok, I'll remember your advice the next time I post. First post since I made the account, so I don't have the habit (yet) of always explaining what the code does and how someone can make it themselves.
The codes is somewhat ugly, I'll agree with you on that, but it does the job.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us