Hello,
below is the program written to enter the details of grocery products purchased and find the totalamount.

namespace grocery_billing
{


public class program
{



public static void Main()
{
int choice, key;
float Price, TotalCost;
string ItemDescription;
DateTime Date;
File.Create(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "grocery billing.txt");
Console.WriteLine("Press 1 to read data from file");
Console.WriteLine("press 2 to write data to file");
Console.WriteLine("press 3  to enter data");
Console.WriteLine("press 4 to exit");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\ramya\desktop\grocery billing.txt");
System.Console.WriteLine("Contents of grocery billing.txt :");
foreach (string line in lines)
{
Console.Write(line);
Console.Write(",", line);
}


break;
}
case 2:
{
break;
}


case 3:
{
do
{
Console.Write("Enter the item:");
ItemDescription = Console.ReadLine();
Console.Write("Enter the Date of purchse:");
Date = DateTime.Parse(Console.ReadLine());
Console.Write("Enter the cost:");
Price = float.Parse(Console.ReadLine());
TotalCost += Price;
TotalCost = float.Parse(console.ReadLine();


StreamWriter fileWriter = new StreamWriter("grocery billing.txt");
fileWriter.Close();
FileStream fs = new FileStream(@"C:\Users\ram\desktop\grocery billing.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("the item:" + ItemDescription);
sw.WriteLine("date of purchase:" + Date);
sw.WriteLine("the amount:" + Price);
sw.WriteLine("the total amount:" +TotalCost);
sw.Flush();
sw.Close();
Console.WriteLine("If you want to enter more data press 3");
key = int.Parse(Console.ReadLine());



} while (key == 3);


break;
}
}
}
}
}

1) can any one help me how to write a different function to write the data into the text file. As itemdescription , date and Price are local to do while loop i can't write the filestream outside the loop.
2)is there any way to display the Totalcost only once i.e., before exiting the application.
3)i am able to read the data from grocery billing.txt file .but i want to read the data as key value pairs is it possible?help me.

is it compulsory that you don't want to write filestream outside the loop? Let me know if it is the case.
otherwise in simple way you can write a function which will accept your itemdesc,price etc as a parameter and write this code inside the function

See Below Mentioned Code:

namespace PraticeCA
{
class Program
{


static void Main(string[] args)
{
trial t = new trial();
int choice, key;
float price, tot;
string itemdesc;
DateTime date;
// File.Create(Environment.SpecialFolder.Desktop + "abc.txt");
Console.WriteLine("press 1 to read");
Console.WriteLine("press 2 to write");
Console.WriteLine("press 3 to exit");
Console.WriteLine("Please enter you choice");
char i =Convert.ToChar(Console.ReadLine());


switch (i)
{
case '1':
break;
case '2':
Console.WriteLine("Please enter item desc");
itemdesc = Convert.ToString(Console.ReadLine());
Console.WriteLine("Please enter price");
price = Convert.ToInt32(Console.ReadLine());
t.writedata(itemdesc, price);
break;


case '3':
break;


}
}



}
class trial
{
public void writedata(string item, float pri)
{
FileStream fs = new FileStream(Environment.SpecialFolder.Desktop + "abc.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(item);
sw.WriteLine(pri);
sw.Close();
}
}
}

hello,
It is not compulsory. when i am trying with your sample code it is not taking any input.
can you please tell me how to read tha data as key value pairs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.