Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to insert a while loop in the following code in order to insert more than one record.

The code output asks for Name and ID and then print them out in a text file.

Here is my code:

Console.Write(" Enter your ID: ");
int ID = int.Parse(Console.ReadLine());
Console.Write(" Enter your Name: ");
string Name = Console.ReadLine();
Console.WriteLine(" Thank you! you are logged in as " + Name + " " + ID);
StreamWriter sw = new StreamWriter("fileone.txt", true);
Console.SetOut(sw);
Console.WriteLine("Thank you! you are logged in as " + Name + " " + ID);
Console.Read();

sw.Close();

How can I use while loop in order to to let the program ask for another 10 records to enter?

Any answers would be appreciated.

Thanks in advance.

share|improve this question
Do you just need to know how to do a while loop and then you can put it in where ever you need? msdn.microsoft.com/en-us/library/2aeyhxcd(v=vs.71).aspx – DROP table users Apr 25 at 15:14
why don't you just put it in a for loop since it is a static number of times it has to run? – Thewads Apr 25 at 15:22
@DROPtableusers: Btw, the reason I downvoted your (now-deleted) answer is that you opened 10 StreamWriter objects on one file all the same time and didn't synchronize their write buffers in any way. – Ben Voigt Apr 25 at 15:22
@BenVoigt I was trying to illustrate how you would put a while loop in to loop through their code 10 times, your points are valid admittedly, but not what I was trying to illustrate. I think that your comment would have been more helpful than a down-vote. No big deal. – DROP table users Apr 25 at 15:37
@DROPtableusers: I was commenting but you can't leave comments on a deleted answer. – Ben Voigt Apr 25 at 17:03
show 1 more comment

2 Answers

up vote 1 down vote accepted

I have reformatted your code. You can use Int32.TryParse() while safely convert ID to Integer.

                int count = 0;
                while (count < 10)
                {
                    Console.Write(" Enter your ID: ");
                    int ID = int.Parse(Console.ReadLine());

                    Console.Write(" Enter your Name: ");
                    string Name = Console.ReadLine();

                    string output = string.Format("Thank you! you are logged in as {0} {1}", Name, ID);
                    Console.WriteLine(output);
                    File.AppendAllText("fileone.txt", output + Environment.NewLine);

                    count++;
                }
share|improve this answer
Thank you very much. It works very well. – MISHAK Apr 25 at 16:04
2  
It might be desirable to collect all the output in a StringBuilder and use just one file I/O call, instead of repeatedly opening and closing the file. – Ben Voigt Apr 25 at 17:04
Does anyone know how to insert update/delete methods in this code in order to delete specific entries/records from the text file? Thank you. – MISHAK Apr 25 at 17:06

Not sure I understand. Wrap your code inside a for loop?

for (int i=0; i<10; i++)
{
  ... your code
}

Of course you'll have to use the i loop variable to construct a different tile name for each user Id and password pair.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.