473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

First delve into C#

Complete n00b here, so please bear with me.

I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo

Also, the While clause at the end...ideally, I would like to write something
like
while (strMore != "y" and strMore !="n")

but I don't know how to do this.

Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";

//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);

//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the

//system can loop
//ideal...don't think so,

//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}

Oct 12 '06 #1
14 1322
It looks to me like you're expecting the user to follow instructions, and
not make any typos, as your code asks the user to enter a number, and then
parses the string input to a number without checking to see if it is a
number first. In the event that a user enters a non-numeric input, your app
will crash. You need to implement some checking. For example, you could use
System.Double.TryParse to check the input first. Example:

bool isNumeric = false;
double dblFirstNo;
string input;

while (!isNumeric)
{
input = Console.ReadLine();
isNumeric = Double.TryParse(input, out dblFirstNo);
if (!isNumeric)
Console.WriteLine("You did not enter a number. Please enter a number
");
}

See http://msdn2.microsoft.com/en-us/lib....tryparse.aspx
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Billy" <Bi***@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
Complete n00b here, so please bear with me.

I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine
which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo

Also, the While clause at the end...ideally, I would like to write
something
like
while (strMore != "y" and strMore !="n")

but I don't know how to do this.

Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";

//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);

//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the

//system can loop
//ideal...don't think so,

//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}

Oct 12 '06 #2
Hi Billy,

I've taken the liberty to rewrite your code, splitting it into several
smaller methods. This makes the code easier to follow. A thumb rule is,
if you can't se the entire method on the screen at the same time, the
method is too long. Your code would have crashed if you entered text or
an invalid operand. Also, a switch is faster and easier to maintain than
if/else if. Default will never happen, but is needed since the compiler
does not know that (or put the return after the switch). Instead of
ReadLine I used a ReadKey for the Y/N and hide the result.
static void Main(string[] args)
{
do
{
Calculate();
}
while (DoContinue());
}

static void Calculate()
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strOperand = "*";

//get the first number
Console.WriteLine("Please enter a number ");
dblFirstNo = ReadNumber();

//ask what they want to do with the numbers
Console.WriteLine("Please enter an operand ( * / + - )");
strOperand = ReadOperand();

//get the next number
Console.WriteLine("Please enter another number");
dblSecondNo = ReadNumber();

// get the answer
dblAnswer = GetAnswer(dblFirstNo, strOperand, dblSecondNo);

//output the sum and the answer
Console.WriteLine("{0} {1} {2} = {3}", dblFirstNo, strOperand,
dblSecondNo, dblAnswer);
}

static string ReadOperand()
{
string operand = Console.ReadLine();
while (operand != "*" && operand != "/" && operand !="+" &&
operand != "-")
{
Console.WriteLine("Please enter a valid operand");
operand = Console.ReadLine();
}

return operand;
}

static double ReadNumber()
{
double result;
string value = Console.ReadLine();

while (!Double.TryParse(value, out result))
{
Console.WriteLine("Please enter a valid number");
value = Console.ReadLine();
}

return result;
}

static double GetAnswer(double firstNumber, string operand, double
secondNumber)
{
switch (operand)
{
case "*":
return firstNumber * secondNumber;
case "/":
return firstNumber / secondNumber;
case "+":
return firstNumber + secondNumber;
case "-":
return firstNumber - secondNumber;
default:
return Double.NaN;
}
}

static bool DoContinue()
{
//ask if the user wants to continue
Console.WriteLine("Do you want to enter more? (y/n)");

do
{
ConsoleKey k = Console.ReadKey(true).Key;
if (k == ConsoleKey.Y)
return true;
else if(k == ConsoleKey.N)
return false;
else
Console.WriteLine("Please enter Y or N");
}
while (true);
}

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 12 '06 #3
A perfectly reasonable effort. I've rewritten it and you'll notice I've
not answered your question about "while (strMore !="y" and strMore
!="n")" so I'll do that here.
you would write it as
do
{
stuff
}while(aString != "y" & aString != "n"); use | (pipe) for or.

Here's my effort. I've bunged it in a function and added an enum for
program control. The function is static so you can just paste it into
your app and call it with Calc(); Watch for line wrap :)

