Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:35:08+00:00 2026-06-15T16:35:08+00:00

In C# can someone teach me how to write the output of this program

  • 0

In C# can someone teach me how to write the output of this program to a csv and a text file with unique ID each time? Like instead of writing to the console I want everything to go to a csv file and a text file at the same time. And also for the txt file includes a unique ID as record keeping.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

public class Product
{
    public string description;
    public double price;
    public int count;

    public Product(string description, double price, int count)
    {
        this.description = description;
        this.price = price * count;
        this.count = count;
    }

    public string GetDetailLine()
    {

        return String.Format(count + " {0}: {1:c}", description, price);
    }
}

class Program
{
    static void Main(string[] args)
    {
        int AdultTicket;
        int KidTicket;
        int PopcornOrder;
        int DrinkOrder;
        double tax = .05;

        List<Product> products = new List<Product>();

        Console.Write("How many adults? ");
        int.TryParse(Console.ReadLine(), out AdultTicket);
        Product adultTicket = new Product("Adult Ticket(s)", 7.75, AdultTicket);
        products.Add(adultTicket);

        Console.Write("How many kids? ");
        int.TryParse(Console.ReadLine(), out KidTicket);
        Product kidTicket = new Product("Kid Ticket(s)", 5.75, KidTicket);
        products.Add(kidTicket);

        Console.Write("Do you want popcorn? ");
        string input = Console.ReadLine();
        if (input.ToUpper() == "YES")
        {
            Console.Write ("How many? ");
            int.TryParse(Console.ReadLine (), out PopcornOrder);
            Product popcorn = new Product("Small popcorn", 3.50, PopcornOrder );
            products.Add(popcorn);
        }

        Console.Write("Do you want a drink? ");
        string input2 = Console.ReadLine();
        if (input2.ToUpper() == "YES")
        {
            Console.Write("How many? ");
            int.TryParse(Console.ReadLine(), out DrinkOrder);
            Product drink = new Product("Large Soda", 5.50, DrinkOrder);
            products.Add(drink);
            Console.Clear();
        }



        int count = 0;
        for (int i = 0; i < products.Count; i++)
        {
            count += products[i].count;
        }

        double total = 0;
        for (int i = 0; i < products.Count; i++)
        {                
            Console.WriteLine("\t\t" + products[i].GetDetailLine());
            total += products[i].price;
        }

        Console.WriteLine("\nYour sub total is:  {0:c}", total);
        Console.WriteLine("Tax: {0:c}", total * tax);
        Console.WriteLine("Your total is: {0:c}", total * tax + total);
        Console.ReadLine();
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:35:09+00:00Added an answer on June 15, 2026 at 4:35 pm

    to a csv and a text file, well,For writing to text file

    use this for your req in some button click:

           private static string logFileName = @"C:\Junk\MyLogfile.txt";
           using (StreamWriter writer = File.AppendText(logFileName))
                {
                    Log("Your sub total is:"+total.toString(), writer);
                    Log("Tax:"+ (total * tax).toString(), writer);
                    Log("Your total is:"+(total * tax + total).toString(), writer);
                    writer.Close();
                }
    

    And

            public static void Log(String logMessage, TextWriter writer)
            {
                writer.Write("\r\nLog Entry : ");
                writer.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());
                writer.WriteLine("  :");
                writer.WriteLine("  :{0}", logMessage);
                writer.WriteLine("-------------------------------");
                // Update the underlying file.
                writer.Flush();
            }
    

    more details visit http://www.c-sharpcorner.com/resources/735/how-to-append-a-text-file-in-c-sharp.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone teach me how to export tables with BLOB to CSV using java?
Can someone convert this query to linq2sql for me? Trying to teach myself linq
Can someone teach me how can I find out if bluetooth is connected to
I was reading about pixel per foot but can someone teach how can i
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Can someone tell me how to modify this regex to allow periods in a
can someone tell me what seems to be the issue with this <?php $increment
Can someone please explain to me how this responsive approach works? This was done
Can someone teach me or give me a link for my problem? I wanted
Can someone please teach me how to create C# code that would allow my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.