469,631 Members | 1,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

C# question regarding mathematical operator input

Hi

Operating system is WinXP using SharpDevelop version 1.1.0 and build 2124.

I'm new to C# and have a problem trying to get a user to enter 3 mathematical operators of choice, then 2 numbers of choice, and giving ouput for each of the mathematical operations using the 2 numbers.

The general script will work but I have a user inputting a string which I don't know how to convert to int to be capable of using it in mathematical operations.

Any advice would be appreciated.

Thanks

M


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class MathOperators
  3. {
  4.     public static void Main()
  5.     {
  6.         MathOperators MO = new MathOperators();
  7.         MO.ShowDemo(); 
  8.   }
  9.   void ShowDemo() 
  10.   {
  11.       // ask the user to choose their mathematical operator
  12.       Console.Write("Please choose your preferred mathematical operator : *, - or +? ");
  13.  
  14.       // declare a string that will take the input parameter
  15.  
  16.       string choice = Console.ReadLine();
  17.  
  18.  
  19.       Console.WriteLine("please enter a number : ");
  20.                     int num1 = Console.ReadLine();
  21.  
  22.                     Console.WriteLine("please enter another number : ");
  23.                     int num2 = Console.ReadLine();
  24.  
  25.       // Apply the ToLower method to convert all casing to lower casing before entering the loop in case 
  26.       // the user enters capatilized letters or words. I don't know how to get ToLower() to work
  27.  
  28.       choice.ToLower();
  29.  
  30.       switch (choice)
  31.                 {
  32.           case "*":                                
  33.               Console.WriteLine("The product of your numbers is : " + (num1 * num2));
  34.               break;
  35.           case "-":
  36.               Console.WriteLine("The value of subtracting the first number from the second is : " + (num1 - num2));
  37.               break;
  38.           case "+":
  39.               Console.WriteLine("The sum of the 2 values is : " + (num1 + num2));
  40.               break;
  41.           default:
  42.               Console.WriteLine("Que??");
  43.               break;
  44.               }  
  45.         }
  46. }
Feb 26 '08 #1
2 2149
Hello !
Replace your code: int num1 = Console.ReadLine();
with int num1=Int.Parse(Console.ReadLine());.
Same for int num2. It is convert give string into number....

Hi

Operating system is WinXP using SharpDevelop version 1.1.0 and build 2124.

I'm new to C# and have a problem trying to get a user to enter 3 mathematical operators of choice, then 2 numbers of choice, and giving ouput for each of the mathematical operations using the 2 numbers.

The general script will work but I have a user inputting a string which I don't know how to convert to int to be capable of using it in mathematical operations.

Any advice would be appreciated.

Thanks

M


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. public class MathOperators
  3. {
  4.     public static void Main()
  5.     {
  6.         MathOperators MO = new MathOperators();
  7.         MO.ShowDemo(); 
  8.   }
  9.   void ShowDemo() 
  10.   {
  11.       // ask the user to choose their mathematical operator
  12.       Console.Write("Please choose your preferred mathematical operator : *, - or +? ");
  13.  
  14.       // declare a string that will take the input parameter
  15.  
  16.       string choice = Console.ReadLine();
  17.  
  18.  
  19.       Console.WriteLine("please enter a number : ");
  20.                     int num1 = Console.ReadLine();
  21.  
  22.                     Console.WriteLine("please enter another number : ");
  23.                     int num2 = Console.ReadLine();
  24.  
  25.       // Apply the ToLower method to convert all casing to lower casing before entering the loop in case 
  26.       // the user enters capatilized letters or words. I don't know how to get ToLower() to work
  27.  
  28.       choice.ToLower();
  29.  
  30.       switch (choice)
  31.                 {
  32.           case "*":                                
  33.               Console.WriteLine("The product of your numbers is : " + (num1 * num2));
  34.               break;
  35.           case "-":
  36.               Console.WriteLine("The value of subtracting the first number from the second is : " + (num1 - num2));
  37.               break;
  38.           case "+":
  39.               Console.WriteLine("The sum of the 2 values is : " + (num1 + num2));
  40.               break;
  41.           default:
  42.               Console.WriteLine("Que??");
  43.               break;
  44.               }  
  45.         }
  46. }
Feb 27 '08 #2
Hello !
Replace your code: int num1 = Console.ReadLine();
with int num1=Int.Parse(Console.ReadLine());.
Same for int num2. It is convert give string into number....

Thanks todashah, works a treat! Much appreciated

M
Feb 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by administrata | last post: by
3 posts views Thread by gelong | last post: by
20 posts views Thread by Patrick Guio | last post: by
14 posts views Thread by invincible | last post: by
30 posts views Thread by Brian Dude | last post: by
12 posts views Thread by sam_cit | last post: by
13 posts views Thread by jacek.strzelczyk | last post: by
3 posts views Thread by Michel Esber | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.