We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Another C# question
Options

inchinga
Posts: 84 Forumite
in Techie Stuff
I saw that there are some bright sparks who know c# on here, and wondered if anyone would have a look at my code please:
Could anyone point me in the right direction from here? I especially don't know how to get the values the user enters for the size to be calculated into the formula for area. Thanks
Basically, my program has to calculate the area of a pizza, work out the cost if its £20 per square meter, and add a base cost to the price (2.5 for thin crust, £5 for medium, and £7.50 for thick crust).string[] pizza = new string[3]
{ "Thin Crust",
"Medium Crust",
"Thick Crust"};
Console.WriteLine("What type of pizza do you want? Enter number, ");
for (int i=1; i<4; i++)
Console.WriteLine("{0} {1}",i,pizza[i-1]);
int result = int.Parse(Console.ReadLine());
Console.WriteLine("You have chosen the {0} pizza", pizza[result - 1]);
Console.ReadLine();
{
float x;
A1: Console.Write("Please enter pizza size, between 200 and 600: (or zero to finish) ");
x = float.Parse(Console.ReadLine());
if ((x >= 200f) && (x <= 600f))
{
Console.WriteLine("Your number {0} is between 200 and 600", x);
Console.ReadLine();
else
{
Console.WriteLine("You did not enter a valid number: ");
Console.ReadLine();
goto A1;
}
}
using System;
public class ComputeCircle {
radius = 10.0;
area = radius * radius * 3.1416;
Console.WriteLine("Area is " + area);
}
Could anyone point me in the right direction from here? I especially don't know how to get the values the user enters for the size to be calculated into the formula for area. Thanks
0
Comments
-
I just have to run out and will be back in about 30 minutes0
-
Wouldn't you want the ComputeCircle bit to be a method in your existing program rather than a separate class. Not sure of the exact C# syntax but I understand it's similar to Java, so here's how I'd implement it in the latter
public float computeCircle(float radius){ return radius * radius * Math.PI; }
You'll have to find the C# equivalent of the Math.PI variable, it'll give more accurate results than just defining your own value.
Then from your main program you can call it using the inputted valuefloat price = computeCircle(x)*20;
And then round the figure to 2 decimal pieces to give you the proper price. Again, not sure of how to do this in C#, but you should be able to find it in the documentation.
Of course, you'll have to use consistent values throughout - I can't see anyone ordering a pizza that is 200 meters in radius! Plus, generally pizza sizes are specified in diameter, since that's easier for people to visualise.
Hope this helps point you in the right directions0 -
Oh, and get out of the habit of using goto as soon as possible, it generally shouldn't be used to control program flow. See this famous article by computer scientist E.W. Dijkstra (a name you'll no doubt come across a lot if you pursue your studies further).
For your purposes it's much better to implement a while loop0 -
What about having a Pizza class, with two properties - diameter and crust. The class could have a GetCost method which uses the diameter and cost to calculate and return the cost.
I haven't written any console apps for donkeys, but it would look something like:Pizza pizza = new Pizza; Console.WriteLine("What crust do you want?"); pizza.Crust = int.Parse(Console.ReadLine()); Console.WriteLine("What diameter pizza do you want?"); pizza.Diameter = int.Parse(Console.ReadLine()); Console.WriteLine(String.Format("Your pizza will cost: £{0}", pizza.GetCost())); Public Class Pizza { public int Crust {} public int Diameter {} public float GetCost() {} }
Could work, but I'll leave you to fill in the gory details...
PS syntax is probably wrong in there somewhere, but you get my drift0 -
Thank you all for looking at it. Am going to give all options a go to see what works.
:beer:0 -
I broadly agree with what's been said above. If I was doing it, I would change the following:
- Do not use 'GOTO', use a while loop instead. Also put in int.TryParse in case of invalid input
- Intitialise relevant variable to zero before while loop
- If invalid data (outside range or invalid) set variable to -1
- If -1 print your error message
- You don't need multiple classes, just use methods instead
- Method to get area
- Another method to turn area into cost
- Perhaps use a more descriptive name - to someone else looking at the code, or looking back, what does 'ComputeCircle' return? Something like 'ComputeArea', or even 'getAreaOfPizza' would be nicer.
Edit: Should say, there's nothing wrong with defining a separate pizza class either, as mentioned in another post, I meant you don't need a class to do things like calculate cost or the area of a circle, a method does just fine.0 - Do not use 'GOTO', use a while loop instead. Also put in int.TryParse in case of invalid input
-
Jeez, does everyone have to cheat on their homework these days?[greenhighlight]but it matters when the most senior politician in the land is happy to use language and examples that are simply not true.
[/greenhighlight][redtitle]
The impact of this is to stigmatise people on benefits,
and we should be deeply worried about that[/redtitle](house of lords debate, talking about Cameron)0 -
Jeez, does everyone have to cheat on their homework these days?Hi, we’ve had to remove your signature. If you’re not sure why please read the forum rules or email the forum team if you’re still unsure - MSE ForumTeam0
-
Jeez, does everyone have to cheat on their homework these days?
I dont think its too bad as long as long as you can understand the solution and you learn from the example.
However if you just copy and paste somthing you really dont understand, like this...[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]delegate [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]p[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] d); [/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Main([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] args)[/SIZE] [SIZE=2]{[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] pt = 0;[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ptl = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] {[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Thin"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Medium"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Thick"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}.ToList<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>();[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please enter a pizza size"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] v [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ptl) [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Write(pt+++[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]+v+[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2]pt = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Parse([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine());[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]p[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] m=(d)=>{[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] d+5.0;};[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]p[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] c=(pt==0)?(d)=>{[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] d+2.5;}:(((pt==1)?m:(d)=>{[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] d+7.5;}));[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Please enter a pizza diameter"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE] [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] di = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Parse([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine());[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]p[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] q=(d)=>{[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Math[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].PI*(d/2)*(d/2))*20;};[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Your pizza costs {0}"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], c(q(di)).ToString([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"c"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE] [SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ReadLine();[/SIZE] [SIZE=2]}[/SIZE] [/COLOR][/SIZE][/COLOR][/SIZE]
... you're probably going to get funny looks and quickly asked to explain it and then your really stuck.
(ps dont even try to understand that, its designed to be confusing and to use as many things you probably dont know about and dont need to know yet either)
If you dont understand OO prinicples id forget about creating new classes atm, go back see how methods work and basic control structures like "if" "while" "for" etc.
Then knock out some psuedocode as it will make it easier.0 -
I really want pizza nowSquirrel!If I tell you who I work for, I'm not allowed to help you. If I don't say, then I can help you with questions and fixing products. Regardless, there's still no secret EU law.
Now 20% cooler0
This discussion has been closed.
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 350.9K Banking & Borrowing
- 253.1K Reduce Debt & Boost Income
- 453.5K Spending & Discounts
- 243.9K Work, Benefits & Business
- 598.7K Mortgages, Homes & Bills
- 176.9K Life & Family
- 257.2K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.1K Discuss & Feedback
- 37.6K Read-Only Boards