0

Okay, so here is what I have, the console runs and accepts any number for the first input and outputs all the outputs but it skips the rest of the reads after the first one. I am new to C# and want to learn so I definetly feel like I am just missing something stupid here. Do not worry about the purpose as I only want help with this problem I am facing now.

Thanks guys,

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

namespace Stamps
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, and welcome to the U.S. Mail Stamp Printer. Now, to print the correct stamp for your postage, please enter the number of half ounces (so 1= .5 ounce, 2 = 1 ounce) your package will weigh \n");
            double i = Console.Read();

            Console.WriteLine("Thank you, now, will you be sending to zone 1, 2, or 3? Simply enter the zone number and press enter\n");
            double z = Console.Read();

            Console.WriteLine("Great! Now, last question before I print your stamp, will you be using Local Mail or Air Mail, use the number 1 for local and the number 2 for air.\n");
            double m = Console.Read();


            if (m == 2)
            {
                double m2 = .95;


            }
            else
            {
                double m2 = .49;
            }


            if( i == 1)
            {
               double i2 = 1;
            }



            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
            }
            }
            }
Glitch501
  • 1
  • 2

2 Answers2

3

The Console.Read method doesn't parse input, it only reads one character from the input stream.

Use the Console.ReadLine method and parse the string that you get:

double i = Double.Parse(Console.ReadLine());

or:

int z = Int32.Parse(Console.ReadLine());

If you want to handle invalid input you would use the TryParse methods, they will return a boolean that tell you if the parsing worked or not.


Side note: When you try to use the m2 and i2 variables you will notice that it doesn't exist. The variable is local for the code blocks in the if statement, so you need to declare it outside to be able to use it later:

double m2;
if (m == 2)
{
  m2 = .95;
}
else
{
  m2 = .49;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

you can flush you console buffer or you can used console.readline().

For more information refer below post which give yours question answer.

http://stackoverflow.com/questions/7302544/flushing-system-console-read
chirag
  • 1,818
  • 1
  • 15
  • 36