Fiddling with C# on VisualBasic
October 13, 2006 2:14 PM   Subscribe

I'm learning C# right now, and I can do very basic things with it. For example, I can cause it to ask you to put in two numbers, and it will give you their sum, difference, quotient, and divisor. What command do I need for it to ask the user WHICH operation it would like the program to perform?

This is what I've got so far:

float num1, num2, sum, quotient, divisor, difference;

Console.WriteLine("Please enter two numbers:");

num1 = float.Parse(Console.ReadLine());
num2 = float.Parse(Console.ReadLine());

sum = num1+num2;
quotient = num1 * num2;
divisor = num1 / num2;
difference = num1 - num2;

Console.WriteLine("The sum is: " + sum + " The difference is: " + difference + " The quotient is: " + quotient + " The divisor is: " + divisor);



(with all the appropriate strings before, of course.)

What do I need to add for it to ask "Which operation would you like performd?" and then the user can type in "addition" and would recieve ("The sum is: " + sum) or type in "division" and would recieve ("The divisor is: "+ divisor), etc? Is this even possible in C#?
posted by alon to Computers & Internet (18 answers total)
 
Response by poster: *make that Visual C# for the title, my mistake.
posted by alon at 2:15 PM on October 13, 2006


Best answer: A simple if statement.

*not in C# but you should get the point*

if (userInput == "addition") {
Console.WriteLine("The sum is....);
}
if (userInput == "division") {
Console.WriteLine("The..);
}

...


if you have a bunch of different operations you could use a switch statement

PS you should handle lower and uppercase variants of the input
posted by mphuie at 2:25 PM on October 13, 2006


Response by poster: thanks, but what would i enter instead of userInput, as that command does not exist in C#?
posted by alon at 2:37 PM on October 13, 2006


there was a missing (skipped) line of code in mphuie's comment that were otherwise stellar. Put this in front of the if statement code he provided.

string userInput = Console.ReadLine().ToLower();

That reads what the user types in ("addition", "subtraction", etc) and stores it in the variable called userInput. The ToLower() method converts a string from "MiXeD CaSe" to "lower case".
posted by escher at 2:48 PM on October 13, 2006


Best answer: Escher beat me to it. You want something like this:

string operation = Console.ReadLine().ToLower();
switch (operation)
{
case "addition":
Console.WriteLine("The sum is" + sum);
break;
case (the rest of them....)
default:
Console.WriteLine("I don't recognize that operation!");
break;
}
posted by JohnFredra at 2:50 PM on October 13, 2006


One thing, if this is a college assignment (and from your profile, this is highly likely), I strongly advise you to consult your instructor - we're not here to do your homework.

Second, since this will get you going in the right direction, read up on the switch statement in C#. It will take a string and let you take an action or actions based on the string contents.
posted by plinth at 2:50 PM on October 13, 2006


(or even a high school assignment)
posted by plinth at 2:50 PM on October 13, 2006


There are a lot of tutorials on the web for this kind of introductory stuff.
If you're having trouble googling them because google ignores the # symbol, actually spelling out the word "sharp" works.
posted by juv3nal at 3:05 PM on October 13, 2006


It will take a string and let you take an action or actions based on the string contents.

It'll take any integral type (int, string, etc) and let you take an action or actions based on the contents... it's particularly useful to use enumerations, especially with VS2005 code snippets.

Nifty hint for VS2005 with C# projects: type enough of "switch" that the "switch" item is highlighted in the intellisense box. Hit tab twice, then enter the variable you want to switch on. If the variable is an enum, it'll fill in blank case entries for every enum value. <3 br>
switch statements are great, and are somewhat more performant than if/else constructs.

But i'm guessing at this level the OP isn't worried about a few cycles of performance ;)
posted by flaterik at 3:25 PM on October 13, 2006


Response by poster: this is neither a high school nor college assignment- i learned the basic command string that i posted above in my g10 high school course and out of personal curiosity wondered whether i could build on that, and how.

thanks for all the help, i'll update you on whether this all works or not =)
posted by alon at 3:27 PM on October 13, 2006


Response by poster: it works! hallelujah. thanks for the help!
posted by alon at 4:29 PM on October 13, 2006


JohnFredra writes "case "addition": "


Note that C# departs from C, C++, and Java by allowing switch statements on strings. If you are ever planning to use those lanaguges, don't get in the habit of switching on strings, it'll be something you'll have to unlearn. (It also completely obscures the timing cost of the switch, how strings are compared, and how compiled langauges translate switch statements to machine languge.)
posted by orthogonality at 1:55 AM on October 14, 2006


One thing, if this is a college assignment (and from your profile, this is highly likely), I strongly advise you to consult your instructor

Yeah, God forbid we encourage kids to do a bit of independent research.
posted by chrismear at 2:26 AM on October 14, 2006


God forbid that we enable someone to cheat.
posted by plinth at 6:10 AM on October 14, 2006


God forbid that we enable someone to cheat.

How is asking for help with your homework cheating? You have never, in your life, said "Dad, I don't get this math problem. Can you help me?"

Absurd. Calm down.
posted by ChasFile at 11:27 AM on October 14, 2006


Orthogonality is absolutely right. C# makes easy some things that should be easy, but making things easier for the user unfortunately tends to increase the complexity of the back end.

Orthogonality, you seem to know a thing or two about low-level language performance. Any thoughts on whether the 'foreach' construct inters a similar a performance hit? Having programmed for a long time in c/c++, it's one of the things that's endeared me to c#...
posted by JohnFredra at 11:40 AM on October 14, 2006


foreach is syntactic sugar for the methods GetEnumerator, MoveNext, Reset, and Current in C#, but to be fully compatible with the CLR, you need to implement IEnumerator and IEnumerable.

foreach (string s in collection) { // body
}
is syntactically identical to
IEnumerator noName = collection.GetEnumerator();
while (noName.MoveNext()) {
string s = (string)noName.Current;
// body
}

So what you get is 1 method call overhead for the whole thing and two method calls for each iteration (plus whatever it takes to actually to the work under the hood).

The cost is fairly low, all things considered.
posted by plinth at 1:02 PM on October 14, 2006


foreach is also a bit of a memory hit, though.

It's somewhat less performant, but not enough to matter most of the time.

I love foreach for quick implementations, and things that will only run once. But when I'm doing code that requires high performance I make sure to use indexes in my frequently run loops. It's not a huge gain, but it's low hanging fruit.
posted by flaterik at 3:59 PM on October 16, 2006


« Older Music podcast legalities help   |   Online Bakeries With Pretty Cakes Newer »
This thread is closed to new comments.