0

I'm trying to check if the user's response is a double or an int, but the int is specific, whereas the double is not, as I probably made a right mess of explaining it, here's the code:

Console.WriteLine("\n 2) Q: How old is Sally? \n");
int nSallyAge = Convert.ToInt32(Console.ReadLine());
double dSallyAge = Convert.ToDouble((nSallyAge));

if (nSallyAge == 62 || dSallyAge == 62.0)
{
    // Increase Score
    sUser1Score++;
    Console.WriteLine("\n A: Correct, Sally's age is 62, you have been awarded 1 point. \n");
    Console.ReadLine();
} 

What I'm trying to do, is instead of dSallyAge HAS to equal 62.0, it just has to equal any double figure.

Nic
  • 12,220
  • 20
  • 77
  • 105
Trumpkin12
  • 19
  • 1
  • 1
  • Can you explain further what you're trying to accomplish with the above code? You're not trying to **double** Sally's actual age are you? Because that would be bad news for Sally. – bokibeg Mar 16 '15 at 21:58
  • Check out http://stackoverflow.com/questions/11634079/how-can-i-get-the-data-type-of-a-variable-in-c – LinkBerest Mar 16 '15 at 21:58
  • 1
    If dSallyAge has to equal ANY double, why you place in `if` statement in the first place? In your code above dSallyAge has no meaning... – Bruniasty Mar 16 '15 at 21:59
  • Haha, Nah, I'm trying to get the user's response to be either a specific int (62), or the user to respond with a double that is any number.( eg, 62.7) – Trumpkin12 Mar 16 '15 at 22:00
  • 2
    If inputting "62.3" counts as 62, you parse the user's input as a double, then floor it to an integer. – Warty Mar 16 '15 at 22:01
  • This doesn't address your question, but I'd suggest you avoid using [Hungarian Notation](http://en.wikipedia.org/wiki/Hungarian_notation), e.g. [this SO link](http://stackoverflow.com/q/768255/1364007). In other words, use `sallyAge` instead of `dSallyAge` since you can tell from your IDE what the type of `sallyAge` is. – Wai Ha Lee Mar 16 '15 at 22:03
  • Your question makes no sense. The input is read directly into an integer with `nt nSallyAge = Convert.ToInt32(Console.ReadLine());`, so it can never be *62.7* or anything else, as that would raise an exception. You've read it as an integer, so *use it as an integer*. – Ken White Mar 16 '15 at 22:11
  • I think I understand your question, and provided a possible solution below. What I think you're looking for is `Math.Truncate(double)`. – Rufus L Mar 17 '15 at 00:00

3 Answers3

1

I would approach this problem by first creating a method that gets a double from the user (that will, of course, also accept an int). This removes error handling from your main code.

NOTE in the code below, Math.Truncate can be replaced by Math.Floor, with the same result:

private static double GetDoubleFromUser(string prompt)
{
    double input;

    while (true)
    {
        if (prompt != null) Console.Write(prompt);
        if (double.TryParse(Console.ReadLine(), out input)) break;
        Console.WriteLine("Sorry, that is not a valid number. Please try again.");
    }

    return input;
}

Then, in my main code, I would get the number from the user, and use the Math.Truncate method to just read the first part of the double passed in by the user (this is what it sounds like you want to do). This means that if the user enters anything from 62 to 62.0 to 62.999, it will truncate the result to '62':

double nSallyAge = GetDoubleFromUser("2) Q: How old is Sally? ");

if (Math.Truncate(nSallyAge) == 62)
{
    // Increase Score
    sUser1Score++;
    Console.WriteLine("A: Correct, Sally's age is 62, you have been awarded 1 point.");
    Console.ReadLine();
} 

Other alternative ways to use this are:

int sallyAge = Math.Truncate(GetDoubleFromUser("2) Q: How old is Sally? "));

if (sallyAge == 62)
{
    // Increase Score
    sUser1Score++;
    Console.WriteLine("A: Correct, Sally's age is 62, you have been awarded 1 point.");
    Console.ReadLine();
}

Or, you could use an input function that returns an int in the first place:

private static int GetIntFromUser(string prompt)
{
    return Math.Truncate(GetDoubleFromUser(prompt));
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
-1

In your code above, you are converting the input to an integer and then converting the int result to a double.

Assuming that you are only allowing numerical values to be entered, why not try something like this. This will identify if the input contained decimals or not:

string input = Console.ReadLine();

int iSallyAge;
double dSallyAge;

if (!Int32.TryParse(input, iSallyAge)) 
{        
    dSallyAge = Double.Parse(input);
}
Nic
  • 12,220
  • 20
  • 77
  • 105
Peter Henry
  • 651
  • 5
  • 17
-2

You should be using Double.TryParse to parse the user input to a double and then test that value to see whether it's equal to 62.0.

double age;

if (double.TryParse(Console.ReadLine(), out age) && age == 62.0)
{
    // age is 62.
    // ...
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46