none
Store score with a user profile RRS feed

  • Question

  • Hi everyone!

    I'm fairly new to coding and my professor assigned the class to create a unique program that could be used in real life. I chose to do a user rewards system using 2 forms, one where the customer can register their name and phone number and another where they input their phone number to add reward points to their existing account and display the total points but when they reach past an x amount, they earn a discount code. Our class is not allowed to use databases so I chose do to a text file. I can store the name and phone number but I'm stuck on the point system as the user automatically earns it by typing in their phone number. This is my current code:

     public partial class FrmResult : Form
        {

            // Declare three arrays
            string[] fName;
            string[] lName;
            string[] phonen;

            public FrmResult()
            {
                InitializeComponent();
            }

            private void FrmResult_Load(object sender, EventArgs e)
            {
                // Declare variables and constants
                string firstName, lastName;
                string phone;
                string points = 0;
                const char DELIM = ',';
                string recordIn;  // read entire record
                string[] fields;  // store current record
                int sub = 0;  // keeps track on which subscript we are on.
                const string FILENAME = "userData.txt";
                const string FILENAME2 = "permCustData.txt";
                const string FILENAME3 = "permDelimData.txt";
                int x = 0;  // count number of lines
                int numRec;  // calulate number of records
                const int NUM_VAR = 5;  //Number of variables

                // Create filestream and streamreader
                FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(inFile);

                // Create filestream and streamreader for permanent file
                FileStream inFile2 = new FileStream(FILENAME2, FileMode.Open, FileAccess.Read);
                StreamReader reader2 = new StreamReader(inFile2);

                // Create filestream and streamreader for permanent delimited file
                FileStream inFile3 = new FileStream(FILENAME3, FileMode.Open, FileAccess.Read);
                StreamReader reader3 = new StreamReader(inFile3);

                // Count data in file
                while (reader2.ReadLine() != null)
                    ++x;

                numRec = x / NUM_VAR;

                // Create Arrays
                fName = new string[numRec];
                lName = new string[numRec];
                phonen = new string[numRec];
                

                inFile2.Seek(0, SeekOrigin.Begin);

                // Read data from permanent delim file

                recordIn = reader3.ReadLine();
                while (recordIn != null)
                {
                    fields = recordIn.Split(DELIM); // SEPARATES record
                    fName[sub] = fields[0];
                    lName[sub] = fields[1];
                    phonen[sub] = fields[2];
                    points[sub] = fields[3];
                    ++sub;
                    recordIn = reader3.ReadLine();
                }
                // Read data from file
                firstName = reader.ReadLine();
                lastName = reader.ReadLine();
                phone = reader.ReadLine();
                points = Convert.ToInt32(reader.ReadLine());

                // close file
                reader.Close();
                inFile.Close();

                // close permanent file
                reader2.Close();
                inFile2.Close();

                // close permanent file
                reader3.Close();
                inFile3.Close();
            }

            private void btnEnter_Click(object sender, EventArgs e)
            {
                bool foundFlag = false; // to keep track if there is a match
                int z = 0;  // keep position 
                string nphone;  // person is going to type in
                int found = 0;

                nphone = txtPhone.Text;

                for (z = 0; z < fName.Length; ++z)
                {
                    if (nphone == phonen[z])
                    {
                        foundFlag = true;
                        found = z;
                    }
                }
                if (foundFlag)
                {
                    points++;
                    lblResult.Text = String.Format("{0, -10} {1, -15}: You have {2} points", fName[found], lName[found], points.ToString("C"));
                   
                }
                else
                {
                    lblResult.Text = "Match was not found";
                }
            }

            private void btnRegister_Click(object sender, EventArgs e)
            {
                FrmRegister secondForm = new FrmRegister();
                secondForm.Show();
                
            }
        }
    }

     public partial class FrmRegister : Form
        {
            public FrmRegister()
            {
                InitializeComponent();
            }

            private void btnRegister_Click(object sender, EventArgs e)
            {
                // Declare variables and constants
                string fName, lName;
                string nphone;
                int points;
                const char DELIM = ',';
                const string FILENAME = "userData.txt"; // temp file
                const string FILENAME2 = "permCustData.txt"; // permanent file
                const string FILENAME3 = "permDelimData.txt"; // permanent delim file

                // Create Filestream and Streamwriter for temp file
                FileStream outFile = new FileStream(FILENAME, FileMode.Create, FileAccess.Write);
                StreamWriter writer = new StreamWriter(outFile);
                // Create Filestream and Streamwriter for permanent file
                FileStream outFile2 = new FileStream(FILENAME2, FileMode.Append, FileAccess.Write);
                StreamWriter writer2 = new StreamWriter(outFile2);

                // Create Filestream and Streamwriter for permanent Delim file
                FileStream outFile3 = new FileStream(FILENAME3, FileMode.Append, FileAccess.Write);
                StreamWriter writer3 = new StreamWriter(outFile3);

                // Input data from user
                fName = txtFirst.Text;
                lName = txtLast.Text;
                nphone = txtPhonen.Text;
                

                //Save data to temp file
                writer.WriteLine(fName);
                writer.WriteLine(lName);
                writer.WriteLine(nphone);
                writer.WriteLine(points);

                //Save data to permanent file
                writer2.WriteLine(fName);
                writer2.WriteLine(lName);
                writer2.WriteLine(nphone);
                writer2.WriteLine(points);

                // Save data to delimited file
                writer3.WriteLine(fName + DELIM + lName + DELIM + nphone + DELIM + points);

                // close temp file
                writer.Close();
                outFile.Close();

                // close permanent file
                writer2.Close();
                outFile2.Close();

                // close delim file
                writer3.Close();
                outFile3.Close();

                FrmResult secondForm = new FrmResult();
                secondForm.Show();
                this.Hide();
            }

            private void btnBack_Click(object sender, EventArgs e)
            {
                FrmResult secondForm = new FrmResult();
                secondForm.Show();
                this.Hide();
            }
        }
    }
    • Edited by GC_- Monday, December 9, 2019 1:17 AM
    Monday, December 9, 2019 1:16 AM

