HI,

i am writing a program to enter monthly grocery items, purchase date and price. as part of program i have written to enter the data, write the entered data to a text file and read the existing data from the text file.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace grocery_billing
{
    class Program
    {
        class FileRead
        {

            public void ReadData()
            {

                FileStream fs = new FileStream("grocery billing.txt", FileMode.Open, FileAccess.Read);

                StreamReader sr = new StreamReader(fs);
                
                sr.BaseStream.Seek(0, SeekOrigin.Begin);

                string str = sr.ReadLine();

                while (str != null)
                {

                    Console.WriteLine(str);

                    str = sr.ReadLine();

                }

                Console.ReadLine();

                sr.Close();

                fs.Close();

            }

        }

        
        static void Main(string[] args)
        {
            int choice,key;
            string Description;
            float price, totalcost = 0;
            DateTime dt;
         
            Console.WriteLine("Enter 1 to review existing data");
            Console.WriteLine("Enter 2 to enter new data");
            Console.WriteLine("Enter ur choice");
            choice = Console.Read();
                     
            if (choice == 1)
            {
                FileRead wr = new FileRead();
                wr.ReadData();

            }
            else
            {
                do
                {
                    Console.WriteLine("enter the item:");
                    Description = Console.ReadLine();
                    Console.WriteLine("enter the date of purchase:");
                    dt = DateTime.Parse(Console.ReadLine());
                    Console.WriteLine("enter the amount:");
                    price = float.Parse(Console.ReadLine());
                    totalcost += price;
                    Console.Write("the total expenditure is:");
                    Console.WriteLine(totalcost);
                    Console.WriteLine("press key value to continue");
                    key = int.Parse(Console.ReadLine());

                } while (key != 0) ;

                FileStream fs = new FileStream(@"C:\Users\ramy\desktop\grocery billing.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.WriteLine("the item:" + Description);
                sw.WriteLine("date of purchase:" + dt);
                sw.WriteLine("the amount:" + price);
                sw.WriteLine("The total cost is: " + totalcost);
                sw.Flush();
                sw.Close();
                Console.WriteLine("press enter key to exit");
                Console.Read();
           
                
            }
            
            
         }
    }
}

1)here it is not taking the item. its just displaying enter the item: and immediately displaying enter the date. how ever i can enter the date of purchase and price of the item.
2) is there any other way to enter the data other than using do while loop. because i need the total cost to be displayed only once i.e., before exiting the application.
and even the statements used to write the data can be outside the loop.(i mean not in else loop)
pls correct me.i am still in the beginning stages of C# , let me know how to proceed further.

Thanks in advance.

The first thing I notice is that you are using "choice" as an integer and NOT comparing the ascii value of the number 1 key. You are comparing the number 1, which is something different.

if (choice == 0x31)
   {
      FileRead wr = new FileRead();
      wr.ReadData();
   }

...or you can Parse the char as an int and compare it to 1

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.