Sunday, July 12, 2009

Help with C# code?

im having trouble with this code can anyone tell me what I'm doing wrong?





using System;





namespace JenkinsNutShop


{





class NutSales


{





static void Main()


{


// this is where I am listing the name of the business:


Console.WriteLine("JENKINS NUT SHOP");





string customerName;


double pounds;


double subTotal = 0;


string nutName = "";


char nutType;


double totalPrice;


double tax;














GetUserInput(out customerName, out nutType, out pounds);


CalcNutCosts(pounds, nutType, ref subTotal, ref nutName, out totalPrice, out tax);


DisplayReceipt(pounds,nutName,subTotal, tax, totalPrice);


}





public static void GetUserInput(out string customerName, out char nutType, out double pounds)


{


string userin;


// Prompting user for type of nuts they want to purchase:


Console.WriteLine(); //this is making a blank line


// Prompting user for the number of pounds they are ordering :


// Prompting user to input pounds of puchase





Console.Write("Customer name: ");


customerName = Console.ReadLine();


Console.WriteLine("What type of nuts would you like?");


Console.WriteLine("Please type an upper case letter or lower case letter to represent the nuts you wish to buy");


Console.WriteLine("Type in the letter \"C\" for Cashews");


Console.WriteLine("Type in the letter \"A\" for Almonds");


Console.WriteLine("Type in the letter \"P\" for Peanuts");


Console.WriteLine("Type in the letter \"W\" for Walnuts");


Console.WriteLine(); // enters a black line


Console.Write("Nut Type: ");


userin = Console.ReadLine();


nutType = Convert.ToChar(userin);


Console.Write("How many pounds would you like? ");


userin = Console.ReadLine();


pounds = Convert.ToDouble(userin);

















}





public static void CalcNutCosts(double pounds, char nutType, ref double subTotal, ref string nutName, out double totalPrice, out double tax)


{


const double CASHEWS = 6.50;


const double ALMONDS = 7.25;


const double PEANUTS = 2.39;


const double WALNUTS = 2.79;





switch(nutType)


{


case'C':


case'c':





subTotal = pounds * CASHEWS;


nutName = "Cashew";


break;





case'A':


case'a':


subTotal = pounds * ALMONDS;


nutName = "Almonds";


break;





case'P':


case'p':


subTotal = pounds * PEANUTS;


nutName = "Peanuts";


break;








case'W':


case'w':


subTotal = pounds * WALNUTS;


nutName = "Walnuts";


break;





tax = subTotal * .0675;


totalPrice = subTotal + tax;














}


}


























public static void DisplayReceipt(double pounds,string nutName,double subTotal, double tax, double totalPrice)


{








Console.WriteLine();


Console.WriteLine();





Console.WriteLine("Your receipt:");


Console.WriteLine();





Console.WriteLine("{0} pounds of {1} nuts. Subtotal \t {2:C}", pounds, nutName, subTotal);


Console.WriteLine(" Tax \t {0:C}",tax);


Console.WriteLine(" Total \t {0:C}",totalPrice);





Console.WriteLine();


Console.WriteLine("Thank you for shopping at Jenkins Nut Shop \n \n");


}


}


}

Help with C# code?
As previously noted, no one wants to wade through code like this.





Please provide any specific error messages or things that are happening that you don't want to have happen.





And please, post your code on a Web server:





http://www.dougv.com/blog/2006/12/13/a-r...
Reply:One problem i notice is that in your CalcNutCosts function these statements are unreachable:


tax = subTotal * .0675;


totalPrice = subTotal + tax;





because you have your break; statement above them but you have the closing } for the switch statement after them and since you used the parameter modifier out on tax and totalPrice you will get an error because you are unable to set their values. Try moving the closing } of your switch statement after the break; but before where you set the value of tax and totalPrice like this:





case'W':


case'w':


subTotal = pounds * WALNUTS;


nutName = "Walnuts";


break;





}





tax = subTotal * .0675;


totalPrice = subTotal + tax;





}
Reply:That's a lot of code to eye over. Any specifics on the issue?


No comments:

Post a Comment