private enum PlayAgainOptions
{
invalid, playAgain, exitGame
}

private static void Calc()
{
double firstNo, secondNo, result = 0;
string operand;
PlayAgainOptions playAgain = PlayAgainOptions.invalid;

do
{
System.Console.WriteLine("Please enter a number");
firstNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter another number");
secondNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter an operand ( * / + - )");
operand = System.Console.ReadLine();

switch(operand)
{
case "*":
result = firstNo * secondNo;
break;
case "/":
result = firstNo / secondNo;
break;
case "+":
result = firstNo + secondNo;
break;
case "-":
result = firstNo - secondNo;
break;
default:
// None of the above!
break;
}
// include the operand in the answer
System.Console.WriteLine("{0} {1} {2} = {3}",firstNo, operand,
secondNo, result);

do
{
System.Console.WriteLine("Do you want to enter more? (y/n)");
//convert to lowercase so we capture a Y or N as well.
switch (System.Console.ReadLine().ToLower())
{
case "y":
playAgain = PlayAgainOptions.playAgain;
break;
case "n":
playAgain = PlayAgainOptions.exitGame;
break;
default:
playAgain = PlayAgainOptions.invalid;
break;
}
} while (PlayAgainOptions.invalid == playAgain);

}while(PlayAgainOptions.playAgain == playAgain);
}
}
Billy wrote:
Complete n00b here, so please bear with me.

I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo

Also, the While clause at the end...ideally, I would like to write something
like
while (strMore != "y" and strMore !="n")

but I don't know how to do this.

Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";

//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);

//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the

//system can loop
//ideal...don't think so,

//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}
Oct 12 '06 #4
Kevin

I do know about validation and my programs always have it. It's not here
simply because this is THE first program I have ever written in C#, it won't
be used by anyone and I was using it simply to look at functionality, try out
commands etcm, etc.

Rest assured my C# programs will have validation built in.

Thx for taking the time to reply though. You mayconsider me suitably
chastised ;-)

"Kevin Spencer" wrote:
It looks to me like you're expecting the user to follow instructions, and
not make any typos, as your code asks the user to enter a number, and then
parses the string input to a number without checking to see if it is a
number first. In the event that a user enters a non-numeric input, your app
will crash. You need to implement some checking. For example, you could use
System.Double.TryParse to check the input first. Example:

bool isNumeric = false;
double dblFirstNo;
string input;

while (!isNumeric)
{
input = Console.ReadLine();
isNumeric = Double.TryParse(input, out dblFirstNo);
if (!isNumeric)
Console.WriteLine("You did not enter a number. Please enter a number
");
}

See http://msdn2.microsoft.com/en-us/lib....tryparse.aspx
--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.
Oct 12 '06 #5
Morten

Switch - yes indeed, it is better than if...then...else. Coming from a VB
background, and used to Select Case, I didn't know the syntax for similar
functionality. Thx

Keeping code modular - again, correct and thx for showing me how. I was
wanting to split it down into smaller chunks, but, again coming from VB, I
couldn't work out how to create a method which returns a value...again, thx
for showing me how to.

Thx for taking the time to do this.

Rest assured, I have a book on C# and have only just started it (doing it at
home). I was just trying to write a program at work to try to remember some
of the things from the first two chapters of the book (that I've read so far).

Both your posts were great help

Thx

Oct 12 '06 #6
do
{
stuff
}while(aString != "y" & aString != "n"); use | (pipe) for or.
It might be better to use "&&" instead of "&" (and "||" instead of
"|").

From MSDN:
The operation
x && y
corresponds to the operation
x & y
except that if x is false, y is not evaluated (because the result of
the AND operation is false no matter what the value of y may be). This
is known as "short-circuit" evaluation.

Especially if it is called a lot of times and the "y" uses some method
that takes a "long" time to evaluate, this will speed up your program
somewhat.

Hans Kesting
Oct 12 '06 #7
One thing I am particularly concerned about is this code here

***********************************************
System.Console.WriteLine("Please enter an operand ( * / + - )");
strOperand = System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo = System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
***********************************************
I am getting the operand into a string (strOperand). Rather than try and
determine the contents of that variable and then perform the calculation be
it through a switch of if..then..else statement, is there no syntax that
allows me to do this

dblAnswer = dblFirstNo strOperand dblsecondNo

So use the variable directly, or the contents of the variable directly
rather than expensive processing alternatives like switch or if..then..else?

Thx
Oct 12 '06 #8
Thx DeveloperX...it is very much my first attempt and I plan on getting a
whole lot better.

Thx for the ( | ) in place of or...I forgot (from my dark days in the past
doing Turbo C) about the OR operator.

"DeveloperX" wrote:
A perfectly reasonable effort. I've rewritten it and you'll notice I've
not answered your question about "while (strMore !="y" and strMore
!="n")" so I'll do that here.
you would write it as
do
{
stuff
}while(aString != "y" & aString != "n"); use | (pipe) for or.

