473,587 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# question regarding mathematical operator input

23 New Member
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 2390
todashah
26 New Member
Hello !
Replace your code: int num1 = Console.ReadLin e();
with int num1=Int.Parse( Console.ReadLin e());.
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
Madmartigan
23 New Member
Hello !
Replace your code: int num1 = Console.ReadLin e();
with int num1=Int.Parse( Console.ReadLin e());.
Same for int num2. It is convert give string into number....

Thanks todashah, works a treat! Much appreciated

M
Feb 27 '08 #3

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

Similar topics

6
1721
by: administrata | last post by:
Hi! I'm programming maths programs. And I got some questions about mathematical signs. 1. Inputing suqare like a * a, It's too long when I do time-consuming things. Can it be simplified? 2. Inputing fractions like (a / b) + (c / d), It's tiring work too. Can it be simplified? 3. How can i input root?
3
3107
by: gelong | last post by:
Hi there, I have a problem in writing a mathematical function using C++ programming. How to write an input that can insert whole equation? Example is the input are x² + 3y - 4z³ = 0. In maple, it was x^2+3*y-4*z^3 = 0, but how to write it in C++ ? How about the declaration?
20
3853
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
14
1389
by: invincible | last post by:
Hi I want to find an algorithim , which calculates shortest perpendicular distance from a given point to a line. Thanks Mohan
30
1513
by: Brian Dude | last post by:
Hello, I understand the part about not comparing two floating-point numbers for exact-ness. Does this also apply to comparisons to constants? i.e.: double f; if ( f <= 1.0){ ... }
2
7361
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. ...
12
1440
by: sam_cit | last post by:
Hi Everyone, I often get confused with * and ++ operator when they are used with pointers as to which would be considered first by the compiler, Can anyone explain with some examples to understand them easier???
13
2147
by: jacek.strzelczyk | last post by:
Hello, I'm looking for a C library that provides the notation of n- dimensional mathematical functions. Or is there any other way to decode that kind of functions in C language? Thanks in advance, Jacek
3
3943
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID = ?
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.