Ok erm here goes, im supposed to create a simple log in application using c#, howeaver, all the records of the userIDs and PINs are in a notepad file, which means i need to use a filestream. But heres the problem, how can i make my filestream object read through every record and find the correct one and match it? I really need some help on this. Below is my code so far... but its wrong.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ATMlogin
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"C:\Users\Zest\Documents\Visual Studio 2008\Projects\atm_user\atm_user\users.txt"; 
        //location of data file
        string[] RecArray;
        string recordIn;
        const char DELIM = '*';
        FileStream outFile;
        StreamWriter writer;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ID = Convert.ToString(user_tb.Text);
            string password = Convert.ToString(password_tb.Text);
            //Creating a File Stream object to Open a file called FILENAME for writing

            outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);

            //Creating a Stream Writer with the outFile File Stream object
            writer = new StreamWriter(outFile);
            Boolean valid = false;

            try
            {
                
                recordIn = reader.ReadLine();
                RecArray = recordIn.Split(DELIM);
                
                for(ID == RecArray[0];password == RecArray[1]; )
                {
                 
                    MessageBox.Show("Log In is Successful");
                    valid = true;
                }
                if(valid == false)
                {
                    MessageBox.Show("Unsuccessful Login");
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("You Have Missed out something");
            }
        }
    }
}

Recommended Answers

All 7 Replies

Declare a user defined class containing two variables, an ID and a PIN and at least an overloaded equals operator

Declare two objects of the above class, one called user and the other called target

obtain user input for ID and PIN for user. Validate input if desired.

associate the input stream with the file of valid ID/PIN combinations

use a loop (I'd use a while loop if it's available in C#) to read a single set of values from the file into target each time through the loop. Stop the loop if target is found or file has been completely processed.

within the body of the loop compare user with target. If there is a match then break out of the loop and proceed. If the loop ends with no match, then display appropriate message.

How you do that in C# I don't know.

thats the problem... i do not know how to generate this "loop" to search the whole array...

Here's one possible approach

userDefinedType user, target
bool found ---start as false

while read into target successful and not found
 if user equals target
   found is true

if found
  do whatever you need to do
else
  display appropriate message
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ATMlogin
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"C:\Users\Zest\Documents\Visual Studio 2008\Projects\atm_user\atm_user\users.txt"; 
        //location of data file
        string[] RecArray;
        string recordIn;
        const char DELIM = '*';
        FileStream outFile;
        StreamWriter writer;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ID = Convert.ToString(user_tb.Text);
            string password = Convert.ToString(password_tb.Text);
            //Creating a File Stream object to Open a file called FILENAME for writing

            outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);

            //Creating a Stream Writer with the outFile File Stream object
            writer = new StreamWriter(outFile);
            Boolean valid = false;

            try
            {
                
                recordIn = reader.ReadLine();
                RecArray = recordIn.Split(DELIM);

                while (ID == RecArray[0])
                {
                    if (password == RecArray[1])
                    {
                        MessageBox.Show("Log In is Successful");
                        valid = true;
                    }

                    else
                    {
                        MessageBox.Show("Unsuccessful Login");
                    }
                } 
            }
            catch (Exception)
            {
                MessageBox.Show("You Have Missed out something");
            }
        }
    }
}

how come it prompts that my reader is not in the context...

There is a forum dedicated to C#. This is not C++ code. I have flagged your post to be moved there by a moderator.

so erm... what shud i do now

Post something on this one saying continued in C# (so you don't get posts on both threads) and then repost it over in C# (with code tags this time please [code] //code goes here [/code]). Ordinarily you shouldn't post it multiple places but since no one has moved it that might be the best option. A mod can probably merge them later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.