Thenoblest
Thenoblest

Reputation: 3

C#. Input string was not in a correct format

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

Upvotes: 0

Views: 4753

Answers (3)

Tot Zam
Tot Zam

Reputation: 8746

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).

Upvotes: 1

Alex
Alex

Reputation: 422

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#!

Upvotes: 0

pijemcolu
pijemcolu

Reputation: 2605

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'))

Upvotes: 0

Related Questions