mardi 21 juin 2016

Store 2D array row in parallel array

I'm a beginner to C++, in my homework i have to save a row from a 2D array based on the inputted username and password. The saved row is to be a reference as to where the user info can be found in the 2D array. I haven't been able to figure out how to save the specific row to an integer. The whole code is displayed and the function to preform the task is called validateUser.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;

void showAll(string theAccounts[5][7]);
void sortInput(string theAccounts[5][7]);
bool validateUser(string theAccounts[5][7], string username, string password,     int &saveRow);
bool readFile(string theAccounts[5][7]);

int main()
{
//Declare Necessary Variables and 2D array to store data from input file.
string username;
string password;
string accountData[5][7] = { " " };
bool userGood;
bool fileGood;

    //Call the readFile function and capture the returned value.
fileGood = readFile(accountData);

//Test if file was successfully read, if so continue with program else exit with error message
if (fileGood == false)
{
    cout << "Error occurred...File Unread...Program Exiting" << endl;
}
else
{
    cout << "nFile Read Successfully...nn" << endl;

    //Ask user to Enter User Name or Zero to Exit
    cout << "Enter the following information or 0 to Exit...n";
    cout << "Please Enter Your User Name > ";

    //Read in User Name     
    //If User Name read in is “zero” Exit program.
    cin >> username;
    if (username == "0")
    {
        return 0;
    }

    //Ask the user to Enter Their Password or Zero to Exit
    cout << "Please Enter Your User Password > ";

    //Read in User Password
    //If User Password read in is “zero” Exit program
    cin >> password;
    if (password == "0")
    {
        return 0;
    }

    //Call the Validate User function and capture the returned value

    //If returned value from the Validate User function is TRUE continue program to check access code if U or A
    //If U – code appropriately
    //If A – code appropriately

    //Else if returned value Not Valid from the Validate User function, FALSE, output message username and password invalid

}
return 0;
}

void showAll(string theAccounts[5][7])
{
const int Rows = 5;
const int Columns = 7;

ifstream accountFile;
accountFile.open("AccountData.txt");

if (accountFile.is_open())
{
    for (int i = 0; i < Rows; i++)
    {
        for (int j = 0; j < Columns; j++)
        {
            cout << setw(8) << theAccounts[i][j];
        }
        cout << endl;
    }
}
accountFile.close();
}
void sortInput(string theAccounts[5][7])
{

}

bool validateUser(string theAccounts[5][7], string username, string password, int &saveRow)
{
bool passed = false;
//test for username and password match whats stored in theAccounts
for (int i = 0; i < 5; i++)
{
    if (username == theAccounts[i][0] && password == theAccounts[i][3])
    {
        saveRow = theAccounts[i];
    }
}

return passed;
}

bool readFile(string theAccounts[5][7])
{
bool fileRead;
const int Height = 5;
const int Width = 7;

ifstream inputFile; //an ifstream object – input file stream object
inputFile.open("AccountData.txt"); // the open member function opens the text file and links it with
                                   // inputFile to read data from the AccountData.txt file

//input validation to see if file opened
//if opened, read strings into theAccounts array - reset fileRead to true
if (inputFile.is_open())
{
    fileRead = true;
    for (int i = 0; i < Height; i++)
    {
        for (int j = 0; j < Width; j++)
        {
            inputFile >> theAccounts[i][j];
        }
    }
}
else
{
    fileRead = false;
}

inputFile.close();
return fileRead;
}

The .txt file contains:

bham@gnet.com Blake Ham squid62 1987 U Teacher

jdark@att.net Jim Dark gymrat32 1985 A Master

hgreen@lakes.net Hannah Green flower22 2007 U Apprentice

tsmith@dna.com Tom Smith tuna20 2000 U Teacher

jarrow@pnet.com James Arrow ahoy10 2005 U Apprentice

Aucun commentaire:

Enregistrer un commentaire