Console app

How to make a multi calc console app

Page 1 of 1

2 Replies - 18069 Views - Last Post: 02 November 2007 - 07:53 AM Rate Topic: -----

#1 dmxxmd  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 26-October 07

Console app

Posted 31 October 2007 - 11: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 - 02:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Console app

#2 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Console app

Posted 01 November 2007 - 12:27 AM

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: 01 November 2007 - 12:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 gogole  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 17-July 07

Re: Console app

Posted 02 November 2007 - 07: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 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1