C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,326 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,985 people online right now. Registration is fast and FREE... Join Now!




Console app

 

Console app, How to make a multi calc console app

dmxxmd

31 Oct, 2007 - 10:04 PM
Post #1

New D.I.C Head
*

Joined: 25 Oct, 2007
Posts: 26



Thanked: 4 times
My Contributions
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...

CODE

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 smile.gif , like so => code.gif

This post has been edited by PsychoCoder: 1 Nov, 2007 - 01:07 AM

User is offlineProfile CardPM
+Quote Post


Footsie

RE: Console App

31 Oct, 2007 - 11:27 PM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 337



Thanked: 8 times
Dream Kudos: 50
My Contributions
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:
CODE

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 Oct, 2007 - 11:33 PM
User is offlineProfile CardPM
+Quote Post

gogole

RE: Console App

2 Nov, 2007 - 06:53 AM
Post #3

D.I.C Head
Group Icon

Joined: 17 Jul, 2007
Posts: 131


Dream Kudos: 25
My Contributions
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:
CODE

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: 2 Nov, 2007 - 06:55 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 03:01PM

Live C# Help!

Be Social

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

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month