Click here to Skip to main content
15,867,308 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do I take datatable values into postgresql Pin
Gerry Schmitz3-Feb-23 7:10
mveGerry Schmitz3-Feb-23 7:10 
AnswerRe: How do I take datatable values into postgresql Pin
Dave Kreskowiak2-Feb-23 9:11
mveDave Kreskowiak2-Feb-23 9:11 
AnswerRe: How do I take datatable values into postgresql Pin
Richard MacCutchan2-Feb-23 21:40
mveRichard MacCutchan2-Feb-23 21:40 
AnswerRe: How do I take datatable values into postgresql Pin
jschell3-Feb-23 5:30
jschell3-Feb-23 5:30 
GeneralRe: How do I take datatable values into postgresql Pin
Member 141039873-Feb-23 21:33
Member 141039873-Feb-23 21:33 
GeneralRe: How do I take datatable values into postgresql Pin
Dave Kreskowiak4-Feb-23 5:40
mveDave Kreskowiak4-Feb-23 5:40 
AnswerRe: How do I take datatable values into postgresql Pin
Richard Deeming5-Feb-23 22:52
mveRichard Deeming5-Feb-23 22:52 
Question18 Errors Needs to be Fixed Pin
Michael Afolayan30-Jan-23 21:46
Michael Afolayan30-Jan-23 21:46 
Hello, Profs

Please, I need help in my c# journal code as I find it difficult to get rid of the errors.
Below is the sample of my c# code and I will appreciate if the errors could be fixed.
Thanks.



using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Journal
{
    class Program
    {
        static void Main(string[] args)
        {
            Journal journal = new Journal();

            while (true)
            {
                Console.WriteLine("1. Write a new entry");
                Console.WriteLine("2. Display journal");
                Console.WriteLine("3. Save journal");
                Console.WriteLine("4. Load journal");
                Console.WriteLine("5. Quit");

                Console.Write("Enter your choice: ");
                int userChoice = Convert.ToInt32(Console.ReadLine());

                if (userChoice == 1)
                {
                    string chosenPrompt = journal.GetRandomPrompt();
                    Console.WriteLine("Prompt: " + chosenPrompt);
                    Console.WriteLine("Write your response:");
                    string response = Console.ReadLine();

                    Entry newEntry = new Entry
                    {
                        Date = DateTime.Now,
                        Prompt = chosenPrompt,
                        Response = response
                    };

                    journal.AddEntry(newEntry);
                }
                else if (userChoice == 2)
                {
                    journal.DisplayEntries();
                }
                else if (userChoice == 3)
                {
                    Console.WriteLine("Enter a filename to save journal:");
                    string filename = Console.ReadLine();
                    journal.SaveJournal(filename);
                }
                else if (userChoice == 4)
                {
                    Console.WriteLine("Enter a filename to load journal:");
                    string filename = Console.ReadLine();
                    journal.LoadJournal(filename);
                }
                else if (userChoice == 5)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Invalid choice. Try again.");
                }
            }
        }
    }

    class Entry
    {
        public DateTime Date { get; set; }
        public string Prompt { get; set; }
        public string Response { get; set; }
    }

    class Journal
    {
        private List<string> prompts = new List<string>
        {
            "What did you learn today?",
            "What are you grateful for?",
            "What did you accomplish today?",
            "What challenges did you face today?",
            "What are your goals for tomorrow?"
        };
        private List<Entry> entries = new List<Entry>();

        public string GetRandomPrompt()
        {
            Random rand = new Random();
            int index = rand.Next(prompts.Count);
            return prompts[index];
        }

        public void AddEntry(Entry entry)
        {
            entries.Add(entry);
        }

        public void DisplayEntries()
        {
            Console.WriteLine("Journal Entries:");
            foreach (Entry entry in entries)
            {
                Console.WriteLine("Date: " + entry.Date);
                Console.WriteLine("Prompt: " + entry.Prompt);
                Console.WriteLine("Response: " + entry.Response);
                Console.WriteLine("----------------");
            }
        }

        public void SaveJournal(string filename)
        {
            using (Stream stream = File.Open(filename, FileMode.Create))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(stream, entries);
            }
        }

        public void LoadJournal(string filename)
        {
            if (File.Exists(filename))
            {
                using (Stream stream = File.Open(filename, FileMode.Open))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    entries = (List<Entry>)binaryFormatter.Deserialize(stream);
                }
            }
            else
            {
                Console.WriteLine("File does not exist.");
            }
        }
    }
}

AnswerRe: 18 Errors Needs to be Fixed Pin
Richard Deeming30-Jan-23 21:49
mveRichard Deeming30-Jan-23 21:49 
GeneralRe: 18 Errors Needs to be Fixed Pin
Michael Afolayan30-Jan-23 22:58
Michael Afolayan30-Jan-23 22:58 
GeneralRe: 18 Errors Needs to be Fixed Pin
Richard Deeming30-Jan-23 23:09
mveRichard Deeming30-Jan-23 23:09 
GeneralRe: 18 Errors Needs to be Fixed Pin
Richard MacCutchan30-Jan-23 23:11
mveRichard MacCutchan30-Jan-23 23:11 
GeneralRe: 18 Errors Needs to be Fixed Pin
Michael Afolayan30-Jan-23 23:23
Michael Afolayan30-Jan-23 23:23 
GeneralRe: 18 Errors Needs to be Fixed Pin
Richard MacCutchan30-Jan-23 23:09
mveRichard MacCutchan30-Jan-23 23:09 
GeneralRe: 18 Errors Needs to be Fixed Pin
Calin Negru1-Feb-23 1:00
Calin Negru1-Feb-23 1:00 
AnswerRe: 18 Errors Needs to be Fixed Pin
OriginalGriff30-Jan-23 22:09
mveOriginalGriff30-Jan-23 22:09 
GeneralRe: 18 Errors Needs to be Fixed Pin
Michael Afolayan30-Jan-23 23:15
Michael Afolayan30-Jan-23 23:15 
GeneralRe: 18 Errors Needs to be Fixed Pin
OriginalGriff31-Jan-23 2:06
mveOriginalGriff31-Jan-23 2:06 
GeneralRe: 18 Errors Needs to be Fixed Pin
CHill601-Feb-23 3:58
mveCHill601-Feb-23 3:58 
QuestionWhat's up with displaying transparent pixel? Closed Pin
mo149229-Jan-23 0:13
mo149229-Jan-23 0:13 
AnswerRe: What's up with displaying transparent pixel? Closed Pin
lmoelleb30-Jan-23 1:52
lmoelleb30-Jan-23 1:52 
QuestionWhy is my AlphaPanel approach to disable the entire GUI so slow? Pin
arnold_w25-Jan-23 4:45
arnold_w25-Jan-23 4:45 
AnswerRe: Why is my AlphaPanel approach to disable the entire GUI so slow? Pin
Gerry Schmitz25-Jan-23 6:09
mveGerry Schmitz25-Jan-23 6:09 
GeneralRe: Why is my AlphaPanel approach to disable the entire GUI so slow? Pin
arnold_w27-Jan-23 0:23
arnold_w27-Jan-23 0:23 
GeneralRe: Why is my AlphaPanel approach to disable the entire GUI so slow? Pin
Gerry Schmitz27-Jan-23 6:24
mveGerry Schmitz27-Jan-23 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.