检查选定的数组是否存储了任何内容?C

Checking to see if the selected array has anything stored in it? C++

本文关键字:任何内 存储 是否 数组 检查      更新时间:2023-10-16
#include <iostream> 
#include <string>
#include <math.h> 
#include <fstream>
using namespace std;
int main()
{
    int i;
    string information[10][7];
    //This bit should check if theres anything stored currently.
    cout << "nWhich Slot would you like to store the informaton in ?(1-10)";
    cin >> i;
    i--;
    //input
    for (int j=0;j<7;j++)
    {
        switch(j+1)
        {
        case 1:
            cout << "nFirst Name: ";
            break;
        case 2:
            cout << "nLast Name: ";
            break;
        case 3:
            cout << "nAge: ";
            break;
        case 4:
            cout << "nEmail: ";
            break;
        case 5:
            cout << "nDoor Number: ";
            break;
        case 6:
            cout << "nRoad Name: ";
            break;
        case 7:
            cout << "nPost Code: ";
            break;
        default:
            ;
        }
        cin >> information[i][j];
    }
    // output
    for (int j=0;j<7;j++)
    {   
        switch(j+1)
        {
        case 1:
            cout << "nFirst Name: ";
            break;
        case 2:
            cout << "nLast Name: ";
            break;
        case 3:
            cout << "nAge: ";
            break;
        case 4:
            cout << "nEmail: ";
            break;
        case 5:
            cout << "nDoor Number: ";
            break;
        case 6:
            cout << "nRoad Name: ";
            break;
        case 7:
            cout << "nPost Code: ";
            break;
        default:
            ;
        }       
        cout << information[i][j];
    }
    system("PAUSE");
    return 0;
}

基本上,问题是我对这一点代码做什么,以检查用户选择的数组是否已经存储了信息?我该如何检查。另外,我想知道如何通过输入年龄来搜索用户的详细信息,例如即使我使用的字符串是可能的,或者我必须使用int。?

使用布尔数组可能会产生一些开销,但是如果您也执行删除也可能会有所帮助:

bool stored[10] = {false};
/* ... */
do {
  cout << "nWhich Slot would you like to store the informaton in ?(1-10)";
  cin >> i;
  i--;
} while (stored[i]);
stored[i] = true;

当然,您可以定义一个具有7个字符串成员的类/结构,其中一个布尔值用于此信息。

让我们从代码看起来好像返回到弗隆的事实首先,您有十个字段,有7个Charcters长。即使我们四处互换,有10个字符的名称或电子邮件地址也很短。我的姓是9个字母,我看到的名字比那更长。

,最后是您的问题。您可以使用Perreal建议的方法。但是我可能只是做if (string[i][0] == "") ... stuff to do when it's empty ...;

使用string::length函数(information[1].length())怎么样?如果返回0,则尚未初始化。

只是要找到一些东西。