LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2012, 05:13 AM   #1
jpee
LQ Newbie
 
Registered: Dec 2011
Location: Belgium
Distribution: ubuntu 11
Posts: 5

Rep: Reputation: Disabled
C# trouble with "switch" statement


I copied a program example from "Programming in the key of C#" by charles petzold.
using System;
class calculator
{
static void Main()
{
double Num1, Num2, Result = 0;
bool GotOperation;
Console.Write("Enter first number: ");
Num1 = Double.Parse(Console.ReadLine());

Console.Write("Enter second number: ");
Num2 = Double.Parse(Console.ReadLine());

do
{
Console.Write("Enter the operation (+, -, *, /, %): ");
string op = Console.ReadLine().Trim();
Console.WriteLine("op = {0}" ,op);
GotOperation = true;

switch (op)
{
case "+":
Result = Num1 + Num2;
break;

case "-":
Result = Num1 - Num2;
break;

case "*":
Result = Num1 * Num2;
break;

case "/":
Result = Num1 / Num2;
break;

case "%":
Result = Num1 % Num2;
break;

default:
Console.WriteLine("Operation {0} is not valid",op);
GotOperation = false;
break;

}
}
while (GotOperation);
Console.WriteLine("The result is " + Result);
}
}

See the output. The program doesn't go past I entered the operation.Do I forgot something?
Output:
john@john-Aspire-M1300:~/Documenten$ mono calculator.exe
Enter first number: 45
Enter second number: 9
Enter the operation (+, -, *, /, %): *
op = *
Enter the operation (+, -, *, /, %): +
op = +
Enter the operation (+, -, *, /, %):
 
Old 06-29-2012, 06:27 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,687

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
please use [code][/code] to keep formatting
I think this is what you implemented, just try to follow the logic:
enter num1, enter num2 and start loop. Inside the loop ask operation, calculate result (in case of valid input), and here comes the end of the loop, so this cycle repeated - until you enter an invalid op. In that case it will exit the loop and after the line containing while finally it will print the content of result (that will contain the result of the last valid calculation).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make "ls" command show colors without "--color=always" switch SharpyWarpy Linux - General 4 07-16-2007 11:00 PM
C Drive "/dev/hda1" accidently used in dd command at "of=" switch M D Linux - Software 4 04-26-2007 08:56 AM
"NIM thread blocked" & "Deadman Switch (DMS) close to trigger" errors sosborne Linux - Networking 1 06-28-2006 02:07 PM
"NIM thread blocked" & "Deadman Switch" errors sosborne AIX 3 06-01-2006 11:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration