0

I already wrote a simple Python cash register program on my computer. But now I thought it would be better to write it in Visual Studio using C#, so I started to write the code.

But when I test my program (shown below), it works for the price set and the sell but when I tried to check my wallet, it outputs: "Money in wallet: $".

I actually an experienced C# programmer but I never do C# programming without unity because I usually work in GameDev. I am also an experienced Python programmer so I can write scripts with them.

Here is the python code

static void Main(string[] args)
{
        int total = (0);
        int pricePepsi = (0);
        int priceSprite = (0);
        string[] choice =
            { "1.Set Sprite's price",
              "2.Set Pepsi's price",
              "3.Sell Sprite",
              "4.Sell Pepsi",
              "5.See wallet" };

        Console.WriteLine("--------------------CASH REGISTER--------------------");

        while (true)
        {
            foreach (string i in choice)
            {
                Console.WriteLine(i);
            }

            Console.Write("Choose one(1/2/3/4/5):");

            int choose = Convert.ToInt32(Console.ReadLine());

            if (choose == 1)
            {
                Console.Write("Set Sprite's price to:");
                priceSprite = Convert.ToInt32(Console.ReadLine());
            }

            if (choose == 2)
            {
                Console.Write("Set Pepsi's price to:");
                pricePepsi = Convert.ToInt32(Console.ReadLine());
            }

            if (choose == 3)
            {
                Console.Write("Amount of Sprites sold:");
                int sellSprite = Convert.ToInt32(Console.ReadLine());
                total = (total+(sellSprite * priceSprite));
            }

            if (choose == 4)
            {
                Console.Write("Amount of Pepsis sold:");
                int sellPepsi = Convert.ToInt32(Console.ReadLine());
                total = (total+(sellPepsi * pricePepsi));
            }

            if (choose == 5)
            {
                Console.WriteLine("Money in wallet: ",total,"$");
            }
        }  
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CRO
  • 1
  • 1
  • 4

2 Answers2

1

Your mistake is with choice 5 with your Console.WriteLine.

if (choose == 5)
{

    //change this
    Console.WriteLine("Money in wallet: ",total,"$");
    //to this
    //Here are two alternatives. Choose one
    Console.WriteLine("Money in wallet: {0}$", total); // Composite formatting
    Console.WriteLine($"Money in wallet: {total}$"); // String interpolation
}
panoskarajohn
  • 1,846
  • 1
  • 21
  • 37
1

In C#, you can use Console.WriteLine(String, Object, Object) to write the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information:

public static void WriteLine (string format, object arg0, object arg1);

You can use:

string yourString = string.Format("Money in wallet: , {0}$", total);

or

string yourString = "Money in wallet: " + total + "$";

or you can use $ symbol and {yourVariable} such as below example:

string yourString = $"Money in wallet: {total}$";

But, C# can format the total with money format:

decimal total = 1800.12m; 
string yourString = String.Format("Money in wallet: {0:C}", total); 
Console.WriteLine(yourString);

Output:

Money in wallet: $1.800.12

Nguyen Van Thanh
  • 805
  • 9
  • 18
  • 1
    `string yourString = "Money in wallet: " + total + "$";` i would not suggest this -> https://stackoverflow.com/questions/21078/most-efficient-way-to-concatenate-strings. – panoskarajohn Dec 25 '19 at 14:43
  • 1
    String concatenation performance isn't something you should care about when doing simple programs like this. Also, adding strings together in a single statement is converted into String.Concat by the compiler so the performance is still really good. The problematic cases are if you are for example keep adding to the same string in a loop or multiple if-statements but even that isn't a huge problem unless you are doing something really performance critical. – Karl-Johan Sjögren Dec 25 '19 at 14:54