Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The purpose of the code is for to enter a new users data, and write it to that users file. But the file name needs to be created in the users name.

I had the data being written to the file working completely fine, but when I tried to make the name generate from the variable, it doesn't work. I get errors such as:
Argument 1: cannot convert from 'string' to 'System.IO.Stream' FUNCOCO2

Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2

Error CS1503 Argument 2: cannot convert from 'string' to 'System.Text.Encoding'


As you can probably tell, I'm very new to programming.

What I have tried:

case 2: //enter new user
                        //Enter new user information and add them to file
                    Console.Write("Please Enter the number of users you wish to log: ");
                    iUserNumber = Convert.ToInt32(Console.ReadLine());

                    //open file for writing, in APPEND mode
                    using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))
                    {

                        //loop for each user
                        for (iCount = 1; iCount <= iUserNumber; iCount++)
                        {
                            // enter details
                            Console.Write("Please Enter your Name" + iCount + ": ");
                            sName = Console.ReadLine();

                            Console.Write("Please Enter the Name of your School: ");
                            sSchoolName = Console.ReadLine();
                            Console.WriteLine();

                            Console.Write("Please Enter the Name of your Class: ");
                            sClassName = Console.ReadLine();

                            // write to file
                            sw.WriteLine(sName);
                            Console.WriteLine();
                            sw.WriteLine(sSchoolName);
                            Console.WriteLine();
                            sw.WriteLine(sClassName);


                            Console.WriteLine("User data entered: ");

                        }

                    }
Posted
Updated 1-Dec-16 5:02am
Comments
cvogt61457 1-Dec-16 9:43am    
Which lines are having the errors?
What are the types of sName, sSchoolName, and sClassName? (I'm guessing string)

When you're creating the StreamWriter object, you don't yet know the users name because you haven't asked for it yet. I would do it this way:
C#
case 2: //enter new user
{
    //Enter new user information and add them to file
    Console.Write("Please Enter the number of users you wish to log: ");
    iUserNumber = Convert.ToInt32(Console.ReadLine());

    for (int i = 1; i < iUserCount; i++)
    {
        // enter details
        Console.Write("Please Enter your Name: ");
        sName = Console.ReadLine();
        Console.WriteLine();

        Console.Write("Please Enter the Name of your School: ");
        sSchoolName = Console.ReadLine();
        Console.WriteLine();

        Console.Write("Please Enter the Name of your Class: ");
        sClassName = Console.ReadLine();

        // write to file (using a method)
        CreateUserFile(sName, sSchool, sClass);
    }
}
break;

and the method would look like this:
C#
private void CreateUserFile(string studentName, string schoolName, string className)
{
    using (StreamWriter writer = new StreamWriter(string.Concat("NewUser_", studentName, ".txt"), true))
    {
        writer.WriteLine(name);
        writer.WriteLine(school);
        writer.WriteLine(className);
    }
}

Of course, this code is not robust at all, because there is no error handling to prevent "stupid user tricks"™, but I suppose that's okay since this is homework.
 
Share this answer
 
v2
Probably, it's this line:
C#
using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))
(But double clicking the error message will take you to the right line just to be sure)
And it's probably teh comma that is doing it.
Change:
C#
new StreamWriter("NewUser" +sName,".txt", true))
To:
C#
new StreamWriter("NewUser" +sName+".txt", true))
or better:
C#
string path = string.Format("NewUser{0}.txt", sname);
using (StreamWriter sw = new StreamWriter(path, true))

And you should be fine.
It would be a good idea to use a folder other than the default though: it works in development but tends to fail in production because the application folder is write protected. Better to get used to using a specific data folder which is always read / write instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900