School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,861 people online right now. Registration is fast and FREE... Join Now!



Console app

Console app How to make a multi calc console app Rate Topic: -----

#1 dmxxmd  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 25-October 07


Dream Kudos: 0

Posted 31 October 2007 - 10:04 PM

Hi Im making a console app calculator and I wanto make it so that it ask the person what he want to do, +,-,* or /

but i dont know exactly how to place the code, i believe it will have to be like a if statement..
can someone please help me. my + code works so far it is as follows...

string plus = String.Format(Console.ReadLine());
Console.WriteLine("Enter a number that you want to add up?");
int valA = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter 2nd number that you want to add up?");
int valB = Int32.Parse(Console.ReadLine());
Console.ReadLine();
Console.WriteLine("Value1 + value2 =");
Console.WriteLine(valA + valB);



with the other i will be using the same but just change the + to - etc..
thank you..
DMXXMD

*Always use code tags :) , like so => :code:

This post has been edited by PsychoCoder: 01 November 2007 - 01:07 AM

Was This Post Helpful? 0
  • +
  • -


#2 Footsie  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 354
  • Joined: 20-September 07


Dream Kudos: 50

Posted 31 October 2007 - 11:27 PM

I would maybe look into using a "switch" and "case" statement.
These say pretty much: "in case this is selected this must happen..."

You would have to have a variable indicating the users choice between +,-,*,/
The switch statement would be something like as follows:
Console.WriteLine("Select an operator: +; -; *; /");
	Console.Write("Please enter your selection: ")
		string s = Console.ReadLine();
			
//This is the switch case statement.
//...in CASE variable s is selected as *; +; -; / ,
// do something.

   switch (s)
	   {
			case "*":
		   //Put your multiplication code here
		   break;
			
			 case "+":
			//Put your addition code here
			break;

		   //Put other case statements here			 
		   
		  //always put a default case in to handle if the user 
		  //makes an invalid select

		   default:
		   Console.WriteLine("You have not selected a valid operator");
		break;
		}



I hope this helps you in the right direction...
There may be other ways of doing it... see what the others say too.
Cheers!

This post has been edited by Footsie: 31 October 2007 - 11:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 gogole  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 131
  • Joined: 17-July 07


Dream Kudos: 25

Posted 02 November 2007 - 06:53 AM

foosie is right, you're better off with a switch statement in this case.the code format he provided is also ok but it would be better to use a ReadKey() method than a Console.WriteLine() method since we are dealing with characters here alsoit would prevent string errors. try this:
Console.WriteLine("Select an operator: +; -; *; /");
	Console.Write("Please enter your selection: ")
		char s = Console.Readkey(true);
/*set the boolean to false if you want to display the input in the app window*/
			
//This is the switch case statement.
//...in CASE variable s is selected as *; +; -; / ,
// do something.

   switch (s)
	   {
			case "*":
		   //Put your multiplication code here
		   break;
			
			 case "+":
			//Put your addition code here
			break;

		   //Put other case statements here			 
		   
		  //always put a default case in to handle if the user 
		  //makes an invalid select

		   default:
		   Console.WriteLine("You have not selected a valid operator");
		break;
		}


This post has been edited by gogole: 02 November 2007 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month