Does this server help with homework?

Hello, I'm new here and I would say that I'm new to C#, but I've been taking this course for about 4 weeks. Yet, I struggle very much with its concepts, I'm doing this assignment and I want to get as much done without looking up how other people have done the same assignment. Does this server allow for help or explanations on things like that?
28 Replies
Angius
Angius8mo ago
Does it help with homework? Absolutely, yes. Does it do homework for you? No, not a chance.
AppleCyder (NovaSprite)
Thank you Shall I make a new post dedicated to what I'm struggling with or can it be done here still?
Pobiega
Pobiega8mo ago
Can go ahead here
AppleCyder (NovaSprite)
Ok I'm trying to make it so that it asks the user for the base and exponent(power) then print the result
using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}

}
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
}

}
using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}

}
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
}

}
I want to keep
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
Angius
Angius8mo ago
And what is the issue?
AppleCyder (NovaSprite)
It doesn't work, I don't know where this should go
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
Angius
Angius8mo ago
It should go into a method Probably your Main method, as this is the first method that executes And your PowerFunction should be another method of the Program class Unless you did intend it to be a local function Things become more clearer with syntax highlighting and proper formatting:
using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
}

Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
}
}
using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
}

Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
}
}
As you can see, a good chunk of your code is inside of the class
AppleCyder (NovaSprite)
Yeah so I know that it won't work the way I did it Let me think What doesn't match between
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
int PowerFunction(int base, int power)
{
if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}
and
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);
Angius
Angius8mo ago
What do you mean "what doesn't match"?
AppleCyder (NovaSprite)
Like should they both be able to get me an output if done correctly or is there something in the console.write box that is preventing that from happening?
Angius
Angius8mo ago
The executable code should be inside of the main method Instead, it's inside of the class That is one of the issues $structure
MODiX
MODiX8mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius8mo ago
Classes can contain members. Fields, properties, methods They don't actually run, they don't execute, they're just containers Only methods inside of those classes run, execute If you want code to run, it must me inside of a method
Angius
Angius8mo ago
No description
Angius
Angius8mo ago
Code goes in method Between the {} braces
AppleCyder (NovaSprite)
I know it won't run but like this...
using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);

int PowerFunction(int base, int power)
{

if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}

}

}

}

using System;
namespace PowerNumber
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the base:");
base = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second exponent:");
power = Convert.ToInt32(Console.ReadLine());

double result = Math.Pow(base, power);
Console.WriteLine("\n\nThe sum of the two numbers is " + result);

int PowerFunction(int base, int power)
{

if (power != 0) return base * PowerFunction(base, power - 1);
else return 1;

}

}

}

}

Angius
Angius8mo ago
Yes, that's where code goes And methods/functions go into classes That's where PowerFunction should go The opposite move
AppleCyder (NovaSprite)
Ok So then I have to make it so that the integers equal to 0 right?
Angius
Angius8mo ago
Which integers and why? If you're asking if you need to do
int foo = 0;
foo = Convert.ToInt32(...);
int foo = 0;
foo = Convert.ToInt32(...);
then no, there's no need. It can just be
int foo = Convert.ToInt32(...);
int foo = Convert.ToInt32(...);
There's no need to assign a variable if you're gonna be reassigning it immediately after
AppleCyder (NovaSprite)
I was meaning this int = base 0;
Angius
Angius8mo ago
Here?
No description
Angius
Angius8mo ago
Here?
No description
AppleCyder (NovaSprite)
Yes and for power and result too Then maybe it could work in the order I had before
Angius
Angius8mo ago
If you want to declare a variable, yes, the type of it does need to be declared. So foo = 0; won't work, while int foo = 0; or var foo = 0; will It does not need to be assigned to immediately, though. As long as it is assigned to before you use it So
int foo;
if (something)
{
foo = 9;
}
else
{
foo = 172;
}
Console.WriteLine($"Number is {foo}");
int foo;
if (something)
{
foo = 9;
}
else
{
foo = 172;
}
Console.WriteLine($"Number is {foo}");
will work But
int foo;
if (something)
{
foo = 9;
}
Console.WriteLine($"Number is {foo}");
int foo;
if (something)
{
foo = 9;
}
Console.WriteLine($"Number is {foo}");
or
int foo;
Console.WriteLine($"Number is {foo}");
if (something)
{
foo = 9;
}
else
{
foo = 172;
}
int foo;
Console.WriteLine($"Number is {foo}");
if (something)
{
foo = 9;
}
else
{
foo = 172;
}
will not
AppleCyder (NovaSprite)
Oh ok I think that's gonna be it then But before I go I want for to ask for the base and exponent 5 times, will I need a loop for that?
Angius
Angius8mo ago
Ideally, yes You can just write the same code 5 times, sure, but a loop will make it easier
AppleCyder (NovaSprite)
Ok got it, ty so much goodbye
Angius
Angius8mo ago
Anytime :Ok:
Want results from more Discord servers?
Add your server