Answers

  • Something like this?

            // Declare four arrays
            string[] fName;
            string[] lName;
            string[] phonen;
            int[] customerPoints;
    
    
    // etc. etc. More code goes here.
    
    
    
    
               if (foundFlag)
                {
                    customerPoints[found]++;
                    lblResult.Text = String.Format("{0, -10} {1, -15}: You have {2} points", fName[found], lName[found], customerPoints[found].ToString("C"));
    

    Then you have to save/read customerPoints the same way you do fName, lName and phonen. Just remember it's integers not strings, so you have to change the reading and writing appropriately.

    • Marked as answer by GC_- Monday, December 9, 2019 4:52 AM
    Monday, December 9, 2019 3:11 AM

All replies

  • Greetings GC.

    Sorry, but you might have to be a bit more specific. What exactly is the problem, and where?

    Monday, December 9, 2019 1:26 AM
  • Sorry, my problem is my point system. I'm not sure how to store the points with the name and phone since the points aren't inputted by the user. When the user enters their phone number that matches their phone number from the text, the user earns a point and their total points is displayed:

     private void btnEnter_Click(object sender, EventArgs e)
            {
                bool foundFlag = false; // to keep track if there is a match
                int z = 0;  // keep position 
                string nphone;  // person is going to type in
                int found = 0;

                nphone = txtPhone.Text;

                for (z = 0; z < fName.Length; ++z)
                {
                    if (nphone == phonen[z])
                    {
                        foundFlag = true;
                        found = z;
                    }
                }
                if (foundFlag)
                {
                    points++;
                    lblResult.Text = String.Format("{0, -10} {1, -15}: You have {2} points", fName[found], lName[found], points.ToString("C"));

    Monday, December 9, 2019 1:36 AM
  • I'm still struggling to understand the issue.

    Can't you store the points the same way you store the names and phone numbers? That is, in an array with the same index (except it would be an array of integers, rather than strings).

    (And by the way, in a professional application, you wouldn't store data in separate arrays like that. Instead you would make a class called, for example, Customer, which contains a name, phone number and points accumulated, then create an array of those. Your approach will work, but it's inefficient.)

    Monday, December 9, 2019 1:55 AM
  • I have the points stored in the text file but they remain at zero and does not add another point when I hit enter.
    Monday, December 9, 2019 3:01 AM
  • Something like this?

            // Declare four arrays
            string[] fName;
            string[] lName;
            string[] phonen;
            int[] customerPoints;
    
    
    // etc. etc. More code goes here.
    
    
    
    
               if (foundFlag)
                {
                    customerPoints[found]++;
                    lblResult.Text = String.Format("{0, -10} {1, -15}: You have {2} points", fName[found], lName[found], customerPoints[found].ToString("C"));
    

    Then you have to save/read customerPoints the same way you do fName, lName and phonen. Just remember it's integers not strings, so you have to change the reading and writing appropriately.

    • Marked as answer by GC_- Monday, December 9, 2019 4:52 AM
    Monday, December 9, 2019 3:11 AM
  • Yes thank you so much! That was what I needed to change. The string increment did not work but the array did!
    Monday, December 9, 2019 4:48 AM