Home > OS >  C# -value getiing reset to 0 after the loop
C# -value getiing reset to 0 after the loop

Time:07-15

I have a coffee ordering program. The issue is that the total bill gets reset after this loop.

Note: I didn't add the entire code so it doesn't get too much, but if you want to get the reference then please tell me.

invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string Anything = Console.ReadLine();
            string UpperCaseAnything = Anything.ToUpper();
            if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
            {
                invalidChoice = false;
            }
            else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
            {
                return;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

What's happening is that if I enter "no," it will bring the menu up again, but I don't want to reset the added total value. But in this, the total amount that was getting added up until here gets reset if I enter "no." I don't know what's wrong—why the amount is getting reset here.

Here's the amount method.

private int askForCoffee()
    {
        int totalCoffeeCost = 0;
        bool invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
            Console.Write(" ");
            int CoffeeChoice = int.Parse(Console.ReadLine());

            switch (CoffeeChoice)
            {
                case 1:
                    totalCoffeeCost  = 2;
                    invalidChoice = false;
                    break;
                case 2:
                    totalCoffeeCost  = 5;
                    invalidChoice = false;
                    break;
                case 3:
                    totalCoffeeCost  = 7;
                    invalidChoice = false;
                    break;
                default:
                    Console.WriteLine("");
                    Console.WriteLine(" Please enter a valid choice");
                    break;
            }
        }
        return totalCoffeeCost;
    }

Update

Here is the whole code

using System;

/* A simple coffee ordering program
 * used switch,if else and lable*/

class Program
{
    public static void Main()
    {
        var exiting = false;
        while (!exiting)
        {
            Program p = new Program();
            p.programLoop();
        }
    }

    private int askForCoffee()
    {
        int totalCoffeeCost = 0;
        bool invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
            Console.Write(" ");
            int CoffeeChoice = int.Parse(Console.ReadLine());

            switch (CoffeeChoice)
            {
                case 1:
                    totalCoffeeCost  = 2;
                    invalidChoice = false;
                    break;
                case 2:
                    totalCoffeeCost  = 5;
                    invalidChoice = false;
                    break;
                case 3:
                    totalCoffeeCost  = 7;
                    invalidChoice = false;
                    break;
                default:
                    Console.WriteLine("");
                    Console.WriteLine(" Please enter a valid choice");
                    break;
            }
        }
        return totalCoffeeCost;
    }


    private void programLoop()
    {
        int TotalCoffeeCost = 0;

        TotalCoffeeCost  = askForCoffee();

        var invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
            Console.Write(" ");
            string UserChoice = Console.ReadLine();
            string upperCaseChoice = UserChoice.ToUpper();

            if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
            {
                TotalCoffeeCost  = askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
            }
            else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
            {
                invalidChoice = false;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string Anything = Console.ReadLine();
            string UpperCaseAnything = Anything.ToUpper();
            if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
            {
                invalidChoice = false;
            }
            else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
            {
                return;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
            Console.WriteLine("");
            Console.WriteLine(" Please pay the amount by entering the amount bellow");
            Console.Write(" ");
            int EnterdAmt = int.Parse(Console.ReadLine());
            if (EnterdAmt != TotalCoffeeCost)
            {
                Console.WriteLine("");
                Console.WriteLine(" Please pay the correct amount");
            }
            else
            {
                invalidChoice = false;
            }
        }

        Console.WriteLine("");
        Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
    }
}

CodePudding user response:

It's because you set the totalCoffeCost to 0 when entering another programLoop. What you can do, is making the totalCoffeCost to a class variable, so you don't need to set the totalCoffeCost inside the Loop methode.

This should work fine:

using System;

/* A simple coffee ordering program
 * used switch,if else and lable*/

class Program
{
    private int totalCoffeCost = 0; 
   
    public static void Main()
    {
        var exiting = false;
        while (!exiting)
        {
            Program p = new Program();
            p.programLoop();
        }
    }

