im trying to do the most basic c# program to show my college class and for some reason a couple of simple if statements are messing up my whole small program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("do you have a pet?");
            Console.WriteLine("Y/N");
            string yn = Console.ReadLine();
            if (yn == "N")
            {
                Console.WriteLine("well.....you should!!");
            }
            if (yn == "Y")
            {
                Console.WriteLine("good!! what kind of pet is he?");
                string type = Console.ReadLine();
                Console.WriteLine("i love " + type + "s!!!");
                Console.WriteLine("how old is he?");
                int chanan = Console.Read();
                if (chanan > 10)
                {
                    Console.WriteLine("you should probably start thinking about a funeral");
                    Console.ReadLine();
                }
                if (chanan < 10)
                {
                    Console.WriteLine("well then you have a nice long time to go with him");
                    Console.WriteLine("i think were done here....goodbye");
                    Console.ReadLine();
                }

            }
            
            Console.WriteLine("press ENTER to exit....");
            Console.ReadLine();
            
        }
    }
}

when it asks how old he is, you input a number and it puts it in the age variable and for some reason ti adds 48 to the variable.......i see absolutely no reason for this especially since there isnt any arithmetic in the whole program

good luck trying to help
binny

Console.Read() is giving you the integer value of the character the user entered.

0-9 in ASCII is 48-57. If the user entered a capital letter, you'd get 65 to 90. Lower case, 97-122.

If you want the true value of what the user entered, especially if you're anticipating an entry more than one character in length, use .ReadLine() instead and convert the input to an integer. Tip: use int.TryParse for the conversion.

Console.Read() is giving you the integer value of the character the user entered.

0-9 in ASCII is 48-57. If the user entered a capital letter, you'd get 65 to 90. Lower case, 97-122.

If you want the true value of what the user entered, use .ReadLine() instead and convert the input to an integer. Tip: use int.TryParse for the conversion.

so what do i need to do to have it an int opposed to a char?

so what do i need to do to have it an int opposed to a char?

Like I said, use Console.ReadLine() and convert the input to an integer (int.TryParse!). You could technically work with .Read() since you now know how it works and you can convert the integer value back to the character and then convert that to a string and then convert that to an integer, but (a) that's too much to do and (b) you only get 1 character out of the whole deal. Either way, you still need to validate the user entered a correct value.

totally agree with apegram. When accepting user input you should always validate it. FOr instance, your first question will output an answer if they enter an uppercase Y or uppercase N. If they enter a lowercase Y/N or a letter other than Y/N it will skip over the rest of your program.
A general rule of thumb with user input is to code for every eventuality.

That is why i strongly advocate the int.TryParse rather than int.Parse when you are handling user input. If a user types a letter and you attempt to int.Parse it your application will throw an exception. int.TryParse will output the value to a variable and returns a boolean value showing wether the parse suceeded or failed.

Just as a side note; there are a couple of minor things in your code that i felt i should mention.
Firstly, since your first two if statements are related its common to use if..else. If you have several If statements in sequence then each one will be processed, regardless of whether the previous ones were a match. If you use if..else then the first matching condition will be processed and all other tests will be skipped. You can also then include an else statement to catch incorrect input.

Also, you are testing if age is greater than 10, or less than 10, but what happens if it is equal to 10?

if (yn.ToUpper() == "N") //ToUpper means that 'n' or 'N' will both match
            {
                Console.WriteLine("well.....you should!!");
            }
            else if (yn.ToUpper() == "Y") //else if means that this test is skipped if the first one succeeds.
            {
                Console.WriteLine("good!! what kind of pet is he?");
                string type = Console.ReadLine();
                Console.WriteLine("i love " + type + "s!!!");
                Console.WriteLine("how old is he?");
                int chanan = -1; // -1 used as a default value. Avoids errors from accessing uninitialised value
                if(int.TryParse(Console.ReadLine(), out chanan))
               {
                //int.TryParse will return true if parse was successful
                //accepts console.readline as input.
                //Readline will allow user to enter more than one digit (essential if you are expecting ages over 10).
                //out indicates variable to send value to if parse is successful

                    if (chanan >= 10) //less than or equal to ensures that a match occurs when the user enters 10
                    {
                        Console.WriteLine("you should probably start thinking about a funeral");
                        Console.ReadLine();
                    }
                    else if (chanan < 10) 
                    {
                        Console.WriteLine("well then you have a nice long time to go with him");
                        Console.WriteLine("i think were done here....goodbye");
                        Console.ReadLine();
                    }
                }
                else
                {
                 // tryParse returns false if parse fails. Alert user to invalid input
                     Console.WriteLine("Input was not a number");
                }
            }
            else //else acts as a catch all. If none of the other conditions are met then this code will be processed.
            {
                Console.WriteLine("Invalid response");
            }

The two things I can see are that you have

int chanan = Console.Read();

and I think it should be

int chanan = Console.ReadLine();

you are also directly trying to convert the entered amount to an int and I would use int.parse(string) or better still use a try and catch to make sure what they enter is numerical?

im trying to do the most basic c# program to show my college class and for some reason a couple of simple if statements are messing up my whole small program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("do you have a pet?");
            Console.WriteLine("Y/N");
            string yn = Console.ReadLine();
            if (yn == "N")
            {
                Console.WriteLine("well.....you should!!");
            }
            if (yn == "Y")
            {
                Console.WriteLine("good!! what kind of pet is he?");
                string type = Console.ReadLine();
                Console.WriteLine("i love " + type + "s!!!");
                Console.WriteLine("how old is he?");
                int chanan = Console.Read();
                if (chanan > 10)
                {
                    Console.WriteLine("you should probably start thinking about a funeral");
                    Console.ReadLine();
                }
                if (chanan < 10)
                {
                    Console.WriteLine("well then you have a nice long time to go with him");
                    Console.WriteLine("i think were done here....goodbye");
                    Console.ReadLine();
                }

            }
            
            Console.WriteLine("press ENTER to exit....");
            Console.ReadLine();
            
        }
    }
}

when it asks how old he is, you input a number and it puts it in the age variable and for some reason ti adds 48 to the variable.......i see absolutely no reason for this especially since there isnt any arithmetic in the whole program

good luck trying to help
binny

The two things I can see are that you have

int chanan = Console.Read();

and I think it should be

int chanan = Console.ReadLine();

you are also directly trying to convert the entered amount to an int and I would use int.parse(string) or better still use a try and catch to make sure what they enter is numerical?

Hi SharpJohnnyG. You may have noticed your post has been downvoted a couple of times. There are a couple of things you might wanna watch out for:
-Try to avoid quoting the whole of the message you are replying to. Trim it down to the relevant parts especially if it is long or contains large code blocks.
-You are right that he should be using Readline, but that has already been stated several times. The code you posted int chanan = Console.ReadLine(); , however, will not work as you are implicitly converting a the string returned by ReadLine to an integer.

I'm not trying to discourage you, its always great to see new solvers on the boards; but please read through the thread to make sure you are contributing something new to the discussion and always double check your code/syntax before posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.