0

I'm new in the community and I'm learning C#. I try to write a program and faced with the problem below. I tried to find the answer in Google and here but no luck yet. When I choice "Y" I'm getting the error.

I attached the code and screenshot, please help if you can, thank you!


using System;

namespace YourAge
{
    internal class Age
    {
        public static void Main()
        {
            DateTime newDataTime = DateTime.Now;
            Console.WriteLine("So, today is " + "{0}", newDataTime);

            Console.Write("Do you smoke a cigarettes? Y/N: ");
            char Y = (char)Console.Read();

            if (Char.IsUpper(Y))
            {
                Console.Write("How many cigarettes do you smoke in the day?: ");
                int cigTotal = Convert.ToInt16(Console.ReadLine());

                //cost of one cigarettes
                float costOneCig = 0.3F;

                float sumTotal = cigTotal * costOneCig;
                Console.WriteLine("You are losing every day:{0:C2}", sumTotal);
            }
            else
                //coming soon

                Console.ReadKey();
        }
    }
}

This is the exception thrown:

Exception Thrown

8
  • What did you enter in ... smoke in the day? question's answer? Commented Jun 22, 2016 at 19:35
  • What kind of error (exception thrown, incorrect output, other) do you get? Commented Jun 22, 2016 at 19:35
  • It also looks like you are just checking if the answer to Do you smoke a cigarettes? is an upper case character and not whether or not it is a Y or N Commented Jun 22, 2016 at 19:37
  • Shaharyar thanks, look question Console.Write("Do you smoke a cigarettes? Y/N: "); -> if Y (Yes) ->next question Console.Write("How many cigarettes do you smoke in the day?: "); int cigTotal = Convert.ToInt16(Y); it means how many a person smokes cigarettes a day. Commented Jun 22, 2016 at 19:44
  • Dmitry Bychenko: System.FormatException was unhandled HResult=-2146233033 Message=Input string was not in a correct format. Source=mscorlib Commented Jun 22, 2016 at 19:46

3 Answers 3

1

The problem is that you are using Console.Read() instead of Console.ReadLine().

Console.Read() only reads the next character from standard input. Console.ReadLine(), on the other hand, reads the entire line of characters from the standard input stream and then moves to the next newline.

When you press 'Y' and then enter, when you get up to the next Console input, Convert.ToInt16(Console.ReadLine(), the Console is still up to the previous input line.

Possible solutions:

  1. Change (char)Console.Read() to Covert.ToChar(Console.ReadLine()). ReadLine takes in an entire string, not a char, so you need to use Convert.ToChar instead of the simple (char) cast to convert the first character of the string into a char.
  2. Add a blank Console.ReadLine() after (char)Console.Read() to tell the Console to flush to the next line.
  3. Input your character together with the next number like "Y2" (although I highly doubt this is what you want to do).
Sign up to request clarification or add additional context in comments.

4 Comments

It's funny how the chosen answer has nothing to do with solving the actual exception yet for the OP it miraculously solved all the problems. :)
I still solve problems as they come, I tried your option but it did not help to solve the exception. I have very little experience, so from the outside, it looks funny. Any case - thank you.
@pijemcolu You are correct. I saw that my solution worked and wrote the first thing that looked like that reason. Spent another moment looking at the code and update my explanation.
@Tot Zam I have used the first option. (For the record).
0
string userInput = Console.ReadLine();
int numberOfCigarettes = Convert.ToInt16(userInput);

This might make it more visible for you what is the problem.

Console.ReadLine() returns a string which you later need to convert into an integer. If your userInput string is not a number, then conversion is impossible and an exception is thrown.

On the other hand your if statement is incorrect as well. Right now you are only checking the variable Y whether it is uppercase not whether it holds a literal character 'y'.

You could for example make sure that the variable Y is always uppercase like this:

if(Y.ToUpper().Equals('Y'))

Comments

0

You may want to try something like this so you can diagnose the problem. Please make sure to mark an answer if it is correct.

    Console.Write("Do you smoke a cigarettes? Y/N: ");
    string answer = Console.ReadLine();
    while (answer.Length != 1) {
        Console.WriteLine("Character is one letter silly.");
        Console.Write("Do you smoke a cigarettes? Y/N: ");
        answer = Console.ReadLine(); }

    char response = answer[0];

    if (response == 'Y' || response == 'y')
    {
        //YES RESPONSE
    }
    else
    {
        //NO RESPONSE
    }

    Console.ReadLine();

This code will let you know if you input anything else other than one character. Good luck with C#!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.