    private int askForCoffee()
    {
        int CoffeeCost = 0;
        bool invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
            Console.Write(" ");
            int CoffeeChoice = int.Parse(Console.ReadLine());

            switch (CoffeeChoice)
            {
                case 1:
                    CoffeeCost  = 2;
                    invalidChoice = false;
                    break;
                case 2:
                    CoffeeCost  = 5;
                    invalidChoice = false;
                    break;
                case 3:
                    CoffeeCost  = 7;
                    invalidChoice = false;
                    break;
                default:
                    Console.WriteLine("");
                    Console.WriteLine(" Please enter a valid choice");
                    break;
            }
        }
        return CoffeeCost;
    }


    private void programLoop()
    {
        totalCoffeeCost  = askForCoffee();

        var invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
            Console.Write(" ");
            string UserChoice = Console.ReadLine();
            string upperCaseChoice = UserChoice.ToUpper();

            if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
            {
                totalCoffeeCost  = askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
            }
            else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
            {
                invalidChoice = false;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string Anything = Console.ReadLine();
            string UpperCaseAnything = Anything.ToUpper();
            if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
            {
                invalidChoice = false;
            }
            else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
            {
                return;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Your total bill amount is = {0}$", totalCoffeeCost);
            Console.WriteLine("");
            Console.WriteLine(" Please pay the amount by entering the amount bellow");
            Console.Write(" ");
            int EnterdAmt = int.Parse(Console.ReadLine());
            if (EnterdAmt != totalCoffeeCost)
            {
                Console.WriteLine("");
                Console.WriteLine(" Please pay the correct amount");
            }
            else
            {
                invalidChoice = false;
            }
        }

        Console.WriteLine("");
        Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
        totalCoffeCost = 0;
    }
}

CodePudding user response:

Update askForCoffee() to pass through the existing total:

private int askForCoffee(int totalCoffeeCost)
{
   bool invalidChoice = true;
   while (invalidChoice)
   //...
}

then call it like this:

TotalCoffeeCost = askForCoffee(TotalCoffeeCost);

You also need to do a similar thing with programLoop:

private int programLoop(int TotalCoffeeCost)
{
   TotalCoffeeCost = askForCoffee(TotalCoffeeCost);
   //...
}

Update - adding the whole code to help the OP:

using System;

/* A simple coffee ordering program
 * used switch,if else and lable*/

class Program
{
    public static void Main()
    {
        int totalCoffeeCost = 0;
        var exiting = false;
        while (!exiting)
        {
            Program p = new Program();
            totalCoffeeCost = p.programLoop(totalCoffeeCost);
        }
    }

    private int askForCoffee(int totalCoffeeCost)
    {
        bool invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
            Console.Write(" ");
            int CoffeeChoice = int.Parse(Console.ReadLine());

            switch (CoffeeChoice)
            {
                case 1:
                    totalCoffeeCost  = 2;
                    invalidChoice = false;
                    break;
                case 2:
                    totalCoffeeCost  = 5;
                    invalidChoice = false;
                    break;
                case 3:
                    totalCoffeeCost  = 7;
                    invalidChoice = false;
                    break;
                default:
                    Console.WriteLine("");
                    Console.WriteLine(" Please enter a valid choice");
                    break;
            }
        }
        return totalCoffeeCost;
    }


    private int programLoop(int TotalCoffeeCost)
    {
        TotalCoffeeCost = askForCoffee(TotalCoffeeCost);

        var invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
            Console.Write(" ");
            string UserChoice = Console.ReadLine();
            string upperCaseChoice = UserChoice.ToUpper();

            if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
            {
                TotalCoffeeCost = askForCoffee(TotalCoffeeCost); //Note here that we did not set invalidChoice to false, meaning it will loop again
            }
            else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
            {
                invalidChoice = false;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string Anything = Console.ReadLine();
            string UpperCaseAnything = Anything.ToUpper();
            if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
            {
                invalidChoice = false;
            }
            else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
            {
                return TotalCoffeeCost;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
            Console.WriteLine("");
            Console.WriteLine(" Please pay the amount by entering the amount bellow");
            Console.Write(" ");
            int EnterdAmt = int.Parse(Console.ReadLine());
            if (EnterdAmt != TotalCoffeeCost)
            {
                Console.WriteLine("");
                Console.WriteLine(" Please pay the correct amount");
            }
            else
            {
                invalidChoice = false;
            }
        }

        Console.WriteLine("");
        Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");

        return TotalCoffeeCost;
    }
}

CodePudding user response:

Well, every method should do its work (say, provide an interface for a cup of coffee) and doesn't interfere in other activity (computing total etc. ``)

First of all, let's extract model (don not hardcode it within UI routines):

static readonly IReadOnlyDictionary<int, (string name, int size)> s_Options = 
  new Dictionary<int, (string name, int price)>() {
    {1, ("small",  2)},
    {2, ("medium", 5)},
    {3, ("large",  7)}, 
  };

