Resolved Diary program

Yas

Member
Joined
Jan 26, 2023
Messages
7
Programming Experience
Beginner
Hi everyone, here is my program:
C#:
using System;
using System.IO;
public class Entry
{
   public static void Main()
   {
      Random random = new Random();
      string[] journalQuestions = {
      "What was the best thing that happened to you today?",
      "What steps did you take today towards a goal you're working on?",
      "What are three things you're grateful for today?",
      "What country would you like to visit for the first time?",
      "What magic power would you like to have?",
      "What do you feel is your purpose in life, and has that answer changed in the last five years?",
      "What is your happy place? Describe it in detail",
      "Where do you see yourself in 10 years? 20?",
      "What negative emotions am I holding onto? How can I let them go?",
      "What goals have you lost sight of? Are you interested in picking them up again?",
      "What do you want to be remembered for?" };
                                    

      // Generate random indexes for journalQuestions.
      int mIndex = random.Next(journalQuestions.Length);

      // Display the result.
      Console.Write(journalQuestions[mIndex]);
      string eJournal = Console.ReadLine();
     
      Console.WriteLine($"{eJournal}");
    labelName.Text = DateTime.Now.ToString("MMM dd yyyy");

Console.Write("What is the file name: ");
string fileInput = Console.ReadLine();
string fileName = fileInput;

using (StreamWriter outputFile = new StreamWriter(filename))
{
 
    outputFile.WriteLine("This will be the first line in the file.");
 

    outputFile.WriteLine($" {labelName.Text} {journalQuestions[mIndex]} {eJournal}");
}

In this code I am trying to create a program that selects a random question then prints it to the console, then allow the user to input the answer, and then create a text file where we can store the date, the question and the user entry, can anyone help me how to do it, because I am struggling really bad.

Thanks in advance.
 
Last edited by a moderator:
This seems related to your other thread. I'm going to try move this into that thread so that people will have more context about what you are trying to do.
 
Nevermind. That other thread is by @Reik . I won't merge threads.

Can you tell us what part you are struggling with?
 
/tmp/qxWx4BLMQq.cs(30,5): error CS0103: The name 'labelName' does not exist in the current context
/tmp/qxWx4BLMQq.cs(36,51): error CS0103: The name 'filename' does not exist in the current context
/tmp/qxWx4BLMQq.cs(42,30): error CS0103: The name 'labelName' does not exist in the current context

These are the errors that I am having, basically the information I am trying to write on the text is not written.
 
/tmp/qxWx4BLMQq.cs(36,51): error CS0103: The name 'filename' does not exist in the current context
C# is case sensitive. On line 34 you declared fileName with a capital N. On line 36 you are trying to use filename with a lowercase N.
 
/tmp/qxWx4BLMQq.cs(30,5): error CS0103: The name 'labelName' does not exist in the current context
/tmp/qxWx4BLMQq.cs(42,30): error CS0103: The name 'labelName' does not exist in the current context
It looks like you are trying to write a console app. By default, a console app doesn't have GUI elements like a Label. It looks like you are trying to access some control named labelName and put a value into it.

If you are truly writing a GUI app, you'll need to get a reference to your GUI window and access the labelName within that class to be able to set a property on that member of the class. In general, trying to do this goes against the Law of Demeter. The recommended thing to do is to set the label from within your window code.
 
Thanks for your response, I just changed the labelName to a string (string currentDate1 = DateTime.Now.ToString("MM/dd/yyyy");), and now I am not getting that error anymore, however, I am displaying this error:
/tmp/qxWx4BLMQq.cs(36,51): error CS0103: The name 'filename' does not exist in the current context.
I do not understand why 'filename' is giving that error, can you help me with that please?
 
See post #5.
 
Hi team, this is the last error I am showing: System.UnauthorizedAccessException: Access to the path "/run/txt.txt" is denied.

That happens when I enter the name of the text file where I am trying to write the information of my program, anyone knows why?
 
by the way this is the new program with the changes you suggested:
C#:
using System;
using System.IO;
public class Entry
{
   public static void Main()
   {
      Random random = new Random();
      string[] journalQuestions = {
      "What was the best thing that happened to you today?",
      "What steps did you take today towards a goal you're working on?",
      "What are three things you're grateful for today?",
      "What country would you like to visit for the first time?",
      "What magic power would you like to have?",
      "What do you feel is your purpose in life, and has that answer changed in the last five years?",
      "What is your happy place? Describe it in detail",
      "Where do you see yourself in 10 years? 20?",
      "What negative emotions am I holding onto? How can I let them go?",
      "What goals have you lost sight of? Are you interested in picking them up again?",
      "What do you want to be remembered for?" };
                                    

      // Generate random indexes for journalQuestions.
      int mIndex = random.Next(journalQuestions.Length);

      // Display the result.
      Console.Write(journalQuestions[mIndex]);
      string eJournal = Console.ReadLine();
     
      Console.WriteLine($"{journalQuestions[mIndex]}{eJournal}");
     string eJournalCombine = Console.ReadLine();
    string currentDate1 = DateTime.Now.ToString("MM/dd/yyyy");

Console.Write("What is the file name: ");
string fileInput = Console.ReadLine();
string fileName = fileInput;

using (StreamWriter outputFile = new StreamWriter(fileName))
{
 
    outputFile.WriteLine("This will be the first line in the file.");
 

    outputFile.WriteLine($" {currentDate1} {eJournalCombine}");
}
     
   }
}
 
Last edited by a moderator:
Hmm.. I suspect youre trying to create a file in c:\run\run.txt and either the run folder doesn't exist or the user youre running the program as doesn't have permission. Perhaps don't ask the user for a path too, just a filename
 
When posting code, please put the text of your code in code tags. There simplest way to do this is to use the toolbar button and the text area that looks like </>.
 
Back
Top Bottom