繁体   English   中英

如何检查数组的索引是否为空,如果是,则跳到下一个?

[英]How do I check whether an index of array is empty and then, if it is, skip to the next?

我正在尝试构建一个可以将用户注册到数据库的程序(仍在学习cpp,我希望在不久的将来我能够使用数据库)。

我试图用这段代码做的是检查数组的索引是否为空,以便用户在其中存储 ID。 如果它不为空,我希望程序继续寻找数组的空索引,以便存储新信息。

这是代码:

void registro() {
std::string userid[3];
userid[0] = "Houkros"; // eventually I'll try to have this being read from a file or server database..
std::string userpass[3];
std::string usermail[3];
std::string userkey[3];
std::string getUid[3];
std::string getUpass[3];
std::string getUmail[3];
std::string getUkey[3];

std::cout << std::endl << " >>>> REGISTRATION <<<< " << std::endl;
std::cout << " =============================================== " << std::endl;
std::cout << std::endl;
std::cout << "Please, enter the desired user id: " << std::flush;
if (userid[0].empty())
{
    std::cin >> userid[0];
}
else {
    std::cin >> userid[1];
}
for (int i = 0; i < 2; i++)
{
    std::cout << " Element of array: " << i << " is > " << userid[i] << std::endl;
}

请考虑以下“空”数组元素的定义:
a) 未初始化(无用,无法检查)
b) 从未写信给 (same as a) )
c) 包含"" (可能,但意味着""不能被接受为实际内容)
d) 根据维护该信息的第二个数组为空(这是我几乎推荐的)
e)包含一个带有字符串的结构和一个维护的“空”标志(我推荐这个)

无论您做什么,请确保在第一次读取访问它们之前初始化所有变量和数组元素; 即在所有情况下,首先写一些对它有意义的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM