我是C ++的初学者,在我的作业中,我必须根据输入的用户名和密码从2D数组中保存一行。保存的行是关于在2D阵列中可以找到用户信息的位置的参考。我还没有弄清楚如何将特定行保存为整数。显示整个代码,并且执行任务的函数称为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...\n\n" << 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;
}
.txt文件包含:
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老师
jarrow@pnet.com James Arrow ahoy10 2005 U Apprentice
答案 0 :(得分:0)
我还没有弄清楚如何将特定行保存到 整数
在OP函数中,特定行只是i
,所以我们要做的就是将它分配给通过引用传递的变量saveRow
并返回。
我不会评论这个程序的一般设计,因为我不知道它有多少是作业的一部分,所以我将使用相同的OP接口和数据结构:
bool validateUser(string theAccounts[5][7], string username, string password, int &saveRow)
{
//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])
{
// match found, "save" the right row and return
saveRow = i;
return true;
}
}
// there's no match
return false;
}
答案 1 :(得分:0)
为了更简洁明了,您应该使用std::map
和std::vector
的组合。 std::map<Key, Value>
是一个容器,其中Keys
都是唯一的,可以是您想要的任何类型。 Values
也可以是您想要的任何类型,但可以复制。因此,想象一下,std::map<Deepblue, information>
是一个元素,另一个元素是std::map<FirstStep, information>
,依此类推。没有两个相同的键可以找到。因此,Key
将是string
,Information
将成为vector<string>
字符串容器。
以下代码有效,它会将您输入文件中的所有数据填充到地图中。按照评论,你应该看看它是如何工作的:
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
int main()
{
std::map<std::string, std::vector<std::string>> MyMap; // map that has a string key and a vector of strings as information
std::ifstream InFile; // to read from a text file
InFile.open("C:\\....\\input.txt");
if (InFile.fail()) // terminate if failed
{
std::cout << "File did not open correctly!";
return 0;
}
std::string OneLine;
std::vector<std::string> MyVector;
std::string MyKey;
std::vector<std::string> MyValue;
while(std::getline(InFile, OneLine)) // while "reading a whole line" is valid
{
//split the whole line on whitespaces and convert it to a vector of strings
std::stringstream ss(OneLine);
std::string item;
while (getline(ss, item, ' '))
MyVector.push_back(item);
// Now take the first word and set it as the key because it is the email, so it is the "ID" of every line/person
MyKey = MyVector[0];
// Now take the rest of the vector as Value
for (int i = 1; i < MyVector.size(); i++)
MyValue.push_back(MyVector[i]);
// Now add this [Key, Value] pair to the map
MyMap[MyKey] = MyValue;
}
system("pause");
return 0;
}
现在将鼠标悬停在此变量上以查看其结构。并做一些关于如何访问地图写/读等的研究。