I feel stupid. I cant for the life of me figure out why this wont work. I'm very bad at c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I feel stupid. I cant for the life of me figure out why this wont work. I'm very bad at c#

https://code.sololearn.com/c9k6y7tVHqW1/?ref=app

13th Sep 2017, 5:26 PM
K1llJ0Y
K1llJ0Y - avatar
9 Answers
+ 23
You're not stupid, don't worry! Working with objects is not easy. First thing to note: When you call tipperAppRun(), you're calling it through the type tipperApp, and not the object you just made (runApp). tipperApp runApp = new tipperApp(); runApp.tipperAppRun(); Similarly, the same thing happened with myCalc - you called the type instead. tipCalc myCalc = new tipCalc(); tip = myCalc.tipCalcRun(tip, bill); Secondly, don't use Console.Read() for the bill. There's a method that can convert a string from Console.ReadLine() into a value, called x.Parse (x being the type to convert to). Surround Console.ReadLine() with Double.Parse(). bill = Double.Parse(Console.ReadLine()); Next, tipCalcRun() returns a value, but it isn't stored anywhere. Relatedly, tip is never assigned a value in the tipperApp class, and so it can't be printed. Save the returned value in tip. tip = myCalc.tipCalcRun(tip, bill); Lastly, because tip doesn't contain a value, it can't be passed to tipCalcRun(). But since you're returning a value for tip anyway, it isn't necessary. Just pass in the bill and return bill * 0.15. public double tipCalcRun(double bill) { return (bill * 0.15); } tip = myCalc.tipCalcRun(bill);
13th Sep 2017, 6:00 PM
Tamra
Tamra - avatar
+ 14
// This is a fix. Please compare. // The main problem is that you are confused with class objects and how to use them. // Taking class A for example, // A obj = new A(); // You have to use the class instance to call the class methods, not the class name, i.e. // obj.method(), instead of A.method(). using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace week3 { class tipCalc { public double tipCalcRun(double tip, double bill) { tip=bill*0.15; return tip;// calculates and returns the tip } } }//end of namespace namespace week3 { class tipperApp{ public void tipperAppRun(){ string restName; double tip = 0; double bill; Console.WriteLine("Enter restaraunt name: "); restName = Console.ReadLine(); Console.WriteLine("Enter bill total: "); bill = Console.Read();//input bill tipCalc myCalc = new tipCalc ();//creates the object for tipCalc tip = myCalc.tipCalcRun (tip, bill);//calls method tipCalcRun Console.WriteLine("You should tip: {0}", tip); } } } namespace week3 { class Program { static void Main(string[] args) { tipperApp runApp = new tipperApp(); runApp.tipperAppRun(); } } }
13th Sep 2017, 6:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
I wrote a fix, included comments of what's changed, and why. https://code.sololearn.com/cBANpY07NRlg/?ref=app
13th Sep 2017, 6:05 PM
Ipang
+ 3
@Tamra, your answer explained my mystery, I tried enter 200 for the bill, but get 7.5 for the tip, I'll update the fix to include your suggestion (bill input) (Edit) Double.Parse has been implemented. Currency format for bill & tip Thanks
13th Sep 2017, 6:10 PM
Ipang
+ 3
@K1llJ0Y Glad you found the solution with so many helps. Trust me you'll like the beauty of C# in future! 😉
13th Sep 2017, 11:37 PM
Zephyr Koo
Zephyr Koo - avatar
+ 2
Luka I made it that way because in the class we are using visual studio and are making different classes in different files so I made it that way so I'd remember
13th Sep 2017, 8:35 PM
K1llJ0Y
K1llJ0Y - avatar
+ 1
thank you lots. I'm a c++ coder myself and can actually work well with oop in c++. but I'm taking a college course for c# and he's pretty vague about things
13th Sep 2017, 6:27 PM
K1llJ0Y
K1llJ0Y - avatar
+ 1
finally figured it out after way too long. now have a final solution. what's funny is I tried it out in c++ to see how long it would take. 5 minutes flat so I guess I'm glad c++ exists and makes everything easier
13th Sep 2017, 8:34 PM
K1llJ0Y
K1llJ0Y - avatar
0
now as for formatting, as it is working with money, I need to format it to 2 decimal points
13th Sep 2017, 6:35 PM
K1llJ0Y
K1llJ0Y - avatar