Then asking for a single cup can be written like this:

private static int askForCoffee() {
  // Keep asking until correct value is provided
  while (true) {  
    var optionsToSell = s_Options
      .OrderBy(pair => pair.Key)
      .Select(pair => $"{pair.Key} - {pair.Value.name}");   

    Console.WriteLine();
    Console.WriteLine("Please enter your coffee size : {string.Join(" - ", optionsToSell)}}"); 
    Console.Write();

    // If entered value is a valid integer and we have such an option in s_Options
    // just return it   
    if (int.TryParse(Console.ReadLine(), out int choice) &&
        s_Options.TryGetValue(choice, out var option))
      return option.price;

    Console.WriteLine("");
    Console.WriteLine(" Please enter a valid choice");
  }
}

To ask for several cups, we can use a simple loop:

private static int askForManyCoffee() {
  int total = askForCoffee();

  while (true) {
    Console.WriteLine("");
    Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
    Console.Write(" ");

    var input = Console.ReadLine().Trim().ToUpper();

    if (input == "Y" || input == "YES")
      totsl  = askForCoffee();
    else if (input == "N" || input == "NO")
      return total;
    else {
      Console.WriteLine("");
      Console.WriteLine(" Please enter a valid choice"); 
    }   
  } 
}

Then in program loop you can operate with TotalCoffeeCost:

private void programLoop() {
  int TotalCoffeeCost = askForManyCoffee();
  ...
}

please, note, that being static askForCoffee() and askForManyCoffee() can't ruin anything like TotalCoffeeCost

CodePudding user response:

try this, move the variable to a property and rename the TotalCoffeeCost instances to totalCoffeeCost. That should get you the desired result.

using System;


class Program
{
private int totalCoffeeCost = 0;

public static void Main()
{
    var exiting = false;
    while (!exiting)
    {
        Program p = new Program();
        p.programLoop();
    }
}

private int askForCoffee()
{
    
    bool invalidChoice = true;
    while (invalidChoice)
    {
        Console.WriteLine("");
        Console.WriteLine(" Please enter your coffee size : 1 - small, 2 - medium, 3 - large");
        Console.Write(" ");
        int CoffeeChoice = int.Parse(Console.ReadLine());

        switch (CoffeeChoice)
        {
            case 1:
                totalCoffeeCost  = 2;
                invalidChoice = false;
                break;
            case 2:
                totalCoffeeCost  = 5;
                invalidChoice = false;
                break;
            case 3:
                totalCoffeeCost  = 7;
                invalidChoice = false;
                break;
            default:
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
                break;
        }
    }
    return totalCoffeeCost;
}


private void programLoop()
{


    totalCoffeeCost  = askForCoffee();

    var invalidChoice = true;
    while (invalidChoice)
    {
        Console.WriteLine("");
        Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
        Console.Write(" ");
        string UserChoice = Console.ReadLine();
        string upperCaseChoice = UserChoice.ToUpper();

        if (upperCaseChoice == "Y" || upperCaseChoice == "YES")
        {
            totalCoffeeCost  = askForCoffee(); //Note here that we did not set invalidChoice to false, meaning it will loop again
        }
        else if (upperCaseChoice == "N" || upperCaseChoice == "NO")
        {
            invalidChoice = false;
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter a valid choice");
        }
    }

    invalidChoice = true;
    while (invalidChoice)
    {
        Console.WriteLine(" ");
        Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
        Console.Write(" ");
        string Anything = Console.ReadLine();
        string UpperCaseAnything = Anything.ToUpper();
        if (UpperCaseAnything == "Y" || UpperCaseAnything == "YES")
        {
            invalidChoice = false;
        }
        else if (UpperCaseAnything == "N" || UpperCaseAnything == "NO")
        {
            return;
        }
        else
        {
            Console.WriteLine("");
            Console.WriteLine(" Please enter a valid choice");
        }
    }

    invalidChoice = true;
    while (invalidChoice)
    {
        Console.WriteLine("");
        Console.WriteLine(" Your total bill amount is = {0}$", totalCoffeeCost);
        Console.WriteLine("");
        Console.WriteLine(" Please pay the amount by entering the amount bellow");
        Console.Write(" ");
        int EnterdAmt = int.Parse(Console.ReadLine());
        if (EnterdAmt != totalCoffeeCost)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please pay the correct amount");
        }
        else
        {
            invalidChoice = false;
        }
    }

    Console.WriteLine("");
    Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}
}
  • Related