Just learning

Pgill

Member
Joined
Nov 26, 2019
Messages
6
Programming Experience
Beginner
Ok I need some help I am doing a program from Learn C# in one day. Well of course it is taking longer than one day. LOL Can someone please look this over and let me know where the error is I would really appreciate it


C#:
namespace Chapter_6_A
{
    class Program
    {
        int userAge;

        Console.Write("Please enter your age: ");
        userAge = Convert.ToInt32(Console.ReadLine());
          
        if (userAge< 0 || userAge> 100);
        {
            Console.WriteLine("Invalid Age");
            Console.WriteLine("Age must be between 0 and 100);
        }
        else if (userAge < 18)
            Console.WriteLine("Sorry you are underage");
        else if (userAge< 21)
            Console.WriteLine("You need Parental consent");
        else
        {
            Console.WriteLine("Congratulations !");
            Console.WriteLine("You may sign up for the event!);)
        }
    }
 
Last edited by a moderator:

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
There is a link In my signature, for getting started in C#. I suggest you drop that book because its clearly not giving you much help in creating the fundamental structure to begin writing your application. You're missing closing brackets and they're also not in the right places. I would be very weary of a book that encourages someone to learn a language in one day. There is just to much involving the basics to cover in just one day. Clearly the book is titled for marketing purposes only.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Where is your
C#:
        private static void Main(string[] args)
        {

        }
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
C#:
using System;

namespace Chapter_6_A
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int userAge;

            Console.Write("Please enter your age: ");
            userAge = Convert.ToInt32(Console.ReadLine());

            if (userAge < 0 || userAge > 100)
            {
                Console.WriteLine("Invalid Age");
                Console.WriteLine("Age must be between 0 and 100");
            }
            else if (userAge < 18)
                Console.WriteLine("Sorry you are underage");
            else if (userAge < 21)
                Console.WriteLine("You need Parental consent");
            else
            {
                Console.WriteLine("Congratulations !");
                Console.WriteLine("You may sign up for the event");
            }
        }

    }
}
Your old Code :
C#:
namespace Chapter_6_A
{
    internal class Program
    {
        private int userAge;

        Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());

if (userAge< 0 || userAge> 100);
{
Console.WriteLine("Invalid Age");
Console.WriteLine("Age must be between 0 and 100);
}

else if (userAge< 18)
Console.WriteLine("Sorry you are underage");
else if (userAge< 21)
Console.WriteLine("You need Parental consent");
else
{
Console.WriteLine("Congratulations !");
Console.WriteLine("You may sign up for the event!);)
}
}
Compare them.

If statements don't end with a ; after : if (foo == someotherfoo) ;
You have no using directive.
Strings open with a " and close with one of the same "
For each opening bracket ( there is a closing bracket ).
You can't dump if statements directly into a class. They belong in methods. Like : private static void Main(string[] args)
Keep practising but get yourself a new source for study material and try not to skip steps.
 

Pgill

Member
Joined
Nov 26, 2019
Messages
6
Programming Experience
Beginner
THis is how the whole exercise is written


C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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


            int userAge;

        Console.Write("Please enter your age: ");
            userAge = Convert.ToInt32(Console.ReadLine());
          
            if (userAge< 0 || userAge> 100);
        {
            Console.WriteLine("Invalid Age");
            Console.WriteLine("Age must be between 0 and 100);
      
            else if (userAge< 18);
                Console.WriteLine("Sorry you are underage");
            else if (userAge< 21);
                Console.WriteLine("You need Parental consent");
            else
        {
                Console.WriteLine("Congratulations !");
                Console.WriteLine("You may sign up for the event!);
        }

}
 
Last edited by a moderator:

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
THis is how the whole exercise is written
And? See my reply on post 4.

Please post your code in code tags. [CODE=csharp] Your code here [/CODE]
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
If that's the material you are using to learn to code, can you give it to me so I can wipe my ass with it?

That code you posted is missing ", has ; in the wrong places. Perhaps this is part of your exercise; to workout what is wrong with the code. If so, then me giving you the answers isn't worth you doing the assignment; is it?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,506
Location
Sydney, Australia
Programming Experience
10+
I have to agree with Sheepings here. If the point of the exercise is for you to find and fix the mistakes in the code then our doing it for you completely defeats the purpose and if it's your code then you probably need to review what you've learned previously and the basic rules of code syntax. If you were to tell us where and what you think the problems are then we can work with that, but we're not here to just do your work for you or help you cheat. For a start, you can just put the code into VS and the compiler will start flagging the issues for you. It may not tell you exactly how to fix them and it may require you to fix one before it can detect another but, at the very least, that will show you where to start looking.
 

Pgill

Member
Joined
Nov 26, 2019
Messages
6
Programming Experience
Beginner
C#:
using System;

namespace Chapter_6_A
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int userAge;

            Console.Write("Please enter your age: ");
            userAge = Convert.ToInt32(Console.ReadLine());

            if (userAge < 0 || userAge > 100)
            {
                Console.WriteLine("Invalid Age");
                Console.WriteLine("Age must be between 0 and 100");
            }
            else if (userAge < 18)
                Console.WriteLine("Sorry you are underage");
            else if (userAge < 21)
                Console.WriteLine("You need Parental consent");
            else
            {
                Console.WriteLine("Congratulations !");
                Console.WriteLine("You may sign up for the event");
            }
        }

    }
}
Your old Code :
C#:
namespace Chapter_6_A
{
    internal class Program
    {
        private int userAge;

        Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());

if (userAge< 0 || userAge> 100);
{
Console.WriteLine("Invalid Age");
Console.WriteLine("Age must be between 0 and 100);
}

else if (userAge< 18)
Console.WriteLine("Sorry you are underage");
else if (userAge< 21)
Console.WriteLine("You need Parental consent");
else
{
Console.WriteLine("Congratulations !");
Console.WriteLine("You may sign up for the event!);)
}
}
Compare them.

If statements don't end with a ; after : if (foo == someotherfoo) ;
You have no using directive.
Strings open with a " and close with one of the same "
For each opening bracket ( there is a closing bracket ).
You can't dump if statements directly into a class. They belong in methods. Like : private static void Main(string[] args)
Keep practising but get yourself a new source for study material and try not to skip steps.

Thanks for the help I was able to get it to be clear of errors but dont have time to run it right now but thank you very much.
Just starting to learn it is something I always wanted to learn
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
644
Location
Virginia Beach, VA
Programming Experience
10+
Can you tell you which "Learn C# in a Day" book you are using:

C#: Learn C# in One Day and Learn It Well. C# for Beginners with Hands-on Project.

Learn C# in 1 Day: Complete C# Guide with Examples

If it's the first one, then the few 1 star and 2 star reviews hint at why the OP is in their current situation. This is the most helpful 2-star review:
10/7/16 Update: Gotta drop the rating to two stars. Just spend a few minutes stuck because the author didn't mention exactly where to put new code (specifically page 75 where she talks about adding a method). To someone so new to the language, this kind of small omission is immensely frustrating, as the last thing I should have to do is figure out where code should go. I had to download the files from the website in order to get a visual overview of where the method should go. I'll leave this at two for price alone, but as the book starts delving into more and more complex topics, I'm spending more time getting hung up on author oversights than I should ever have to in a book designed for beginners.
Amazingly, though, the book is an Amazon best seller with over 84% giving it 5 and 4 stars. I tend to agree with the 1-star reviews from what I've seen on Amazon's "Look Inside"
 

Pgill

Member
Joined
Nov 26, 2019
Messages
6
Programming Experience
Beginner
Can you tell you which "Learn C# in a Day" book you are using:

C#: Learn C# in One Day and Learn It Well. C# for Beginners with Hands-on Project.

Learn C# in 1 Day: Complete C# Guide with Examples

If it's the first one, then the few 1 star and 2 star reviews hint at why the OP is in their current situation. This is the most helpful 2-star review:

Amazingly, though, the book is an Amazon best seller with over 84% giving it 5 and 4 stars. I tend to agree with the 1-star reviews from what I've seen on Amazon's "Look Inside"

It is the first one you have listed that I am using right now.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
the author didn't mention exactly where to put new code (specifically page 75 where she talks about adding a method).
I think we found why there was no method above. Obviously the author thought it would keep the reader occupied with the book a little longer. By adding this as something to troubleshoot without actually explaining what the user would be troubleshooting, is a terrible addition to add to a book for new comers to the C# language in my opinion, whether it be for an assignment or not. I wouldn't advocate dropping this on a beginners lap. That chapter would have been best wrote to teach the reader how not to write code, and how best to write good code rather than playing guessing games in hope the reader could work it out themselves. I bet half of the five star reviews are probably fake, from fake accounts.
If you were to tell us where and what you think the problems are then we can work with that, but we're not here to just do your work for you or help you cheat.
While I stand by this. I will explain why I provided an exception on post 4. Firstly, I have a rule I try not to break; where it involves doing an assignment for another poster. If we do every assignment that's flogged to the forum, we would essentially be responsible for creating uneducated programmers. The principle of an assignment is that the assigner believes they have elevated you the assignee to a level in which they believe you can complete whichever assignment they provide to you.

I have always been an advocate for encouraged learning, and forcing OPS to educate themselves by making them read relevant documentation that led to their own coding horrors. Rather than explaining a solution to them first; because If they don't understand how they created their error, then there is no point explaining a solution to a problem they don't understand how they managed to create. In this case; I don't think the Author of the book was right by dumping a puzzle of broken code as a practical way to teach a newcomer how to read or write a code-based language. It would have been more practical for all readers, if the Author had provided information on what the symbols in the language actually do and explained where they belong and why. Basically a 101 on C# syntax would have been more practical from a teaching prospective.

I would recommend you bin that book and start with the link In my signature or follow the one in John's signature for tutorials on getting started with C#.

Edit, corrected some grammar.
 
Last edited:

Pgill

Member
Joined
Nov 26, 2019
Messages
6
Programming Experience
Beginner
C#:
using System;

namespace Chapter_6_A
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            int userAge;

            Console.Write("Please enter your age: ");
            userAge = Convert.ToInt32(Console.ReadLine());

            if (userAge < 0 || userAge > 100)
            {
                Console.WriteLine("Invalid Age");
                Console.WriteLine("Age must be between 0 and 100");
            }
            else if (userAge < 18)
                Console.WriteLine("Sorry you are underage");
            else if (userAge < 21)
                Console.WriteLine("You need Parental consent");
            else
            {
                Console.WriteLine("Congratulations !");
                Console.WriteLine("You may sign up for the event");
            }
        }

    }
}
Your old Code :
C#:
namespace Chapter_6_A
{
    internal class Program
    {
        private int userAge;

        Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());

if (userAge< 0 || userAge> 100);
{
Console.WriteLine("Invalid Age");
Console.WriteLine("Age must be between 0 and 100);
}

else if (userAge< 18)
Console.WriteLine("Sorry you are underage");
else if (userAge< 21)
Console.WriteLine("You need Parental consent");
else
{
Console.WriteLine("Congratulations !");
Console.WriteLine("You may sign up for the event!);)
}
}
Compare them.

If statements don't end with a ; after : if (foo == someotherfoo) ;
You have no using directive.
Strings open with a " and close with one of the same "
For each opening bracket ( there is a closing bracket ).
You can't dump if statements directly into a class. They belong in methods. Like : private static void Main(string[] args)
Keep practising but get yourself a new source for study material and try not to skip steps.

Ok I have all the code written and I run the program. It states "PLease enter your age: " you enter an age and hit enter and the program closes. I have looked through the information that I have and you provided an no where see a mistake.

private static void Main(string [] args)
{
int userAge;

Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());

if (userAge < 0 || userAge > 100)
{
Console.WriteLine("Invalid Age");
Console.WriteLine("Age must be between 0 and 100");


Please look this over and let me know if you see a mistake or not.
From what I think there should be a statement under the userAge = Convert line ?
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
652
Location
UK
Programming Experience
10+
Please do not color your text like that. It is an eyesore. This is also the second time I've asked you to post your code in code tags :
Please post your code in code tags. [CODE=csharp] Your code here [/CODE]
Try going to tools > options > debugging and un-tick Automatically close console when debugger completes. Or something to that affect.

Or place a break point on your Main Method on line 7 and step through the code to see what else might be wrong.
 
Last edited:
Top Bottom