Kim D
Kim D

Reputation: 21

Can't figure out why my If else/Else if statements are not working. C#

Trying to figure out why my else if/if else statements aren't executing properly. Every time I run it it skips the if statement and goes to the else if and also skips the else statement. It's not completed yet, trying to get the first category to work before I move on. But it's executable.

This is the portion that's giving me the trouble:

//tells user if they overspent, underspent, or broke even
        //Budget 1
        if (moneyBudget[0]>moneySpent1) {
            Console.WriteLine ("You overspent by $" + moneySpent1 + " this     week.");
        } else if (moneyBudget [0]<moneySpent1) {
            Console.WriteLine ("You underspent by $" + moneySpent1 + " this week.");
        } 
        else {
            Console.WriteLine ("You broke even this week.");
        }

`

In order to run here is the full code:

using System;

namespace Conditionals_Assignment
{
class MainClass
{
    public static void Main (string[] args)
    {
        /* 
         */
        //Explains to the user what this program is for and prompts for 3 categories
        Console.WriteLine ("Welcome to the Weekly Budget Calculator!\r\nThis calculator is designed to tell you if you overspent, underspent, or broke even \r\nfor your weekly budget in 3 different categories of spending. Please enter values without the $ sign.");
        Console.WriteLine ("Please enter the name of the first bill you want to budget for this week and press enter.");
        string budgetOneString = Console.ReadLine ();
        //make sure something is entered ant not left blank
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetOneString));
        if (string.IsNullOrWhiteSpace(budgetOneString)) {
            Console.WriteLine ("Please enter a value for category 1");
            //re-ask
            budgetOneString = Console.ReadLine();
        }
        //ask for name of second category budget
        Console.WriteLine ("Please enter the name of the second bill you want to budget for this week and press enter.");
        string budgetTwoString = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetTwoString));
        if (string.IsNullOrWhiteSpace(budgetTwoString)) {
            Console.WriteLine ("Please enter a value for category 2");
        budgetTwoString = Console.ReadLine ();
        }
        //asking for 3rd category budget
        Console.WriteLine ("Please enter the name of the third bill you want to budget for this week and press enter.");
        string budgetThreeString = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(budgetThreeString));
        if (string.IsNullOrWhiteSpace(budgetThreeString)) {
            Console.WriteLine ("Please enter a value for category 3");
        budgetThreeString = Console.ReadLine();
        }

        //set array for budget categories
        string [] budgetCat = new string[3];

        //fill array with 3 categories set by user
        budgetCat [0] = budgetOneString;
        budgetCat [1] = budgetTwoString;
        budgetCat [2] = budgetThreeString;

        //tells user what they input for categories
        Console.WriteLine ("You entered "+budgetCat [0]+", "+budgetCat[1]+", and "+budgetCat[2]+" for the three categories you would like to budget.");

        //Prompt user to input value for first named category
        Console.WriteLine("How much money did you budget for "+budgetCat [0]+"?");
        string moneyBudgetString1 = Console.ReadLine();
        Console.WriteLine (string.IsNullOrWhiteSpace(moneyBudgetString1));
        if (string.IsNullOrWhiteSpace(moneyBudgetString1)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString1 = Console.ReadLine ();
        }

        decimal money1 = decimal.Parse (moneyBudgetString1);
        Console.WriteLine ("How much money did you budget for " +budgetCat [1]+"?");
        string moneyBudgetString2 = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrWhiteSpace(moneyBudgetString2));
        if (string.IsNullOrWhiteSpace(moneyBudgetString2)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString2 = Console.ReadLine ();
        }
        decimal money2 = decimal.Parse (moneyBudgetString2);
        Console.WriteLine ("Mow much money did you budget for " + budgetCat [2] + "?");
        string moneyBudgetString3 = Console.ReadLine ();
        Console.WriteLine (string.IsNullOrEmpty(moneyBudgetString3));
        if (string.IsNullOrEmpty(moneyBudgetString3)) {
            Console.WriteLine ("Please enter a value.");
            moneyBudgetString3 = Console.ReadLine ();
        }
        decimal money3 = decimal.Parse (moneyBudgetString3);

        //set array with amounts entered
        decimal[] moneyBudget = new decimal[3];
        moneyBudget [0] = money1;
        moneyBudget [1] = money2;
        moneyBudget [2] = money3;

        //tells user what they input for categories
        Console.WriteLine("You entered $"+moneyBudget [0]+" for "+budgetCat [0]+", $"+moneyBudget [1]+" for "+budgetCat [1]+", and $"+moneyBudget [2]+" for "+budgetCat [2]+" for the amount of money your budgeted for each category.");

        //asks user how much they actually spent for eatch category
        Console.WriteLine("How much money did you actually spend for "+budgetCat [0]+"?");
        string moneyActualString1 = Console.ReadLine();
        decimal moneyActual1 = decimal.Parse (moneyActualString1);
        Console.WriteLine ("How much money did you actually spend for "+budgetCat [1]+"?");
        string moneyActualString2 = Console.ReadLine();
        decimal moneyActual2 = decimal.Parse (moneyActualString2);
        Console.WriteLine ("How much money did you actually spend for "+budgetCat [2]+"?");
        string moneyActualString3 = Console.ReadLine();
        decimal moneyActual3 = decimal.Parse (moneyActualString3);

        //set array for amounts entered
        decimal[] actNum = new decimal[3];
        actNum [0] = moneyActual1;
        actNum [1] = moneyActual2;
        actNum [2] = moneyActual3;

        //returns the values entered to the user
        Console.WriteLine("You entered that you actually spent these amounts for each category:\r\n"+budgetCat [0]+" = $"+actNum [0]+"\r\n"+budgetCat [1]+" = $"+actNum[1]+"\r\n"+budgetCat [2]+" = $"+actNum [2]+".");


        //calculates over,under or break even point
        decimal moneySpent1 = moneyBudget [0]-actNum [0];
        decimal moneySpent2 = moneyBudget [1]-actNum [1];
        decimal moneySpent3 = moneyBudget [2]-actNum [2];



        //tells user if they overspent, underspent, or broke even
        //Budget 1
        if (moneyBudget[0]>moneySpent1) {
            Console.WriteLine ("You overspent by $" + moneySpent1 + " this week.");
        } else if (moneyBudget [0]<moneySpent1) {
            Console.WriteLine ("You underspent by $" + moneySpent1 + " this week.");
        } 
        else {
            Console.WriteLine ("You broke even this week.");
        }





    }
}

}

Upvotes: 2

Views: 183

Answers (1)

Janne Matikainen
Janne Matikainen

Reputation: 5121

Add

Console.WriteLine("Hit any key to continue");
Console.ReadKey();

To the end of your Main method. You are not waiting for any input and the application will exit after last Console.WriteLine is executed.

Also you might want to consider changing the logic of determining under/over spending to.

//tells user if they overspent, underspent, or broke even
//Budget 1
if (moneyBudget[0] < actNum[0])
{
    Console.WriteLine("You overspent by $" + Math.Abs(moneyBudget[0] - actNum[0]) + " this week.");
}
else if (moneyBudget[0] > actNum[0])
{
    Console.WriteLine("You underspent by $" + Math.Abs(moneyBudget[0] - actNum[0]) + " this week.");
}
else
{
    Console.WriteLine("You broke even this week.");
}

Upvotes: 1

Related Questions