Here's my effort. I've bunged it in a function and added an enum for
program control. The function is static so you can just paste it into
your app and call it with Calc(); Watch for line wrap :)

private enum PlayAgainOptions
{
invalid, playAgain, exitGame
}

private static void Calc()
{
double firstNo, secondNo, result = 0;
string operand;
PlayAgainOptions playAgain = PlayAgainOptions.invalid;

do
{
System.Console.WriteLine("Please enter a number");
firstNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter another number");
secondNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter an operand ( * / + - )");
operand = System.Console.ReadLine();

switch(operand)
{
case "*":
result = firstNo * secondNo;
break;
case "/":
result = firstNo / secondNo;
break;
case "+":
result = firstNo + secondNo;
break;
case "-":
result = firstNo - secondNo;
break;
default:
// None of the above!
break;
}
// include the operand in the answer
System.Console.WriteLine("{0} {1} {2} = {3}",firstNo, operand,
secondNo, result);

do
{
System.Console.WriteLine("Do you want to enter more? (y/n)");
//convert to lowercase so we capture a Y or N as well.
switch (System.Console.ReadLine().ToLower())
{
case "y":
playAgain = PlayAgainOptions.playAgain;
break;
case "n":
playAgain = PlayAgainOptions.exitGame;
break;
default:
playAgain = PlayAgainOptions.invalid;
break;
}
} while (PlayAgainOptions.invalid == playAgain);

}while(PlayAgainOptions.playAgain == playAgain);
}
}
Billy wrote:
Complete n00b here, so please bear with me.

I have written the following code and would like to know if it could be
optimised. In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo

Also, the While clause at the end...ideally, I would like to write something
like
while (strMore != "y" and strMore !="n")

but I don't know how to do this.

Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";

//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand ( * / + -
)");
strOperand =
System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);

//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the

//system can loop
//ideal...don't think so,

//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}

Oct 12 '06 #9
No, you will have to compare against each known operator and thereby
create four different lines of code.

On Thu, 12 Oct 2006 13:55:01 +0200, Billy
<Bi***@discussions.microsoft.comwrote:
One thing I am particularly concerned about is this code here

***********************************************
System.Console.WriteLine("Please enter an operand ( * / + - )");
strOperand = System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo = System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
***********************************************
I am getting the operand into a string (strOperand). Rather than try and
determine the contents of that variable and then perform the calculation
be
it through a switch of if..then..else statement, is there no syntax that
allows me to do this

dblAnswer = dblFirstNo strOperand dblsecondNo

So use the variable directly, or the contents of the variable directly
rather than expensive processing alternatives like switch or
if..then..else?

Thx


--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 12 '06 #10
As Hans mentioned, the only time you would want to use | instead of || (or
& instead of &&) is when you absolutely need the second statement to be
checked no matter what the first statement returned. For instance

string s = null;

if(s == null | s.Length == 0)
// This will crash since you can't read the Length property of nothing

if(s == null || s.Length == 0)
// This will be ok. Since s == null, s.Length will never be tested

In logic operations you would use | and & though.

int n = 6 | 3;
On Thu, 12 Oct 2006 13:57:01 +0200, Billy
<Bi***@discussions.microsoft.comwrote:
Thx DeveloperX...it is very much my first attempt and I plan on getting a
whole lot better.

Thx for the ( | ) in place of or...I forgot (from my dark days in the
past
doing Turbo C) about the OR operator.

