Click here to Skip to main content
15,908,115 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;

namespace AgeGuesser
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Input your age please: ");
            int input = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Is this right?(Y/N): " + input);
            var test = Console.ReadKey();
            bool yes = Convert.ToBoolean(ConsoleKey.Y);
            bool no = Convert.ToBoolean(ConsoleKey.N);
            Convert.ToBoolean(test);
            if (test = yes) //This doesn't work because it won't convert bool to consolekeyinfo
            {
                
            }
        }
    }
}


What I have tried:

I don't really remember what I've tried but all I know is that it didn't work, but I do know it was just slight variations of the problem code. Sorry.
Posted
Updated 26-Jan-20 7:51am
v4

1. why not test to see if the user inputs a number ... defensive programming ! see the use of In32.TryParse in this example.

2. why not limit the number of tries the user can have to avoid endless looping ?
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int agevalue;

            int ntries = 2;
            int tries = 1;

            while (true)
            {
                Console.WriteLine($"try: #{tries} | Input your age please: ");

                var age = Console.ReadLine();

                if (Int32.TryParse(age, out agevalue))
                {
                    // add range checking here ?

                    Console.WriteLine("Is this right?(Y/N): " + agevalue);

                    if (Console.ReadKey().Key == ConsoleKey.Y) break;

                    Console.Clear();
                }

                if (++tries > ntries) break;
            }
        }
    }
}
Another useful thing to add would be checking to see if the age entered is within a certain range that makes sense in the context of your app.
 
Share this answer
 
Comments
Tenet1001 26-Jan-20 15:29pm    
"if (Int32.TryParse(age, out agevalue))"
What are you checking to see here? Are you checking to see if the TryParse works?
Also:
What does the while (true) do at the top?
BillWoodruff 26-Jan-20 16:11pm    
The best way for you to understand this is to create a Console app using this code, set breakpoints on each line of it, run it, and, as you hit each breakpoint, inspect the current values. Second, look up what the 'while operator, and 'TryParse method do. Figure out what would happen if there were no 'break statement in this code.

The only way for you to learn is to take initiative, to study working code, to experiment with writing your own code, to read the documentation, and to learn how to use debugging for code behavior examination, as well as for dealing with bugs.

I am completely confident that you can, and will, develop as a programmer by using these methods ... after a time, they will become familiar ... as familiar to you as using a screwdriver, or a hammer :)

cheers, Bill
if (test = yes) \\This doesn't work because it won't convert bool to

Easy mistake; it's "==" for "comparing"; "=" only for assignment.

("!=" means "not equal to"; ">="; etc.)
 
Share this answer
 
v3
You are making it complicated for no reason. You dont have to use bool there.

using System;

namespace AgeGuesser
{
    class Program
    {
        static void Main(string[] args)
        {
            string yes = "Y";
            Console.WriteLine("Input your age please: ");
            int input = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Is this right?(Y/N): " + input);
            string input2 = Console.ReadLine();
            if (yes == input2)
            {
                Console.WriteLine("Mgergerge");
            }
            else
            {
                Console.WriteLine("eqwrq");
            }
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
Comments
Tenet1001 25-Jan-20 20:21pm    
I'm trying to make it so when you hit the key "Y" it counts as yes but if you hit the key "N" it does a callback or whatever command makes it go back to the start. This is just a project so I can figure concepts out. I'm doing this project to understand bool and ConsoleKeyInfo (I think).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900