Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
SuggestionRe: Properties vs Methods Opinions sought Pin
Richard Deeming10-May-13 5:16
mveRichard Deeming10-May-13 5:16 
GeneralRe: Properties vs Methods Opinions sought Pin
Keith Barrow10-May-13 5:37
professionalKeith Barrow10-May-13 5:37 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 7:58
jschell10-May-13 7:58 
AnswerRe: Properties vs Methods Opinions sought Pin
jschell10-May-13 7:50
jschell10-May-13 7:50 
GeneralRe: Properties vs Methods Opinions sought Pin
Ennis Ray Lynch, Jr.10-May-13 8:41
Ennis Ray Lynch, Jr.10-May-13 8:41 
GeneralRe: Properties vs Methods Opinions sought Pin
jschell13-May-13 7:58
jschell13-May-13 7:58 
AnswerRe: Properties vs Methods Opinions sought Pin
PIEBALDconsult10-May-13 18:32
mvePIEBALDconsult10-May-13 18:32 
GeneralPracticing Code Logic Pin
N8tiv10-May-13 3:24
N8tiv10-May-13 3:24 
Okay, I have a Drill/Exercise/Homework Assignment that my book has asked me to complete.
It seems fairly simply enough, from a end-user point of view.
(Probably easy for you old pros, I'm just beginning my coding career)

Anyway, here is my assignment:
"Drill 4-2
Write a C# program to compute the value of a number raised to the
power of a second number. Read the two numbers from the keyboard"

The first time I thought about doing this, just assigning two variables (integers) and assigning them a predefined value.
I would enter those values into the console window and get my answer.
I thought about doing it that way just to satisfy the homework assignment.
This would also assume I would be the only one using this and that was the only one that knew what values to enter.
Not very interactive and lazy after I thought about it.

So, I wrote down an outline and started following it. After a while, I found myself breaking away and changing things a little bit and adding on to it.
Now, keep this in mind… I'm just beginning, I'm just a newbie.
I have learned some shortcut methods, but… They're still just a little bit cryptic to me.

I also tried to comment as well as I could to explain what I was doing and thinking.

Down below is my code and what I have so far.

There is only three things (that I need to know right now, I think).

For right now, I just want to keep this as simple as possible.

I know I'm going to have to call the Main method.

How do I programmatically clear the screen and call the Main method to start the program over?
(We haven't covered this in my course/book yet)
Those were the first two things I need to know.

Third thing:
I probably should create another method for the second user input?

Okay, a few more things pop up in my head.

if the answer to the second variable/user input is incorrect and answer no, will the first variable/user input still be usable if I have to start the second question over?
It will still be in memory right?

I know I'm going to have to change my code towards the bottom if I create a new method for the second question//variable/user input.


<pre lang="c#">

using System;
using System.Text;

namespace CalcPowerOfNums
{
class Program
{
//Declaring the two main string variables to be used in our calculation.
string firstUserString;
string secondUserString;

static void Main(string[] args)
{
//Ask the user for the first number.
Console.WriteLine("Enter your first number and press the Enter/Return key");
string firstUserString = Console.ReadLine();

//Make sure this number is correct.
Console.WriteLine("You want to find the power of {0}?\n" , firstUserString);

//Declaring, Initializing string variables for user answer.
string firstAnswer = "";

//Make user confirm or deny their choice.
Console.WriteLine("Press the lowercase letter y for yes");
Console.WriteLine("Press the lowercase letter n for no");
Console.ReadKey();

//If user answer is yes, move on… It user answer is no, start program over.
do
{
if (firstAnswer == "y")
continue;
if (firstAnswer == "n")

}
//Ask the user for the second number.
Console.WriteLine("\nEnter your second number and press the Enter/Return key");
string secondUserString = Console.ReadLine();

//Make sure this number is correct.
Console.WriteLine("You want to use this number to find the power of {0}?\n", firstUserString);

//Declaring, Initializing string variables for user answer.
string secondAnswerYes = "y";
string secondAnswerNo = "n";

//Make user confirm or deny their choice.
Console.WriteLine("Press the lowercase letter y for yes");
Console.WriteLine("Press the lowercase letter n for no");
Console.ReadKey();

//Check to see if both first and second answer is yes.
if (firstAnswerYes == secondAnswerYes)
{
//Declare integers and assign the first and second user strings
//Then convert strings into integers
//Then use integers to calculate
//Then print the answer to screen

int firstUserInt = Convert.ToInt32(firstUserString);
int secondUserInt = Convert.ToInt32(secondUserString);
Console.WriteLine(Math.Pow(firstUserInt, secondUserInt));

}
Console.ReadKey();
}
}
}

</pre>

<a href="http://www.widmarkrob.com">My Coding Journey</a>
GeneralRe: Practicing Code Logic Pin
Richard MacCutchan10-May-13 3:48
mveRichard MacCutchan10-May-13 3:48 
Questionxmpp Pin
Member 1004242210-May-13 2:20
Member 1004242210-May-13 2:20 
AnswerRe: xmpp Pin
Pete O'Hanlon10-May-13 2:34
mvePete O'Hanlon10-May-13 2:34 
AnswerRe: xmpp Pin
Richard MacCutchan10-May-13 3:44
mveRichard MacCutchan10-May-13 3:44 
QuestionAbout xmpp n bosh server Pin
Member 1004242210-May-13 1:46
Member 1004242210-May-13 1:46 
AnswerRe: About xmpp n bosh server Pin
Pete O'Hanlon10-May-13 2:13
mvePete O'Hanlon10-May-13 2:13 
QuestionExport to PST Pin
JD869-May-13 14:29
JD869-May-13 14:29 
QuestionProper Use of a Semaphore Pin
Richard Andrew x649-May-13 14:14
professionalRichard Andrew x649-May-13 14:14 
AnswerRe: Proper Use of a Semaphore Pin
SledgeHammer019-May-13 17:05
SledgeHammer019-May-13 17:05 
GeneralRe: Proper Use of a Semaphore Pin
Richard Andrew x6410-May-13 6:10
professionalRichard Andrew x6410-May-13 6:10 
Questionhow do i display http fields Pin
mais irreem9-May-13 3:35
mais irreem9-May-13 3:35 
AnswerRe: how do i display http fields Pin
OriginalGriff9-May-13 3:54
mveOriginalGriff9-May-13 3:54 
QuestionHTML, RTF or WordML? Pin
Jassim Rahma8-May-13 23:47
Jassim Rahma8-May-13 23:47 
AnswerRe: HTML, RTF or WordML? Pin
Abhinav S9-May-13 0:33
Abhinav S9-May-13 0:33 
AnswerRe: HTML, RTF or WordML? Pin
Dave Kreskowiak9-May-13 1:40
mveDave Kreskowiak9-May-13 1:40 
QuestionLogin Form in Visual studio 2008 c# Pin
Akash Bharatvala8-May-13 23:18
Akash Bharatvala8-May-13 23:18 
AnswerRe: Login Form in Visual studio 2008 c# Pin
Abhinav S9-May-13 0:23
Abhinav S9-May-13 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.