Beginner with a easy question

Updated on September 22, 2017 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on September 20, 2017

Having some issues with this tipping app i’m trying to make. Any help would be appreciated, not really sure why i’m not getting anything back in the tipAmount or totalBill variables i have. i didn’t add the main method here but i called all the necessary classes in Main for this to work in the console. 

 

namespace Week4HomeworkHelper
{
class TipperClass
{

string restName;
string pretipAmount;
string tipPercent;
double num01;
double num02;
double tipAmount;
double totalBill;

public TipperClass()
{
Intro();
Console.ReadKey();
Console.Clear();

Inputstorage();

}
public void Intro()
{

Console.WriteLine(“This App was made to find out the tip amount of a bill using percentage from 1%-50%”);
Console.WriteLine(“Press any key to continue”);

}

public void Inputstorage()
{
TipCalc();

Console.WriteLine(“Please enter the name of the restaurant”);
restName = Console.ReadLine();
Console.WriteLine(“Please enter the total amount of your bill (e.i 20.00)”);
pretipAmount = Console.ReadLine();
Console.WriteLine(“Please enter the tip percentage you would like to leave (i.e 15.00 %)”);
tipPercent = Console.ReadLine();
Console.WriteLine(“your tip amount is ” + tipAmount);
Console.WriteLine(“your total bill with tip is ” + totalBill);

Console.ReadKey();
}
public void TipCalc()
{

num01 = Convert.ToDouble(pretipAmount);
num02 = Convert.ToDouble(tipPercent);
tipAmount = (num01 * num02) * 0.01;
totalBill = num01 + tipAmount;
}

}
}

  • Liked by
Reply
0 on September 22, 2017

I’m guessing it’s this line:

num02 = Convert.ToDouble(tipPercent);

I’m no expert with convert, but tipPercent is a stored string value with a ‘%’ character in it, and not just numbers and a decimal, so it’s possible that it isn’t successfully converting tipPercent to what you want, thus leaving num02 defaulted to 0.00 which would make your tip amount zero dollars and zero cents.

Here’s one quick solution you could try:

num01 = Convert.ToDouble(pretipAmount);
 
char[] charsToTrim = {'%', ' '};
string tipPercentOnlyNumbers = tipPercent.TrimEnd(charsToTrim); // gets rid of any spaces and  percents if they exist in user input
 
num02 = Convert.ToDouble(tipPercent);

Hopefully that will solve your issue, but it’s currently untested

 

ToDouble Source (this shows an example or two of strings which don’t get converted to doubles because they have other characters in them)

 TrimEnd source

  • Liked by
Reply
Cancel