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)


                    str = sr.ReadLine();







        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();

                    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("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);
                Console.WriteLine("press enter key to exit");

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.

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

6 Years
Discussion Span
Last Post by thines01

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();

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

Edited by thines01: clarity

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.