Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,716 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 2,186 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Console app

 
Reply to this topicStart new topic

Console app, How to make a multi calc console app

dmxxmd
post 31 Oct, 2007 - 11:04 PM
Post #1


New D.I.C Head

*
Joined: 26 Oct, 2007
Posts: 25


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 - 02:07 AM
User is offlineProfile CardPM

Go to the top of the page


Footsie
post 1 Nov, 2007 - 12:27 AM
Post #2


D.I.C Regular

Group Icon
Joined: 20 Sep, 2007
Posts: 279



Thanked 2 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: 1 Nov, 2007 - 12:33 AM
User is offlineProfile CardPM

Go to the top of the page

gogole
post 2 Nov, 2007 - 07: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 - 07:55 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/8/08 09:12AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month