48

I want to get a number from the user, and then multiply that number with Pi. my attempt at this is below. But a contains gibberish. For example, if I insert 22, then a contains 50. What am I doing wrong? I don't get any compiler errors.

double a,b;
a = Console.Read();
b = a * Math.PI;
Console.WriteLine(b);
TylerH
  • 20,799
  • 66
  • 75
  • 101
MaxCoder88
  • 2,374
  • 6
  • 41
  • 59
  • Why couldn't you? Is it because of a compiler error, or does it not work as expected when you run it? Even though I can already see at least one thing wrong with it, on Stack Overflow you have to tell us exactly what you expected to happen and what actually happened instead when writing your question. – In silico Sep 02 '11 at 07:53
  • 2
    Read the error message -- it *won't compile* due to a type-mismatch in the assignment of the result of `Console.Read()` to `a`. Then, if still having problems, include the error message in the post. –  Sep 02 '11 at 07:53
  • @pst Considering that Console.Read() returns an int, no type mismatch! – xanatos Sep 02 '11 at 07:56
  • 5
    One of the best skills you can learn is how to ask a good question. In this case - what went wrong? Was there an error message? What was it? What did you try to fix it? What don't you understand specifically? – Kieren Johnstone Sep 02 '11 at 07:57
  • Closely related post [here](https://stackoverflow.com/q/24443827/465053). – RBT Dec 22 '17 at 06:54

9 Answers9

73

I'm not sure what your problem is (since you haven't told us), but I'm guessing at

a = Console.Read();

This will only read one character from your Console.

You can change your program to this. To make it more robust, accept more than 1 char input, and validate that the input is actually a number:

double a, b;
Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
if (double.TryParse(Console.ReadLine(), out a)) {
  b = a * Math.PI;
  Console.WriteLine("Sonuç " + b); 
} else {
  //user gave an illegal input. Handle it here.
}
Mwiza
  • 7,780
  • 3
  • 46
  • 42
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
19
a = double.Parse(Console.ReadLine());

Beware that if the user enters something that cannot be parsed to a double, an exception will be thrown.

Edit:

To expand on my answer, the reason it's not working for you is that you are getting an input from the user in string format, and trying to put it directly into a double. You can't do that. You have to extract the double value from the string first.

If you'd like to perform some sort of error checking, simply do this:

if ( double.TryParse(Console.ReadLine(), out a) ) {
  Console.Writeline("Sonuç "+ a * Math.PI;); 
}
else {
  Console.WriteLine("Invalid number entered. Please enter number in format: #.#");
}

Thanks to Öyvind and abatischev for helping me refine my answer.

Andreas Eriksson
  • 8,979
  • 8
  • 47
  • 65
  • What for try-catch if you can use `TryParse()`? – abatishchev Sep 02 '11 at 07:59
  • 1
    `TryParse()` is faster. See [SO](http://stackoverflow.com/questions/467613/parse-v-tryparse), [SO](http://stackoverflow.com/questions/4945763/what-is-better-int-tryparse-or-try-int-parse-catch), [CH](http://www.codinghorror.com/blog/2005/08/tryparse-and-the-exception-tax.html), – abatishchev Sep 02 '11 at 08:05
  • I have to agree with @abatishchev here. I like the TryParse route much better. I don't think the user writing an illegal input to be an exception that should be catched. Basically I don't think exceptions should be a part of normal program flow. – Øyvind Bråthen Sep 02 '11 at 08:06
  • Thanks for pointing that out - I'll be changing my answer to reflect it. – Andreas Eriksson Sep 02 '11 at 08:06
6
string input = Console.ReadLine();
double d;
if (!Double.TryParse(input, out d))
    Console.WriteLine("Wrong input");
double r = d * Math.Pi;
Console.WriteLine(r);

The main reason of different input/output you're facing is that Console.Read() returns char code, not a number you typed! Learn how to use MSDN.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
2
string str = Console.ReadLine(); //Reads a character from console
double a = double.Parse(str); //Converts str into the type double
double b = a * Math.PI; // Multiplies by PI
Console.WriteLine("{0}", b); // Writes the number to console

Console.Read() reads a string from console A SINGLE CHARACTER AT A TIME (but waits for an enter before going on. You normally use it in a while cycle). So if you write 25 + Enter, it will return the unicode value of 2 that is 50. If you redo a second Console.Read() it will return immediately with 53 (the unicode value of 5). A third and a fourth Console.Read() will return the end of line/carriage characters. A fifth will wait for new input.

Console.ReadLine() reads a string (so then you need to change the string to a double)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • @abatishchev Because it should be `"Sonuç {0}"` (as it was in the first "version" of the question), but I was too much lazy to write it. I normally try to control the formatting of WriteLine instead of simply `WriteLine(myDouble)` – xanatos Sep 02 '11 at 08:02
2

I think there are some compiler errors.

  • Writeline should be WriteLine (capital 'L')
  • missing semicolon at the end of a line

        double a, b;
        Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
        a = double.Parse(Console.ReadLine());
        b = a * Math.PI; // Missing colon!
        Console.WriteLine("Sonuç " + b);
    
philipproplesch
  • 2,127
  • 17
  • 20
1

Sometime in the future .NET4.6

//for Double
double inputValues = double.Parse(Console.ReadLine());

//for Int
int inputValues = int.Parse(Console.ReadLine());
Krishneil
  • 1,432
  • 1
  • 18
  • 26
0

Console.Read() takes a character and returns the ascii value of that character.So if you want to take the symbol that was entered by the user instead of its ascii value (ex:if input is 5 then symbol = 5, ascii value is 53), you have to parse it using int.parse() but it raises a compilation error because the return value of Console.Read() is already int type. So you can get the work done by using Console.ReadLine() instead of Console.Read() as follows.

int userInput = int.parse(Console.ReadLine());

here, the output of the Console.ReadLine() would be a string containing a number such as "53".By passing it to the int.Parse() we can convert it to int type.

NANDU KISHORE
  • 11
  • 1
  • 3
0
        double a,b;
        Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
        try
        {
            a = Convert.ToDouble(Console.ReadLine());
            b = a * Math.PI;
            Console.WriteLine("Sonuç " + b); 
        }
        catch (Exception)
        {
            Console.WriteLine("dönüştürme hatası");
            throw;
        }
Mustafa Güven
  • 15,526
  • 11
  • 63
  • 83
-2

You're missing a semicolon: double b = a * Math.PI;

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144