Reputation: 1
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.
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,"$");
}
}
}
Upvotes: 0
Views: 158
Reputation: 825
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
Upvotes: 1
Reputation: 1988
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
}
Upvotes: 1