0

I'm making a Console application and I have one while loop to restart the whole program when the paycheck is calculated and another while loop to re-asks the questions about what type of paycheck the user receives if the input is wrong. But when I enter the incorrect option the question is asked again and the whole program restarts too. Is there a way to have the second while loop react separately from the first while loop?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WageCalc
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables
            string hourWorkedInput;
            string wageInput;
            double hourlyWage = 0.00;
            int hoursWorkedAWeek = 0;
            double payCheck;
            bool wageCalculated = false;
            bool AskQuestion = true;


            //Actual Code
            while (wageCalculated == false)
            {
                Console.WriteLine("Welcome to the Monthly Wage Calculator");
                Console.WriteLine("Please enter how much you are paid an hour");
                wageInput = Console.ReadLine();
                hourlyWage = Double.Parse(wageInput);
                Console.WriteLine("Please enter how many hours you work a week");
                hourWorkedInput = Console.ReadLine();
                hoursWorkedAWeek = Int32.Parse(hourWorkedInput);
                Console.WriteLine("Do you get paid (W)eekly or (B)iweekly?");
                string often = Console.ReadLine();

                //Repeats if input is incorrect
                while(AskQuestion)
                {
                    if (often == "W")
                    {
                        payCheck = hoursWorkedAWeek * hourlyWage;
                        Console.WriteLine("You will make {0} on every paycheck", payCheck);
                        Console.WriteLine("Would you like to Calculate another Paycheck?");
                        string repeat = Console.ReadLine();
                        switch (repeat)
                        {
                            case "Y":
                                wageCalculated = false;
                                break;
                            case "N":
                                wageCalculated = true;
                                break;
                            default:
                                wageCalculated = true;
                                break;
                        }
                    }
                    else if (often == "B")
                    {
                        payCheck = (hoursWorkedAWeek * 2) * hourlyWage;
                        Console.WriteLine("You will make {0} on every paycheck", payCheck);
                        Console.WriteLine("Would you like to Calculate another Paycheck?");
                        string repeat = Console.ReadLine();
                        switch (repeat)
                        {
                            case "Y":
                                wageCalculated = false;
                                break;
                            case "N":
                                wageCalculated = true;
                                break;
                            default:
                                wageCalculated = true;
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please pick W or B");
                        AskQuestion = false;
                    }
                }
            }
        }
    }
}
3
  • So much unneeded code, wow Commented May 27, 2018 at 8:04
  • Please, if the comment isn't intended to give some sort of help do not post it. Thank you. Commented May 27, 2018 at 20:04
  • It is, I'm letting you know that we don't want to have to sort through your code to find the important bits. Do us a favor and help us help you, don't just copy-pasta your code from your IDE in here, that's lazy and rude. Commented May 27, 2018 at 21:26

1 Answer 1

2

Or you could make it a little more fun and simple

private static void Main(string[] args)
{

   //Actual Code
   while (true)
   {
      Console.WriteLine("Welcome to the Monthly Wage Calculator");
      Console.WriteLine("Please enter how much you are paid an hour");

      double hourlyWage;
      while (!double.TryParse(Console.ReadLine(), out hourlyWage))
         Console.WriteLine("You had one job, now answer the question properly");

      Console.WriteLine("Please enter how many hours you work a week");

      double hoursWorkedAWeek;
      while (!double.TryParse(Console.ReadLine(), out hoursWorkedAWeek))
         Console.WriteLine("You had one job, now answer the question properly");

      Console.WriteLine("Do you get paid (W)eekly or (B)iweekly?");
      var often = Console.ReadLine();
      while (often != "W" && often != "B")
      {
         Console.WriteLine("You had one job, now answer the question properly");
         often = Console.ReadLine();
      }

      if (often == "W")
         Console.WriteLine($"You will make {hoursWorkedAWeek * hourlyWage} on every paycheck");
      else if (often == "B")
         Console.WriteLine($"You will make {hoursWorkedAWeek * 2 * hourlyWage} on every paycheck");

      Console.WriteLine("Would you like to play again Y/N?");

      var repeat = Console.ReadLine();
      while (repeat != "Y" && repeat != "N")
      {
         Console.WriteLine("You had one job, now answer the question properly");
         repeat = Console.ReadLine();
      }

      if (repeat == "N")
      {
         Console.WriteLine("Suit yourself, goodbye");
         break;
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a problem with declaring variables at the top or is it just personal preference?
@Recht88 No problem, just makes the code cleaner in some peoples opinions

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.