0

What I have here is a small console app I'm wanting the user to type in their monthly salary then their "Expenses" To calculate how much money they have over the month once they take away their expenses (A calculator that tells the user how much money they have monthly once all bills are paid). I'd like to take away from int Salary. I want the expenses to keep populating until the user types "false", in the bool FinishedAdding currently the variable Expenses only holds one value, I want to add all Expenses then subtract from Salary. Am I doing this correctly or is this the wrong approach?

string NewLine = "\n";
bool Finished = false;
var Expenses = default(int);

Console.WriteLine("Enter you earn a month (after tax");
int Salary = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(NewLine);

if (Finished != true)
{
    while (Finished == false)
    {
        Console.WriteLine("What are your expenses");
        Expenses = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("You are finished?");
        bool FinishedAdding = Convert.ToBoolean(Console.ReadLine());

        if (FinishedAdding == true)
        {
            break;
        }
    }
}

Console.WriteLine(NewLine);
Console.WriteLine("Your total is: " + (Expenses - Salary));
2
  • You need a new variable called TotalExpenses that adds up everytime a new expense is written. Commented Mar 11, 2020 at 12:10
  • 1
    You don't need if( Finished != true ). If it is false, the while won't be entered. 2.) You want to subtract Expenses from Salary, why do you then subtract Salary from Expenses? ... Commented Mar 11, 2020 at 12:12

4 Answers 4

2

Couple changes I made:
1) Most importantly: Expenses += will add what they enter each time to the amount they entered previously. This will be your total expenses that you can then subtract from Salary.
2) Instead of using a separate variable for Finished, just set the Finished variable to whether or not they enter "true" or "false".
3) No need for an if and break statement, just let the while criteria check the Finished variable.

string NewLine = "\n";
bool Finished = false;
var Expenses = default(int);

Console.WriteLine("Enter you earn a month (after tax)");
int Salary = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(NewLine);

while (Finished == false)
{
    Console.WriteLine("What are your expenses");
    Expenses += Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("You are finished?");
    Finished = Convert.ToBoolean(Console.ReadLine());
}

Console.WriteLine(NewLine);
Console.WriteLine($"Your total is: {(Salary - Expenses)}");
Sign up to request clarification or add additional context in comments.

2 Comments

Now it's identical to Jeroen's answer. Could you add some explanations, so OP can understand, why you made the changes that you did?
Explanation added. We posted our answers at the same time.
2

Let's implement the routine step by step. We can start from reading decimal (which better fits financial data like Salary)

// Either enter valid decimal value or press enter (for exit)
private static bool TryReadDecimalOrExit(string title, out decimal value) {
  value = 0m;

  while (true) {
    if (!string.IsNullOrWhiteSpace(title)) 
      Console.WriteLine(title);

    string input = Console.ReadLine();

    if (string.IsNullOrWhiteSpace(input))
      return false;

    if (decimal.TryParse(input, out value))
      return true;

    Console.WriteLine("Sorry, invalid value. Please, try again");
  }
}

private static decimal ReadDecimal(string title) {
  while (true) {
    if (!string.IsNullOrWhiteSpace(title)) 
      Console.WriteLine(title);

    string input = Console.ReadLine();

    if (decimal.TryParse(input, out value))
      return value;

    Console.WriteLine("Sorry, invalid value. Please, try again");
  }
}

Time to loop:

   decimal Salary = ReadDecimal("Enter you earn a month (after tax");

   Decimal Expenses = 0m;

   // While not exited, ask for a new expense
   while (TryReadDecimalOrExit("What are your expenses", out var expense)) 
     Expenses += expense;

   Console.WriteLine("Your total is: {Salary - Expenses:c2}");

Comments

2

With

Expenses = Convert.ToInt32(Console.ReadLine());

you are assigning a value to Expenses with each iteration. Since the expenses of the user do not equal the last amount they spent, but the summed amount, you'd have to sum up the expenses

Expenses = Expenses + Convert.ToInt32(Console.ReadLine());

This can be simplified with +=, which is basically "add a value to the contents of a variable and assign the new value to the variable". This yields

Expenses += Convert.ToInt32(Console.ReadLine());

On a side note

There is no error handling. Your program will crash if I enter e.g. ei19 as the amount. While the answer of Dmitry provides an approach to error handling in your program, it will exit as soon as you type something that is not a number. You might want to check whether the input is valid ans display an error message

while(!Finish)
{
    var input = ReadInput("Your message");

    if(ShouldExit(input))
    {
        Finish = true;
    }
    else if(IsValidAmount(input))
    {
        Expenses = input.Amount;
    }
    else
    {
        WriteErrorMessage("Your error message");
    }
}

if input being of type UserInput for example

class UserInput
{
    // ...

    public bool Finished { get; }

    public Decimal Amount { get; }
}

just for the gist of it.

Comments

0
string NewLine = "\n";
    bool Finished = false;
    var Expenses = default(int);

    Console.WriteLine("Enter you earn a month (after tax");
    int Salary = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(NewLine);

    while (!Finished)
    {
        Console.WriteLine("What are your expenses");
        Expenses += Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("You are finished?");
        Finished = Convert.ToBoolean(Console.ReadLine());
    }


    Console.WriteLine(NewLine);
    Console.WriteLine("Your total is: " + (Salary - Expenses));

3 Comments

Could you please elaborate how and why this solves the OPs issue?
Expenses gets added up instead of updated and expenses are subtracted from salary, but OP should really be learning how while loops work.
That comment ^^ should be part of your answer. Just throwing (working) code at someone who obviously has difficulty understanding code (or new to programming in general or both) is ... let's say "suboptimal".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.