delegate double processdelegate(double param1, double param2);
static double multiply(double param1, double param2)
{
return param1 * param2;
}
static double divide(double param1, double param2)
{
return param1 / param2;
}
static void Main(string[] args)
{
processdelegate process;
double param1, param2;
string input;
Console.WriteLine("Please enter first number");
param1 = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter second number");
param2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
while (input != "m")
{
Console.WriteLine("Please enter either m or d");
input = Console.ReadLine();
if (input == "m")
{
process = new processdelegate(multiply);
}
if (input == "d")
{
process = new processdelegate(divide);
}
}
Console.WriteLine("Result: " + process);
Console.ReadKey();
problem with output
Started by Siten0308, Sep 11 2008 10:48 AM
12 replies to this topic
#1
Posted 11 September 2008 - 10:48 AM
Hello I am having a problem (duh) what i want it to do is, if someone puts something that is not M = multiplay or D = divide, then ask them again until they get it right, which i am using a while loop for that, however it does not do that, wondering if you can take a look, also i am practacing and finishing up on my basic C# by closing this part of my training with delegates and functions which are part of my code and using the if statements. But also my MAIN and BIG problem is below, the process variable, it will not put out what i want, if you can correct what should be in there that would be great, it gives error message "use of unassigned local variable'process'" so i dont know, i tried global variable but trying global process = processdelegate.proc ess wont work. please help, the code is below
|
|
|
#2
Posted 11 September 2008 - 02:52 PM
Ok i have tried something else, which sorta helped but still gave me a warning, what i did was use a global variable called process so now at the end of my program or main function i use Program.process. however the problem is two things still: 1. it gives me this warning message: "field'Console.Application.Program.process' is never assigned to, and will always have its default 0" which when i run and type M, for multiply two numbers, it says 0 no matter which number i put, D still throws that loop which is incorrect, should divide the two numbers, but what it should do is D is to divide, or m is to multiply, any other input by user should loop, which that is the second problem, it loops on D how do I use the while with two variables to look out for or exception to any other or more than 2 which i think is a good practice. Please help and if you can correct the code below including explain what i did wrong that would be helpful, thank you in advance.
here is the code below:
here is the code below:
delegate double processdelegate(double param1, double param2);
static double multiply(double param1, double param2)
{
return param1 * param2;
}
static double divide(double param1, double param2)
{
return param1 / param2;
}
static double process;
static void Main(string[] args)
{
processdelegate process;
double param1, param2;
string input;
Console.WriteLine("Please enter first number");
param1 = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter second number");
param2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
while (input != "m")
{
Console.WriteLine("Please enter either m or d");
input = Console.ReadLine();
if (input == "m")
{
process = new processdelegate(multiply);
}
if (input == "d")
{
process = new processdelegate(divide);
}
}
Console.WriteLine("Result: " + Program.process);
Console.ReadKey();
#3
Posted 11 September 2008 - 08:58 PM
To make the while loop work right you need to add an and statement like so:
while(input != "m" && input != "d")
Console.WriteLine("Please enter either m or d");
input = Console.ReadLine();
if (input == "m")
{
process = new processdelegate(multiply);
}
else if (input == "d")
{
process = new processdelegate(divide);
}
}
this should take care of that issue.
#4
Posted 11 September 2008 - 09:16 PM
great thanks gaylo565, now i just need help figuring out what to do about that process variable problem? what do you think?
#5
Posted 12 September 2008 - 11:07 AM
Hey Gaylo565, after reviewing and running the changes you suggested, i get this error message "The field'Console.application1.Program.process'is never used, which of course is my second problem, but the first problem still continues sadly, when i run it, when i press D, it still loops asking, so if there is any suggestion or correct it that would greatly help.Thanks in advance
#6
Posted 12 September 2008 - 12:42 PM
Hello,
Well with the help of Gaylo565 i did arrange or modify his answer, and now i sorta got it working, except the processdelegate process is still not working, i dont know how to get it to work, should i use a reference in the parameter? should i use global? if so then how to i make it equal to the processdelegate process to a global process? please help, and thank you in advance, here is the code that sorta works.
Well with the help of Gaylo565 i did arrange or modify his answer, and now i sorta got it working, except the processdelegate process is still not working, i dont know how to get it to work, should i use a reference in the parameter? should i use global? if so then how to i make it equal to the processdelegate process to a global process? please help, and thank you in advance, here is the code that sorta works.
class Program
{
delegate double processdelegate(double param1, double param2);
static double multiply(double param1, double param2)
{
return param1 * param2;
}
static double divide(double param1, double param2)
{
return param1 / param2;
}
static string process;
static void Main(string[] args)
{
processdelegate process;
double param1, param2;
string input;
Console.WriteLine("Please enter first number");
param1 = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter second number");
param2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
while (input != "m" && input != "d")
{
Console.WriteLine("Please enter either m or d");
input = Console.ReadLine();
}
if (input == "m")
{
process = new processdelegate(multiply);
}
else if (input == "d")
{
process = new processdelegate(divide);
}
Console.WriteLine("Results = {0}", process(param1,param2));
Console.ReadKey();
}
}
}
#7
Posted 12 September 2008 - 01:04 PM
Hello everyone, finally got it, the reason was because the processdelegate process was in one which was in the first method, however the others didnt know about it, so, by declaring processdelegate process outside and making it global i can now use it anywhere. did i explain that right?
here is the code that works now
here is the code that works now
class Program
{
delegate double processdelegate(double param1, double param2);
static double multiply(double param1, double param2)
{
return param1 * param2;
}
static double divide(double param1, double param2)
{
return param1 / param2;
}
static processdelegate process;
static void Main(string[] args)
{
double param1, param2;
string input;
Console.WriteLine("Please enter first number");
param1 = double.Parse(Console.ReadLine());
Console.WriteLine("Please enter second number");
param2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
while (input != "m" && input != "d")
{
Console.WriteLine("Please enter either m or d");
input = Console.ReadLine();
}
if (input == "m")
{
process = new processdelegate(multiply);
}
else if (input == "d")
{
process = new processdelegate(divide);
}
Console.WriteLine("Results = {0}", process(param1,param2));
Console.ReadKey();
}
}
}
#8
Posted 24 September 2008 - 08:25 AM
all these while loops... why not just:
checkopt:
Console.WriteLine("What are we doing?\r\nm: Multiplying\r\n d: Dividing");
input = Console.ReadLine();
switch(input) {
case "m": goto Multiply; break;
case "d": goto Divide; break;
default: goto checkopt; break;
}
?
#9
Posted 24 September 2008 - 10:07 AM
not really what i meant...sorry, but they are not usually considered to be good coding practice and may be fased out due to the way oop is headed.
#10
Posted 24 September 2008 - 10:09 AM
Saying something is improper in C# makes me lol.
But goto s are usually considered bad coding.
Although they have their uses.
But goto s are usually considered bad coding.
Although they have their uses.
#11
Posted 24 September 2008 - 11:26 AM
Why do you hate .Net so much Methodz? Did you have a bad experience on it one time?
#12
Posted 24 September 2008 - 03:59 PM
goto's are like
runs the code at least once then keeps running till it gets it done, and from my experience, using while loops all over the place tends to make your program try to (not very much, but noticeably) use more resources than goto. Whatever your preference is though, I suppose.
do {
/blah blah
} while (condition)
runs the code at least once then keeps running till it gets it done, and from my experience, using while loops all over the place tends to make your program try to (not very much, but noticeably) use more resources than goto. Whatever your preference is though, I suppose.


Sign In
Create Account


Back to top









