-2

I tried to generate 4 random numbers with 4 digits into a text file, but the only thing i can type to it is just text in the quotes. Also you need to type an address to a textbox, but that doesn't matter. How can i type several ints into that file? Here's how i tried to do that.

private void btnGenerate_Click(object sender, EventArgs e)
{
    int i;
    i = textboxMAIL.Text.Length;
    if (i < 1)
    {
        MessageBox.Show("No address!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        MessageBox.Show("Successfully saved into psc.txt.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        Random r;
        r = new Random();
        int generate;
        generate = r.Next(9999); 

        Random r2;
        r2 = new Random();
        int generate2;
        generate2 = r2.Next(9999); 

        Random r3;
        r3 = new Random();
        int generate3;
        generate3 = r3.Next(9999); 

        Random r4;
        r4 = new Random();
        int generate4;
        generate4 = r4.Next(9999); 

        if (!File.Exists("c://desktop//psc.txt")) // If file does not exists
        {
            File.Create("c://desktop//psc.txt").Close(); // Create file
            using (StreamWriter sw = File.AppendText("c://desktop//psc.txt"))
            {
                sw.WriteLine(generate, generate2, generate3, generate4); // Write text to .txt file
            }
        }
        else // If file already exists
        {
            File.WriteAllText("c://desktop//psc.txt", String.Empty); // Clear file
            using (StreamWriter sw = File.AppendText("c://desktop//psc.txt"))
            {
                sw.WriteLine(generate, generate2, generate3, generate4); // Write text to .txt file
            }
        }
    }
}
p0150n
  • 3
  • 5
  • 3
    always instantiating a `new Random()` is wrong. – Uwe Keim May 22 '20 at 17:39
  • 3
    *"but the only thing i can type to it is just text in the quotes"* - Your second use of `sw.WriteLine` is indeed just text in quotes. Compare it to your first use. – David May 22 '20 at 17:39
  • a single Random object will generate an infinite number of values. No need for a new one each time, – Ňɏssa Pøngjǣrdenlarp May 22 '20 at 17:40
  • Yeah, sorry. I accidentally used the quotes in the second sw.writeline. – p0150n May 22 '20 at 17:41
  • 4
    Side note: Displaying a message saying that an operation was successful before you even perform that operation is just asking for trouble. – David May 22 '20 at 17:41
  • 2
    @p0150n: Since you've corrected that typo, it's time for you to re-test your code. Does it still fail in some way? What's the new issue? – David May 22 '20 at 17:42
  • 2
    Also: you don't need an extra line for a declaration. Just write `int generate1 = r.Next(9999);` and `Random r = new Random();` – KekuSemau May 22 '20 at 17:43
  • I get two CS1503 errors. Can't convert int into string, but i don't understand why. – p0150n May 22 '20 at 17:43
  • 3
    @p0150n: Because you're trying to use an `int` value where a method expects a `string` value. Always check the documentation of the methods you're trying to use: https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.writeline – David May 22 '20 at 17:45
  • > a single Random object will generate an infinite number of values" Yeah, but if i write it 4 times it will show me the same random, right? – p0150n May 22 '20 at 17:45
  • 1
    @p0150n: *"if i write it 4 times it will show me the same random, right?"* - The code you have now will do that, yes. If you use a single `Random` object and generate 4 values from that one object then they will (most likely) be different. – David May 22 '20 at 17:46
  • Ok. Then how do i make use of the sw.writeline? Are there any alternatives? – p0150n May 22 '20 at 17:49

1 Answers1

0

Check the documentation for StreamWriter.WriteLine. You're passing it 4 int values, but there is no version of that method which expects that. The first parameter of that method is the string to write.

If you want to write each value on its own line, convert each to a string and pass that to the method:

sw.WriteLine(generate.ToString());
sw.WriteLine(generate2.ToString());
sw.WriteLine(generate3.ToString());
sw.WriteLine(generate4.ToString());

If you want them to all be concatenated to the same line, the first argument is the string with format placeholders and the remaining arguments are the values for the placeholders:

sw.WriteLine("{0}{1}{2}{3}", generate, generate2, generate3, generate4);

Or with more modern syntax, if you're using a more recent C# version:

sw.WriteLine($"{generate}{generate2}{generate3}{generate4}");

It's also worth noting that your random number generation is broken. The code you have is very likely going to produce 4 identical random numbers every time. Refer to this question for more information. In short, you should use one instance of Random and use it to generate all of the numbers you need.

David
  • 208,112
  • 36
  • 198
  • 279
  • Hmm. I used the last one and i seen '8204' repeated 4 times. I'm gonna use the first one, since the second one is probably the same as the last. – p0150n May 22 '20 at 17:55