"DeveloperX" wrote:
>A perfectly reasonable effort. I've rewritten it and you'll notice I've
not answered your question about "while (strMore !="y" and strMore
!="n")" so I'll do that here.
you would write it as
do
{
stuff
}while(aString != "y" & aString != "n"); use | (pipe) for or.

Here's my effort. I've bunged it in a function and added an enum for
program control. The function is static so you can just paste it into
your app and call it with Calc(); Watch for line wrap :)

private enum PlayAgainOptions
{
invalid, playAgain, exitGame
}

private static void Calc()
{
double firstNo, secondNo, result = 0;
string operand;
PlayAgainOptions playAgain = PlayAgainOptions.invalid;

do
{
System.Console.WriteLine("Please enter a number");
firstNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter another number");
secondNo = double.Parse(System.Console.ReadLine());

System.Console.WriteLine("Please enter an operand ( * / + - )");
operand = System.Console.ReadLine();

switch(operand)
{
case "*":
result = firstNo * secondNo;
break;
case "/":
result = firstNo / secondNo;
break;
case "+":
result = firstNo + secondNo;
break;
case "-":
result = firstNo - secondNo;
break;
default:
// None of the above!
break;
}
// include the operand in the answer
System.Console.WriteLine("{0} {1} {2} = {3}",firstNo, operand,
secondNo, result);

do
{
System.Console.WriteLine("Do you want to enter more? (y/n)");
//convert to lowercase so we capture a Y or N as well.
switch (System.Console.ReadLine().ToLower())
{
case "y":
playAgain = PlayAgainOptions.playAgain;
break;
case "n":
playAgain = PlayAgainOptions.exitGame;
break;
default:
playAgain = PlayAgainOptions.invalid;
break;
}
} while (PlayAgainOptions.invalid == playAgain);

}while(PlayAgainOptions.playAgain == playAgain);
}
}
Billy wrote:
Complete n00b here, so please bear with me.

I have written the following code and would like to know if it could
be
optimised. In particular, I'm reading the user input for the operand
into
variable strOperand, and rather than use the IF statement to
determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo

Also, the While clause at the end...ideally, I would like to write
something
like
while (strMore != "y" and strMore !="n")

but I don't know how to do this.

Any help appreciated.
Code ************************************
static void Main(string[] args)
{
//declare the variables to be used with default values
double dblFirstNo = 0, dblSecondNo = 0, dblAnswer = 0;
string strMore = "y", strOperand = "*";

//keep going until the user enters "n"
while (strMore == "y")
{
//get the first number
System.Console.WriteLine("Please enter a number ");
dblFirstNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//ask what they want to do with the numbers
System.Console.WriteLine
("Please enter an operand (*
/ + -
)");
strOperand =
System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo =

System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
//output the sum and the answer
System.Console.WriteLine
("{0} * {1} =
{2}",dblFirstNo,dblSecondNo,dblAnswer);

//ask if the user wants to continue
strMore = "z";
while (strMore == "z")
{
System.Console.WriteLine
("Do you want
to
enter more? (y/n)");
strMore = System.Console.ReadLine();
//check to see what the user entered
//was it Y
if (strMore != "y")
{
//nope was it n
if (strMore != "n")
{
//set it to z so the

//system can loop
//ideal...don't think so,

//but it'll do until
//I get more proficient
strMore = "z";
}
}
}
}
}



--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 12 '06 #11

Hans Kesting wrote:
do
{
stuff
}while(aString != "y" & aString != "n"); use | (pipe) for or.

It might be better to use "&&" instead of "&" (and "||" instead of
"|").

From MSDN:
The operation
x && y
corresponds to the operation
x & y
except that if x is false, y is not evaluated (because the result of
the AND operation is false no matter what the value of y may be). This
is known as "short-circuit" evaluation.

Especially if it is called a lot of times and the "y" uses some method
that takes a "long" time to evaluate, this will speed up your program
somewhat.

Hans Kesting
Very good point!

Oct 12 '06 #12
Thx all for replies.

Very helpful

"Morten Wennevik" wrote:
No, you will have to compare against each known operator and thereby
create four different lines of code.

On Thu, 12 Oct 2006 13:55:01 +0200, Billy
<Bi***@discussions.microsoft.comwrote:
One thing I am particularly concerned about is this code here

***********************************************
System.Console.WriteLine("Please enter an operand ( * / + - )");
strOperand = System.Console.ReadLine();

//get the next number
System.Console.WriteLine("Please enter another number" );
dblSecondNo = System.Convert.ToDouble(System.Console.ReadLine()) ;

//determine the operand used and perform the calculation
//this isn't ideal either, but I don't know how to use
//the operand as a variable and get it to work as an operand!
if (strOperand == "*")
{
dblAnswer = dblFirstNo * dblSecondNo;
}
else if (strOperand == "/")
{
dblAnswer = dblFirstNo / dblSecondNo;
}
else if (strOperand == "+")
{
dblAnswer = dblFirstNo + dblSecondNo;
}
else if (strOperand == "-")
{
dblAnswer = dblFirstNo - dblSecondNo;
}
***********************************************
I am getting the operand into a string (strOperand). Rather than try and
determine the contents of that variable and then perform the calculation
be
it through a switch of if..then..else statement, is there no syntax that
allows me to do this

dblAnswer = dblFirstNo strOperand dblsecondNo

So use the variable directly, or the contents of the variable directly
rather than expensive processing alternatives like switch or
if..then..else?

Thx

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 12 '06 #13
I've been thinking about the below and it is possible, but it's not
pretty and more effort than it's worth (unless anyone has a simpler
method :)). It's quite interesting though and probably a nice little
proof of concept project.

