簡體   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