+ 2
I feel stupid. I cant for the life of me figure out why this wont work. I'm very bad at c#
10 Respostas
+ 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);
+ 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();
}
}
}
+ 5
I wrote a fix, included comments of what's changed, and why.
https://code.sololearn.com/cBANpY07NRlg/?ref=app
+ 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
+ 3
@K1llJ0Y Glad you found the solution with so many helps. Trust me you'll like the beauty of C# in future! 😉
+ 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
+ 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
+ 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
0
now as for formatting, as it is working with money, I need to format it to 2 decimal points