In dotnet you can use something called reflection to peer in on the
code, dynamically load modules and call functions amongst other things.

You can also use reflection to generate code on the fly. All you would
have to do is generate some source code that included perhaps a static
function on an object that returned the result of dblFirstNo strOperand
dblSecondNo. I.e
return 23 * 23;

If you've done any SQL server work, think of reflection as the dotnet
version of using sysobjects and syscolumns to find out what tables have
what columns.

In particular, I'm reading the user input for the operand into
variable strOperand, and rather than use the IF statement to determine which
operand to use, I'd like to use the variable contents
e.g. dblAnswer = dblFirstNo strOperand dblSecondNo
Oct 12 '06 #14
Hi Billy,

*Not* "chastised!" As I didn't know your skill level, other than your
self-description as a "Complete n00b" it was meant as a simple and friendly
instruction/caution.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"Billy" <Bi***@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
Kevin
<snip>
Thx for taking the time to reply though. You mayconsider me suitably
chastised ;-)

"Kevin Spencer" wrote:
>It looks to me like you're expecting the user to follow instructions, and
not make any typos, as your code asks the user to enter a number, and
then
parses the string input to a number without checking to see if it is a
number first. In the event that a user enters a non-numeric input, your
app
will crash. You need to implement some checking. For example, you could
use
System.Double.TryParse to check the input first. Example:

Oct 13 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: R.Marquez | last post by:
I hope I don't bore you with this personal experience. But, I hope the details are helpful for other Python and/or Linux newbies, or for those thinking about becoming such. I have been using...
24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
12
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on...
1
by: Patrick | last post by:
I am trying to get "first-letter" to work inline withing an anchor. Actually I have not been able to get it to work within an <a> tag whether inline, linked, or embedded. Using IE6 service pack 1....
3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
16
by: TB | last post by:
Hi all: If you think that the following comments are absolute amateurish, then please bear with me, or simply skip this thread. A couple of months back I made the decision to initiate a...
1
by: Tarun Mistry | last post by:
Hello all, im transitioning to .NET from various other languages (java, php, c++ etc) as I need to create some web services. However before I delve into doing so, I thought I would find some best...
4
by: Milan Krejci | last post by:
int first=15,latest=15; QString typ=NULL; std::map<int,std::string>::iterator i; for(i = SeznamPracovniDoby.begin(); i != SeznamPracovniDoby.end(); i++) { if (typ==NULL) typ=i->second.c_str(); if...
26
by: cutecutemouse | last post by:
I'm writing a casting function like that, string dbl2s(double dbl) { char chs; memset(chs, 0, sizeof(chs)); _snprintf_s(chs, _countof(chs), MAX_STRING_LENGTH, "%f", dbl); return string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.