|
Tada!
Took me long enough!
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
double valOne = AcceptValue("Enter your first number:");
double valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
Console.WriteLine(message);
string valueOne = Console.ReadLine();
int IntOne = Convert.ToInt32(valueOne);
return IntOne;
}
}
}
|
|
|
|
|
Nice and tidy.
You also need to understand that AcceptValue is returning a variable of type int (an integer), which you are storing in a variable of type double , which is floating point. In this case it's not a big problem, the compiler will merely convert it to double before storing it. However, going the other way from double to int can lead to strange results. for the moment don't think about it.
Use the best guess
|
|
|
|
|
Yeah, I was just playing around with the different data types to see what kind of output I would get…
I guess I forgot to change it back to an int datatype before posted…
I tried playing around with the do while loop… I'm going to have to study that much more before I try to implement it in anything.
However, my if statements are acting a little funny…
I couldn't get the regular do while loop to work quite right either, I'll try to play around with a little bit later…
When I answered yes to both numbers, it does fine and does the calculation…
I entered the first number and then answer no, it starts over like it's supposed to…
Then I go to enter the first number again, this time I answered yes…
it moves like it's supposed to…
I go to enter the second number and answer no, it starts the second number over like it's supposed to…
then I answered the second number again, this time answering yes… For some reason it looks like the first digit is cut off
For both numbers I used the number 5, in this particular scenario… When it's working, the answer should be 3125…
only 125 shows up when I answer no for the second question, the first time…
enter number 5
y
enter number 2
n
enter second number again 5
calculates and shows 125
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
int valOne = AcceptValue("Enter your first number:");
int valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
Console.WriteLine(message);
string valueOne = Console.ReadLine();
int IntOne = Convert.ToInt32(valueOne);
Console.WriteLine("Are you sure you want to use that number?\nPress lowercase y or n, to continue.");
string YesNo = Console.ReadLine();
if (YesNo == "y")
return IntOne;
else
{
Console.Clear();
AcceptValue(message);
}
return IntOne;
}
}
}
|
|
|
|
|
Aaarrgh!!!
Do you sense my frustration? You must not call AcceptValue from within itself - it does not do what you think it's doing. And even if it was, you are not storing its returned value. The only way to repeat the sequence until you get a valid number is by using a loop as I showed you before.
Use the best guess
|
|
|
|
|
I need a beer now...
using System;
namespace CalculatetothePowerof
{
class Program
{
static void Main()
{
int valOne = AcceptValue("Enter your first number:");
int valTwo = AcceptValue("\nEnter your second number:");
Console.WriteLine(Math.Pow(valOne, valTwo));
Console.Read();
}
static int AcceptValue(string message)
{
string valueOne;
int IntOne;
string YesNo;
do
{
Console.WriteLine(message);
valueOne = Console.ReadLine();
IntOne = Convert.ToInt32(valueOne);
Console.WriteLine("Are you sure you want to use that number?\nPress lowercase y or n, to continue.");
YesNo = Console.ReadLine();
if (YesNo == "y")
{
break;
}
Console.Clear();
} while (YesNo != "y");
return IntOne;
}
}
}
|
|
|
|
|
Excellent!
Use the best guess
|
|
|
|
|
I had to step away from my laptop for a day… That literally made me mentally tired.
Before I became a quadriplegic in '98, I was a blue-collar monkey with calluses all over my hands and feet. I've always been using my gray matter, even in my blue-collar days. Always started out at the bottom of the totem pole, eventually working my way up to some sort of management position.
Learning this programming language, kind of brings me back to my elementary days in school.
Sir Richard, I greatly appreciate the time you took to help me figure some of this out.
By the way, I did eventually open the book suggested and started reading some. The book I bought for $85 , I'm going to continue and finish that book first before I pick your book up again (the book you suggested).
They both start out fairly similar in trying to explain about the .Net framework and trying to give you a little history about the programming language C#.
$85 is why I'm going to continue, read, finish the other book first. Both books are about 350 pages, I'm already 100 pages into the book I bought. I only got about 20 pages into the book you suggested. That's when I realized that I should stop reading that book and continue with the other one, they both started off fairly similar and they're both about the same length.
I will definitely take your book up again, there were a few things in there that my book did not mention that. So, it is a good resource to have.
Again, thank you for your patience and help Sir Richard.
|
|
|
|
|
Happy to help, and good luck with your reading. Remember, this forum is always open, and there are lots of people offering assistance.
Use the best guess
|
|
|
|
|
Rob, I see several problems in your code here. Let's break down some of the more apparent errors (I'm ignoring style issues such as using Pascal casing for variables):
string valueOne = Console.ReadLine();
int IntOne = 0;
bool result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed.", IntOne);
AcceptValueOne();
} In this section of code, you call AcceptValueOne again, and you aren't returning anything from it. The issue you have here is that you are recursively calling this method is completely unecessary, and also will return 0 to the calling function if the condition fails the first time (also, you are using the failed conversion in your WriteLine). This could easily be solved with a loop instead:
bool result = false;
do
{
string valueOne = Console.ReadLine();
result = Int32.TryParse(valueOne, out IntOne);
if (!result)
{
Console.WriteLine("Attempted conversion of '{0}' failed, please try again.", valueOne);
}
} while (!result); As you can see, the test is straightforward and there's no recursion. Well, as you can see, the same logic applies in cases where you are doing your tests for the y/n combination.
Another problem with your code is that you have two paths to AcceptValueTwo - the one in AcceptValueOne is completely useless because you aren't doing anything with the value. You're actually getting this value elsewhere (by the way, your code won't compile at the moment because you have a class level variable that's calling a method - and this is something you can't do; also, your valOne and valTwo members aren't static which means they won't be visible in any of your static methods).
|
|
|
|
|
I appreciate you replying and trying to help… Making the call back to the first method if the user enters "n" or anything else, does turn the variable `zero…
Which is what I wanted…
I realize and understand there is probably a better way to do it, but… So you understand where I'm coming from and what I'm trying to accomplish and do… Please read my reply that I made to Richard MacCutchan
|
|
|
|
|
Rob, the reason I have shown you that you need corrections is because the underlying logic in your code is flawed. No amount of fiddling is going to get around the fact that you have paths through your code that return you unexpected values. If you try and simplify your code the way I have shown you, you should find it easier.
|
|
|
|
|
I'm learning this stuff at my pace… Not your pace.
Feel free not to comment on any more of my posts…
|
|
|
|
|
Rob, as I've tried to offer you advice which is intended to help you, I find that attitude highly offensive. Your code has logic problems in it, and there are easy fixes, which I attempted to point out. If you are this hostile to people who try to help you improve, then I would suggest that you need to take a long hard look at whether or not you really want to get better at this.
|
|
|
|
|
Hello All,
My windows form application which allows users to import, edit, and save images works for the most part. However, it lacks many features that I would like to have but I have no idea on how to go about creating them. On top of the list of features I'd like to have is the ability to create images on separate layers. Right now I can put paint on top of an image or lay one image on top of another image but once I do that I can not separate them since they are on the same layer. Any suggestion will be greatly appreciated thanks in advance.
|
|
|
|
|
You would need to store each layer as a separate entity. When your persisted "image file" is read by your app, it should render the layers in order. In practice, every layer object will have its own properties (e.g. opacity, visibility, etc.)
/ravi
|
|
|
|
|
You should move to WPF. WPF already implements all this for you.
|
|
|
|
|
How can C # Assistance WindowsFormsApplication1 save a text document in pdf-for example, in
The second question
Notify me when someone who has a calendar to "pay something" for example ...
I can not understand how the code should be written
|
|
|
|
|
ghost22th wrote: save a text document in pdf-for example Try iTextSharp[^].
ghost22th wrote: Notify me when someone who has a calendar to "pay something" for example Check the date and compare it with the due date in your calendar/database.
Use the best guess
|
|
|
|
|
Dear
am using gmail smtp means mail send ..working good but i have change smtp name username password nic.in means did not send mail
|
|
|
|
|
|
|
You've replied to the wrong person. You should have posted this for the OP. He doesn't get notified if you reply to anything other than his post.
|
|
|
|
|
|
|